Add support for dicts in array (#375)

Fixes #111.
This commit is contained in:
Federico G. Schwindt 2021-11-02 00:53:02 +00:00 committed by GitHub
parent 78448ee023
commit 6973188a74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 4 deletions

View File

@ -231,3 +231,10 @@ func (a *Array) MACAddr(ha net.HardwareAddr) *Array {
a.buf = enc.AppendMACAddr(enc.AppendArrayDelim(a.buf), ha) a.buf = enc.AppendMACAddr(enc.AppendArrayDelim(a.buf), ha)
return a 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
}

View File

@ -27,8 +27,12 @@ func TestArray(t *testing.T) {
RawJSON([]byte(`{"some":"json"}`)). RawJSON([]byte(`{"some":"json"}`)).
Time(time.Time{}). Time(time.Time{}).
IPAddr(net.IP{192, 168, 0, 10}). IPAddr(net.IP{192, 168, 0, 10}).
Dur(0) 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]` 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 { if got := decodeObjectToStr(a.write([]byte{})); got != want {
t.Errorf("Array.write()\ngot: %s\nwant: %s", got, want) t.Errorf("Array.write()\ngot: %s\nwant: %s", got, want)
} }

View File

@ -238,11 +238,15 @@ func ExampleEvent_Array() {
Str("foo", "bar"). Str("foo", "bar").
Array("array", zerolog.Arr(). Array("array", zerolog.Arr().
Str("baz"). Str("baz").
Int(1), Int(1).
Dict(zerolog.Dict().
Str("bar", "baz").
Int("n", 1),
),
). ).
Msg("hello world") 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() { func ExampleEvent_Array_object() {