github.com/livekit/protocol@v1.39.3/logger/slice.go (about)

     1  // Copyright 2023 LiveKit, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package logger
    16  
    17  import (
    18  	"time"
    19  
    20  	"go.uber.org/multierr"
    21  	"go.uber.org/zap/zapcore"
    22  	"google.golang.org/protobuf/proto"
    23  )
    24  
    25  type protoSlice[T proto.Message] []T
    26  
    27  func ProtoSlice[T proto.Message](s []T) zapcore.ArrayMarshaler {
    28  	return protoSlice[T](s)
    29  }
    30  
    31  func (s protoSlice[T]) MarshalLogArray(e zapcore.ArrayEncoder) error {
    32  	var err error
    33  	for _, v := range s {
    34  		err = multierr.Append(err, e.AppendObject(Proto(v)))
    35  	}
    36  	return err
    37  }
    38  
    39  type objectSlice[T zapcore.ObjectMarshaler] []T
    40  
    41  func ObjectSlice[T zapcore.ObjectMarshaler](s []T) zapcore.ArrayMarshaler {
    42  	return objectSlice[T](s)
    43  }
    44  
    45  func (s objectSlice[T]) MarshalLogArray(e zapcore.ArrayEncoder) error {
    46  	var err error
    47  	for _, v := range s {
    48  		err = multierr.Append(err, e.AppendObject(v))
    49  	}
    50  	return err
    51  }
    52  
    53  type timeSlice []time.Time
    54  
    55  func TimeSlice(s []time.Time) zapcore.ArrayMarshaler {
    56  	return timeSlice(s)
    57  }
    58  
    59  func (s timeSlice) MarshalLogArray(e zapcore.ArrayEncoder) error {
    60  	for _, v := range s {
    61  		e.AppendTime(v)
    62  	}
    63  	return nil
    64  }
    65  
    66  type durationSlice []time.Duration
    67  
    68  func DurationSlice(s []time.Duration) zapcore.ArrayMarshaler {
    69  	return durationSlice(s)
    70  }
    71  
    72  func (s durationSlice) MarshalLogArray(e zapcore.ArrayEncoder) error {
    73  	for _, v := range s {
    74  		e.AppendDuration(v)
    75  	}
    76  	return nil
    77  }
    78  
    79  type boolSlice[T ~bool] []T
    80  
    81  func BoolSlice[T ~bool](s []T) zapcore.ArrayMarshaler {
    82  	return boolSlice[T](s)
    83  }
    84  
    85  func (s boolSlice[T]) MarshalLogArray(e zapcore.ArrayEncoder) error {
    86  	for _, v := range s {
    87  		e.AppendBool(bool(v))
    88  	}
    89  	return nil
    90  }
    91  
    92  type byteStringSlice[T ~[]byte] []T
    93  
    94  func ByteStringSlice[T ~[]byte](s []T) zapcore.ArrayMarshaler {
    95  	return byteStringSlice[T](s)
    96  }
    97  
    98  func (s byteStringSlice[T]) MarshalLogArray(e zapcore.ArrayEncoder) error {
    99  	for _, v := range s {
   100  		e.AppendByteString([]byte(v))
   101  	}
   102  	return nil
   103  }
   104  
   105  type complex128Slice[T ~complex128] []T
   106  
   107  func Complex128Slice[T ~complex128](s []T) zapcore.ArrayMarshaler {
   108  	return complex128Slice[T](s)
   109  }
   110  
   111  func (s complex128Slice[T]) MarshalLogArray(e zapcore.ArrayEncoder) error {
   112  	for _, v := range s {
   113  		e.AppendComplex128(complex128(v))
   114  	}
   115  	return nil
   116  }
   117  
   118  type complex64Slice[T ~complex64] []T
   119  
   120  func Complex64Slice[T ~complex64](s []T) zapcore.ArrayMarshaler {
   121  	return complex64Slice[T](s)
   122  }
   123  
   124  func (s complex64Slice[T]) MarshalLogArray(e zapcore.ArrayEncoder) error {
   125  	for _, v := range s {
   126  		e.AppendComplex64(complex64(v))
   127  	}
   128  	return nil
   129  }
   130  
   131  type float64Slice[T ~float64] []T
   132  
   133  func Float64Slice[T ~float64](s []T) zapcore.ArrayMarshaler {
   134  	return float64Slice[T](s)
   135  }
   136  
   137  func (s float64Slice[T]) MarshalLogArray(e zapcore.ArrayEncoder) error {
   138  	for _, v := range s {
   139  		e.AppendFloat64(float64(v))
   140  	}
   141  	return nil
   142  }
   143  
   144  type float32Slice[T ~float32] []T
   145  
   146  func Float32Slice[T ~float32](s []T) zapcore.ArrayMarshaler {
   147  	return float32Slice[T](s)
   148  }
   149  
   150  func (s float32Slice[T]) MarshalLogArray(e zapcore.ArrayEncoder) error {
   151  	for _, v := range s {
   152  		e.AppendFloat32(float32(v))
   153  	}
   154  	return nil
   155  }
   156  
   157  type intSlice[T ~int] []T
   158  
   159  func IntSlice[T ~int](s []T) zapcore.ArrayMarshaler {
   160  	return intSlice[T](s)
   161  }
   162  
   163  func (s intSlice[T]) MarshalLogArray(e zapcore.ArrayEncoder) error {
   164  	for _, v := range s {
   165  		e.AppendInt(int(v))
   166  	}
   167  	return nil
   168  }
   169  
   170  type int64Slice[T ~int64] []T
   171  
   172  func Int64Slice[T ~int64](s []T) zapcore.ArrayMarshaler {
   173  	return int64Slice[T](s)
   174  }
   175  
   176  func (s int64Slice[T]) MarshalLogArray(e zapcore.ArrayEncoder) error {
   177  	for _, v := range s {
   178  		e.AppendInt64(int64(v))
   179  	}
   180  	return nil
   181  }
   182  
   183  type int32Slice[T ~int32] []T
   184  
   185  func Int32Slice[T ~int32](s []T) zapcore.ArrayMarshaler {
   186  	return int32Slice[T](s)
   187  }
   188  
   189  func (s int32Slice[T]) MarshalLogArray(e zapcore.ArrayEncoder) error {
   190  	for _, v := range s {
   191  		e.AppendInt32(int32(v))
   192  	}
   193  	return nil
   194  }
   195  
   196  type int16Slice[T ~int16] []T
   197  
   198  func Int16Slice[T ~int16](s []T) zapcore.ArrayMarshaler {
   199  	return int16Slice[T](s)
   200  }
   201  
   202  func (s int16Slice[T]) MarshalLogArray(e zapcore.ArrayEncoder) error {
   203  	for _, v := range s {
   204  		e.AppendInt16(int16(v))
   205  	}
   206  	return nil
   207  }
   208  
   209  type int8Slice[T ~int8] []T
   210  
   211  func Int8Slice[T ~int8](s []T) zapcore.ArrayMarshaler {
   212  	return int8Slice[T](s)
   213  }
   214  
   215  func (s int8Slice[T]) MarshalLogArray(e zapcore.ArrayEncoder) error {
   216  	for _, v := range s {
   217  		e.AppendInt8(int8(v))
   218  	}
   219  	return nil
   220  }
   221  
   222  type stringSlice[T ~string] []T
   223  
   224  func StringSlice[T ~string](s []T) zapcore.ArrayMarshaler {
   225  	return stringSlice[T](s)
   226  }
   227  
   228  func (s stringSlice[T]) MarshalLogArray(e zapcore.ArrayEncoder) error {
   229  	for _, v := range s {
   230  		e.AppendString(string(v))
   231  	}
   232  	return nil
   233  }
   234  
   235  type uintSlice[T ~uint] []T
   236  
   237  func UintSlice[T ~uint](s []T) zapcore.ArrayMarshaler {
   238  	return uintSlice[T](s)
   239  }
   240  
   241  func (s uintSlice[T]) MarshalLogArray(e zapcore.ArrayEncoder) error {
   242  	for _, v := range s {
   243  		e.AppendUint(uint(v))
   244  	}
   245  	return nil
   246  }
   247  
   248  type uint64Slice[T ~uint64] []T
   249  
   250  func Uint64Slice[T ~uint64](s []T) zapcore.ArrayMarshaler {
   251  	return uint64Slice[T](s)
   252  }
   253  
   254  func (s uint64Slice[T]) MarshalLogArray(e zapcore.ArrayEncoder) error {
   255  	for _, v := range s {
   256  		e.AppendUint64(uint64(v))
   257  	}
   258  	return nil
   259  }
   260  
   261  type uint32Slice[T ~uint32] []T
   262  
   263  func Uint32Slice[T ~uint32](s []T) zapcore.ArrayMarshaler {
   264  	return uint32Slice[T](s)
   265  }
   266  
   267  func (s uint32Slice[T]) MarshalLogArray(e zapcore.ArrayEncoder) error {
   268  	for _, v := range s {
   269  		e.AppendUint32(uint32(v))
   270  	}
   271  	return nil
   272  }
   273  
   274  type uint16Slice[T ~uint16] []T
   275  
   276  func Uint16Slice[T ~uint16](s []T) zapcore.ArrayMarshaler {
   277  	return uint16Slice[T](s)
   278  }
   279  
   280  func (s uint16Slice[T]) MarshalLogArray(e zapcore.ArrayEncoder) error {
   281  	for _, v := range s {
   282  		e.AppendUint16(uint16(v))
   283  	}
   284  	return nil
   285  }
   286  
   287  type uint8Slice[T ~uint8] []T
   288  
   289  func Uint8Slice[T ~uint8](s []T) zapcore.ArrayMarshaler {
   290  	return uint8Slice[T](s)
   291  }
   292  
   293  func (s uint8Slice[T]) MarshalLogArray(e zapcore.ArrayEncoder) error {
   294  	for _, v := range s {
   295  		e.AppendUint8(uint8(v))
   296  	}
   297  	return nil
   298  }
   299  
   300  type uintptrSlice[T ~uintptr] []T
   301  
   302  func UintptrSlice[T ~uintptr](s []T) zapcore.ArrayMarshaler {
   303  	return uintptrSlice[T](s)
   304  }
   305  
   306  func (s uintptrSlice[T]) MarshalLogArray(e zapcore.ArrayEncoder) error {
   307  	for _, v := range s {
   308  		e.AppendUintptr(uintptr(v))
   309  	}
   310  	return nil
   311  }