Skip to content
Merged
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
1 change: 1 addition & 0 deletions benchmarks/zap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func fakeFields() []zap.Field {
zap.Times("times", _tenTimes),
zap.Object("user1", _oneUser),
zap.Object("user2", _oneUser),
zap.Object("user3", nil),
zap.Array("users", _tenUsers),
zap.Error(errExample),
}
Expand Down
3 changes: 3 additions & 0 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,9 @@ func Durationp(key string, val *time.Duration) Field {
// struct-like user-defined types to the logging context. The struct's
// MarshalLogObject method is called lazily.
func Object(key string, val zapcore.ObjectMarshaler) Field {
if val == nil {
return nilField(key)
}
return Field{Key: key, Type: zapcore.ObjectMarshalerType, Interface: val}
}

Expand Down
1 change: 1 addition & 0 deletions field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ func TestFieldConstructors(t *testing.T) {
{"Any:PtrUintptr", Any("k", &uintptrVal), Uintptr("k", uintptrVal)},
{"Any:ErrorNil", Any("k", nilErr), nilField("k")},
{"Namespace", Namespace("k"), Field{Key: "k", Type: zapcore.NamespaceType}},
{"Object:Nil", Object("k", nil), nilField("k")},
}

for _, tt := range tests {
Expand Down
11 changes: 11 additions & 0 deletions zapcore/field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func TestFields(t *testing.T) {
{t: Uint8Type, i: 42, want: uint8(42)},
{t: UintptrType, i: 42, want: uintptr(42)},
{t: ReflectType, iface: users(2), want: users(2)},
{t: ReflectType, iface: nil, want: nil},
{t: NamespaceType, want: map[string]interface{}{}},
{t: StringerType, iface: users(2), want: "2 users"},
{t: StringerType, iface: &obj{}, want: "obj"},
Expand Down Expand Up @@ -330,6 +331,16 @@ func TestEquals(t *testing.T) {
b: zap.Object("k", zap.DictObject(zap.String("a", "d"))),
want: false,
},
{
a: zap.Object("k", nil),
b: zap.Object("k", nil),
want: true,
},
{
a: zap.Object("k", users(10)),
b: zap.Object("k", nil),
want: false,
},
}

for _, tt := range tests {
Expand Down