enable adding raw JSON to an array (#145)

* enable adding JSON to an array

* do not forget the comment
This commit is contained in:
Nelz 2019-11-04 09:47:06 -08:00 committed by Olivier Poitrey
parent 7592fcbe60
commit 5861452d64
2 changed files with 8 additions and 1 deletions

View File

@ -85,6 +85,12 @@ func (a *Array) Hex(val []byte) *Array {
return a
}
// RawJSON adds already encoded JSON to the array.
func (a *Array) RawJSON(val []byte) *Array {
a.buf = appendJSON(enc.AppendArrayDelim(a.buf), val)
return a
}
// Err serializes and appends the err to the array.
func (a *Array) Err(err error) *Array {
marshaled := ErrorMarshalFunc(err)

View File

@ -24,10 +24,11 @@ func TestArray(t *testing.T) {
Str("a").
Bytes([]byte("b")).
Hex([]byte{0x1f}).
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","0001-01-01T00:00:00Z","192.168.0.10",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]`
if got := decodeObjectToStr(a.write([]byte{})); got != want {
t.Errorf("Array.write()\ngot: %s\nwant: %s", got, want)
}