Skip to content

Commit

Permalink
Add support for dicts in array (rs#375)
Browse files Browse the repository at this point in the history
Fixes rs#111.
  • Loading branch information
fgsch authored Nov 2, 2021
1 parent 78448ee commit 6973188
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
7 changes: 7 additions & 0 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,10 @@ func (a *Array) MACAddr(ha net.HardwareAddr) *Array {
a.buf = enc.AppendMACAddr(enc.AppendArrayDelim(a.buf), ha)
return a
}

// Dict adds the dict Event to the array
func (a *Array) Dict(dict *Event) *Array {
dict.buf = enc.AppendEndMarker(dict.buf)
a.buf = append(enc.AppendArrayDelim(a.buf), dict.buf...)
return a
}
8 changes: 6 additions & 2 deletions array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ func TestArray(t *testing.T) {
RawJSON([]byte(`{"some":"json"}`)).
Time(time.Time{}).
IPAddr(net.IP{192, 168, 0, 10}).
Dur(0)
want := `[true,1,2,3,4,5,6,7,8,9,10,11.98122,12.987654321,"a","b","1f",{"some":"json"},"0001-01-01T00:00:00Z","192.168.0.10",0]`
Dur(0).
Dict(Dict().
Str("bar", "baz").
Int("n", 1),
)
want := `[true,1,2,3,4,5,6,7,8,9,10,11.98122,12.987654321,"a","b","1f",{"some":"json"},"0001-01-01T00:00:00Z","192.168.0.10",0,{"bar":"baz","n":1}]`
if got := decodeObjectToStr(a.write([]byte{})); got != want {
t.Errorf("Array.write()\ngot: %s\nwant: %s", got, want)
}
Expand Down
8 changes: 6 additions & 2 deletions log_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,15 @@ func ExampleEvent_Array() {
Str("foo", "bar").
Array("array", zerolog.Arr().
Str("baz").
Int(1),
Int(1).
Dict(zerolog.Dict().
Str("bar", "baz").
Int("n", 1),
),
).
Msg("hello world")

// Output: {"foo":"bar","array":["baz",1],"message":"hello world"}
// Output: {"foo":"bar","array":["baz",1,{"bar":"baz","n":1}],"message":"hello world"}
}

func ExampleEvent_Array_object() {
Expand Down

0 comments on commit 6973188

Please sign in to comment.