Skip to content

Commit

Permalink
Add missing (forgotten?) AppendBinary method to ArrayEncoder
Browse files Browse the repository at this point in the history
  • Loading branch information
NWilson committed Dec 5, 2018
1 parent 09235d1 commit 293c067
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion zapcore/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ type ArrayEncoder interface {
type PrimitiveArrayEncoder interface {
// Built-in types.
AppendBool(bool)
AppendByteString([]byte) // for UTF-8 encoded bytes
AppendBinary(value []byte) // for arbitrary bytes
AppendByteString([]byte) // for UTF-8 encoded bytes
AppendComplex128(complex128)
AppendComplex64(complex64)
AppendFloat64(float64)
Expand Down
4 changes: 4 additions & 0 deletions zapcore/json_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ func (enc *jsonEncoder) AppendBool(val bool) {
enc.buf.AppendBool(val)
}

func (enc *jsonEncoder) AppendBinary(val []byte) {
enc.AppendString(base64.StdEncoding.EncodeToString(val))
}

func (enc *jsonEncoder) AppendByteString(val []byte) {
enc.addElementSeparator()
enc.buf.AppendByte('"')
Expand Down
2 changes: 2 additions & 0 deletions zapcore/json_encoder_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ func TestJSONEncoderArrays(t *testing.T) {
f func(ArrayEncoder)
}{
{"bool", `[true,true]`, func(e ArrayEncoder) { e.AppendBool(true) }},
{"binary", `["aw==","aw=="]`, func(e ArrayEncoder) { e.AppendBinary([]byte("k")) }},
{"binary", `["a1w=","a1w="]`, func(e ArrayEncoder) { e.AppendBinary([]byte(`k\`)) }},
{"byteString", `["k","k"]`, func(e ArrayEncoder) { e.AppendByteString([]byte("k")) }},
{"byteString", `["k\\","k\\"]`, func(e ArrayEncoder) { e.AppendByteString([]byte(`k\`)) }},
{"complex128", `["1+2i","1+2i"]`, func(e ArrayEncoder) { e.AppendComplex128(1 + 2i) }},
Expand Down
1 change: 1 addition & 0 deletions zapcore/memory_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func (s *sliceArrayEncoder) AppendReflected(v interface{}) error {
}

func (s *sliceArrayEncoder) AppendBool(v bool) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendBinary(v []byte) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendByteString(v []byte) { s.elems = append(s.elems, string(v)) }
func (s *sliceArrayEncoder) AppendComplex128(v complex128) { s.elems = append(s.elems, v) }
func (s *sliceArrayEncoder) AppendComplex64(v complex64) { s.elems = append(s.elems, v) }
Expand Down

0 comments on commit 293c067

Please sign in to comment.