Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't panic on reflected slice. #10

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,30 @@ func (enc *logfmtEncoder) AddReflected(key string, value interface{}) error {
switch rvalue.Kind() {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Struct:
return ErrUnsupportedValueType
case reflect.Array, reflect.Slice, reflect.Ptr:
case reflect.Ptr:
if rvalue.IsNil() {
enc.AddByteString(key, nil)
return nil
}
return enc.AddReflected(key, rvalue.Elem().Interface())
case reflect.Slice:
if rvalue.IsNil() {
enc.AddByteString(key, nil)
return nil
}
// A non-nil slice is handled the same way as an array.
fallthrough
case reflect.Array:
marshal := zapcore.ArrayMarshalerFunc(func(aenc zapcore.ArrayEncoder) error {
for i := 0; i < rvalue.Len(); i++ {
v := rvalue.Index(i).Interface()
if err := aenc.AppendReflected(v); err != nil {
return err
}
}
return nil
})
return enc.AddArray(key, marshal)
}
enc.AddString(key, fmt.Sprint(value))
return nil
Expand Down Expand Up @@ -519,7 +537,35 @@ func (enc *literalEncoder) AppendObject(zapcore.ObjectMarshaler) error {
}

func (enc *literalEncoder) AppendReflected(value interface{}) error {
return ErrUnsupportedValueType
rvalue := reflect.ValueOf(value)
switch rvalue.Kind() {
case reflect.Bool:
enc.AppendBool(value.(bool))
return nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
enc.AppendInt64(rvalue.Int())
return nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
enc.AppendUint64(rvalue.Uint())
return nil
case reflect.Float32:
enc.AppendFloat32(value.(float32))
return nil
case reflect.Float64:
enc.AppendFloat64(value.(float64))
return nil
case reflect.Complex64:
enc.AppendComplex64(value.(complex64))
return nil
case reflect.Complex128:
enc.AppendComplex128(value.(complex128))
return nil
case reflect.String:
enc.AppendString(value.(string))
return nil
default:
return ErrUnsupportedValueType
}
}

func (enc *literalEncoder) addSeparator() {
Expand Down
3 changes: 3 additions & 0 deletions encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ func TestEncoderObjectFields(t *testing.T) {
e.OpenNamespace("innermost")
},
},
{"reflected slice", "k=a,b", func(e zapcore.Encoder) { e.AddReflected("k", []string{"a", "b"}) }},
{"reflected slice of int", "k=1,2,3", func(e zapcore.Encoder) { e.AddReflected("k", []int16{1, 2, 3}) }},
{"reflected array", "k=a,b", func(e zapcore.Encoder) { e.AddReflected("k", [2]string{"a", "b"}) }},
}

for _, tt := range tests {
Expand Down
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
module github.com/jsternberg/zap-logfmt

go 1.17

require (
github.com/stretchr/testify v1.2.2
go.uber.org/zap v1.9.1
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.2
go.uber.org/atomic v1.3.2 // indirect
go.uber.org/multierr v1.1.0 // indirect
go.uber.org/zap v1.9.1
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
Expand Down