From 7230f5b8befd76bc62c819bf3ef44a97bdd986a8 Mon Sep 17 00:00:00 2001 From: Russ Egan Date: Fri, 24 Feb 2017 15:08:49 -0500 Subject: [PATCH 1/2] zap.Any() maps []byte and []uint8 values to zap.Binary() #335 --- field.go | 10 ++++++++-- field_test.go | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/field.go b/field.go index 27be5c726..b247bdc09 100644 --- a/field.go +++ b/field.go @@ -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: @@ -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: diff --git a/field_test.go b/field_test.go index 6de2bd431..7c1a1295f 100644 --- a/field_test.go +++ b/field_test.go @@ -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)}, @@ -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))}, From ac9b686c58d0409c7ec15bb52d07d7a0f677c1bc Mon Sep 17 00:00:00 2001 From: Akshay Shah Date: Fri, 24 Feb 2017 14:10:05 -0800 Subject: [PATCH 2/2] Reword documentation --- field.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/field.go b/field.go index b247bdc09..c8ff1372a 100644 --- a/field.go +++ b/field.go @@ -224,12 +224,10 @@ 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 +// Since byte/uint8 and rune/int32 are aliases, Any can't differentiate between +// them. To minimize suprise, []byte values are treated as binary blobs, byte +// values are treated as uint8, and runes are always treated as integers. func Any(key string, value interface{}) zapcore.Field { switch val := value.(type) { case zapcore.ObjectMarshaler: