-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add support for custom ReflectType encoder #1039
Changes from 2 commits
f9e2251
ba0bbf8
ce7215a
31bc967
3babc29
17016b9
bd74de7
c65627f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ package zapcore | |
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
"io" | ||
"math" | ||
"sync" | ||
"time" | ||
|
@@ -64,7 +64,7 @@ type jsonEncoder struct { | |
|
||
// for encoding generic values by reflection | ||
reflectBuf *buffer.Buffer | ||
reflectEnc *json.Encoder | ||
reflectEnc ReflectedEncoder | ||
} | ||
|
||
// NewJSONEncoder creates a fast, low-allocation JSON encoder. The encoder | ||
|
@@ -152,10 +152,14 @@ func (enc *jsonEncoder) AddInt64(key string, val int64) { | |
func (enc *jsonEncoder) resetReflectBuf() { | ||
if enc.reflectBuf == nil { | ||
enc.reflectBuf = bufferpool.Get() | ||
enc.reflectEnc = json.NewEncoder(enc.reflectBuf) | ||
|
||
// For consistency with our custom JSON encoder. | ||
enc.reflectEnc.SetEscapeHTML(false) | ||
// If no EncoderConfig.NewReflectedEncoder is provided by the user, then use default | ||
var newReflectedEncoder func(io.Writer) ReflectedEncoder | ||
if enc.NewReflectedEncoder == nil { | ||
newReflectedEncoder = GetDefaultReflectedEncoder() | ||
} else { | ||
newReflectedEncoder = enc.NewReflectedEncoder | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you please add a test for the custom reflection encoder case? |
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should do this in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @abhinav |
||
enc.reflectEnc = newReflectedEncoder(enc.reflectBuf) | ||
} else { | ||
enc.reflectBuf.Reset() | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,52 @@ | ||||||
// Copyright (c) 2016 Uber Technologies, Inc. | ||||||
// | ||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||
// of this software and associated documentation files (the "Software"), to deal | ||||||
// in the Software without restriction, including without limitation the rights | ||||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||
// copies of the Software, and to permit persons to whom the Software is | ||||||
// furnished to do so, subject to the following conditions: | ||||||
// | ||||||
// The above copyright notice and this permission notice shall be included in | ||||||
// all copies or substantial portions of the Software. | ||||||
// | ||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||||
// THE SOFTWARE. | ||||||
|
||||||
package zapcore | ||||||
|
||||||
import ( | ||||||
"encoding/json" | ||||||
"io" | ||||||
) | ||||||
|
||||||
// A ReflectedEncoder handles the serialization of Field which have FieldType = ReflectType and writes them to the underlying data stream | ||||||
// The underlying data stream is provided by zap during initialization. See EncoderConfig.NewReflectedEncoder | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
type ReflectedEncoder interface { | ||||||
// Encode encodes and writes to the underlying data stream | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
Encode(interface{}) error | ||||||
} | ||||||
|
||||||
func GetDefaultReflectedEncoder() func(writer io.Writer) ReflectedEncoder { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We would prefer not to export this function. We also don't need the wrapper type because json.Encoder satisfies the ReflectedEncoder interface. So something like the following should suffice:
|
||||||
return func(writer io.Writer) ReflectedEncoder { | ||||||
stdJsonEncoder := json.NewEncoder(writer) | ||||||
// For consistency with our custom JSON encoder. | ||||||
stdJsonEncoder.SetEscapeHTML(false) | ||||||
return &stdReflectedEncoder{ | ||||||
encoder: stdJsonEncoder, | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
type stdReflectedEncoder struct { | ||||||
encoder *json.Encoder | ||||||
} | ||||||
|
||||||
func (enc *stdReflectedEncoder) Encode(obj interface{}) error { | ||||||
return enc.encoder.Encode(obj) | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this a function that is not JSON/YAML encodeable, we'll have to use
-
here.