Skip to content

Commit

Permalink
Replace Nest in KeyValue interface with LogMarshalerFunc type
Browse files Browse the repository at this point in the history
This reduces the KeyValue interface while still allowing a function
to be used to build an object using LogMarshalerFunc
  • Loading branch information
prashantv committed Jun 27, 2016
1 parent 2d7efa8 commit 8bc82d9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
17 changes: 5 additions & 12 deletions json_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,15 @@ func (enc *jsonEncoder) AddFloat64(key string, val float64) {
}
}

// Nest allows the caller to populate a nested object under the provided key.
func (enc *jsonEncoder) Nest(key string, f func(KeyValue) error) error {
enc.addKey(key)
enc.bytes = append(enc.bytes, '{')
err := f(enc)
enc.bytes = append(enc.bytes, '}')
return err
}

// AddMarshaler adds a LogMarshaler to the encoder's fields.
//
// TODO: Encode the error into the message instead of returning.
func (enc *jsonEncoder) AddMarshaler(key string, obj LogMarshaler) error {
return enc.Nest(key, func(kv KeyValue) error {
return obj.MarshalLog(kv)
})
enc.addKey(key)
enc.bytes = append(enc.bytes, '{')
err := obj.MarshalLog(enc)
enc.bytes = append(enc.bytes, '}')
return err
}

// AddObject uses reflection to add an arbitrary object to the logging context.
Expand Down
2 changes: 1 addition & 1 deletion json_encoder_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func BenchmarkJSONEncoderNest(b *testing.B) {
}
for i := 0; i < b.N; i++ {
enc := newJSONEncoder()
enc.Nest("nested", f)
enc.AddMarshaler("nested", LogMarshalerFunc(f))
enc.Free()
}
}
Expand Down
24 changes: 12 additions & 12 deletions json_encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,18 @@ func TestJSONWriteMessage(t *testing.T) {
})
}

func TestJSONNest(t *testing.T) {
withJSONEncoder(func(enc *jsonEncoder) {
err := enc.Nest("nested", func(kv KeyValue) error {
kv.AddString("sub-foo", "sub-bar")
return nil
})
require.NoError(t, err, "Unexpected error using Nest.")
enc.AddString("baz", "bing")

assertJSON(t, `"foo":"bar","nested":{"sub-foo":"sub-bar"},"baz":"bing"`, enc)
})
}
// func TestJSONNest(t *testing.T) {
// withJSONEncoder(func(enc *jsonEncoder) {
// err := enc.Nest("nested", func(kv KeyValue) error {
// kv.AddString("sub-foo", "sub-bar")
// return nil
// })
// require.NoError(t, err, "Unexpected error using Nest.")
// enc.AddString("baz", "bing")

// assertJSON(t, `"foo":"bar","nested":{"sub-foo":"sub-bar"},"baz":"bing"`, enc)
// })
// }

type loggable struct{}

Expand Down
1 change: 0 additions & 1 deletion keyvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,4 @@ type KeyValue interface {
// allocation-heavy. Consider implementing the LogMarshaler interface instead.
AddObject(string, interface{})
AddString(string, string)
Nest(string, func(KeyValue) error) error
}
9 changes: 9 additions & 0 deletions marshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,12 @@ package zap
type LogMarshaler interface {
MarshalLog(KeyValue) error
}

// LogMarshalerFunc is a type adapter that allows using a function as a
// LogMarshaler.
type LogMarshalerFunc func(KeyValue) error

// MarshalLog calls the underlying function.
func (f LogMarshalerFunc) MarshalLog(kv KeyValue) error {
return f(kv)
}

0 comments on commit 8bc82d9

Please sign in to comment.