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

Treat byte slices as binary blobs in Any #339

Merged
merged 3 commits into from
Feb 24, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 8 additions & 2 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ func Object(key string, val zapcore.ObjectMarshaler) zapcore.Field {
// Any takes a key and an arbitrary value and chooses the best way to represent
// them as a field, falling back to a reflection-based approach only if
// necessary.
// In golang, byte is an alias of uint8, and rune is an alias of int32, so:
//
// - []byte and []uint8 values will return Binary fields
// - byte and uint8 values will return Uint fields
// - []rune and []int32 values will return Int32s fields
// - rune and int32 values will return Int32 fields
func Any(key string, value interface{}) zapcore.Field {
switch val := value.(type) {
case zapcore.ObjectMarshaler:
Expand Down Expand Up @@ -292,8 +298,8 @@ func Any(key string, value interface{}) zapcore.Field {
return Uint16s(key, val)
case uint8:
return Uint8(key, val)
case []uint8:
return Uint8s(key, val)
case []byte:
return Binary(key, val)
case uintptr:
return Uintptr(key, val)
case []uintptr:
Expand Down
4 changes: 2 additions & 2 deletions field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func TestFieldConstructors(t *testing.T) {
{"Any:Bool", Any("k", true), Bool("k", true)},
{"Any:Bools", Any("k", []bool{true}), Bools("k", []bool{true})},
{"Any:Byte", Any("k", byte(1)), Uint8("k", 1)},
{"Any:Bytes", Any("k", []byte{1}), Uint8s("k", []uint8{1})},
{"Any:Bytes", Any("k", []byte{1}), Binary("k", []byte{1})},
{"Any:Complex128", Any("k", 1+2i), Complex128("k", 1+2i)},
{"Any:Complex128s", Any("k", []complex128{1 + 2i}), Complex128s("k", []complex128{1 + 2i})},
{"Any:Complex64", Any("k", complex64(1+2i)), Complex64("k", 1+2i)},
Expand Down Expand Up @@ -144,7 +144,7 @@ func TestFieldConstructors(t *testing.T) {
{"Any:Uint16", Any("k", uint16(1)), Uint16("k", 1)},
{"Any:Uint16s", Any("k", []uint16{1}), Uint16s("k", []uint16{1})},
{"Any:Uint8", Any("k", uint8(1)), Uint8("k", 1)},
{"Any:Uint8s", Any("k", []uint8{1}), Uint8s("k", []uint8{1})},
{"Any:Uint8s", Any("k", []uint8{1}), Binary("k", []uint8{1})},
{"Any:Uintptr", Any("k", uintptr(1)), Uintptr("k", 1)},
{"Any:Uintptrs", Any("k", []uintptr{1}), Uintptrs("k", []uintptr{1})},
{"Any:Time", Any("k", time.Unix(0, 0)), Time("k", time.Unix(0, 0))},
Expand Down