Skip to content

Commit

Permalink
patched; panic Event.Object() and Event.EmbedObject() with nil (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
spikeekips committed Aug 1, 2021
1 parent 0872592 commit c1533bd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
9 changes: 9 additions & 0 deletions event.go
Expand Up @@ -207,6 +207,12 @@ func (e *Event) Object(key string, obj LogObjectMarshaler) *Event {
return e
}
e.buf = enc.AppendKey(e.buf, key)
if obj == nil {
e.buf = enc.AppendNil(e.buf)

return e
}

e.appendObject(obj)
return e
}
Expand All @@ -224,6 +230,9 @@ func (e *Event) EmbedObject(obj LogObjectMarshaler) *Event {
if e == nil {
return e
}
if obj == nil {
return e
}
obj.MarshalZerologObject(e)
return e
}
Expand Down
26 changes: 26 additions & 0 deletions event_test.go
Expand Up @@ -37,3 +37,29 @@ func TestEvent_AnErr(t *testing.T) {
})
}
}

func TestEvent_ObjectWithNil(t *testing.T) {
var buf bytes.Buffer
e := newEvent(levelWriterAdapter{&buf}, DebugLevel)
_ = e.Object("obj", nil)
_ = e.write()

want := `{"obj":null}`
got := strings.TrimSpace(buf.String())
if got != want {
t.Errorf("Event.Object() = %q, want %q", got, want)
}
}

func TestEvent_EmbedObjectWithNil(t *testing.T) {
var buf bytes.Buffer
e := newEvent(levelWriterAdapter{&buf}, DebugLevel)
_ = e.EmbedObject(nil)
_ = e.write()

want := "{}"
got := strings.TrimSpace(buf.String())
if got != want {
t.Errorf("Event.EmbedObject() = %q, want %q", got, want)
}
}

0 comments on commit c1533bd

Please sign in to comment.