Skip to content
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

Merged
merged 8 commits into from Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions zapcore/encoder.go
Expand Up @@ -22,6 +22,7 @@ package zapcore

import (
"encoding/json"
"io"
"time"

"go.uber.org/zap/buffer"
Expand Down Expand Up @@ -331,6 +332,9 @@ type EncoderConfig struct {
// Unlike the other primitive type encoders, EncodeName is optional. The
// zero value falls back to FullNameEncoder.
EncodeName NameEncoder `json:"nameEncoder" yaml:"nameEncoder"`
// Configure the encoder for interface{} type objects
// If not provided, objects are encoded using json.Encoder
NewReflectedEncoder func(io.Writer) ReflectedEncoder `json:"newReflectedEncoder" yaml:"newReflectedEncoder"`
Copy link
Collaborator

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.

Suggested change
NewReflectedEncoder func(io.Writer) ReflectedEncoder `json:"newReflectedEncoder" yaml:"newReflectedEncoder"`
NewReflectedEncoder func(io.Writer) ReflectedEncoder `json:"-" yaml:"-"`

// Configures the field separator used by the console encoder. Defaults
// to tab.
ConsoleSeparator string `json:"consoleSeparator" yaml:"consoleSeparator"`
Expand Down
16 changes: 10 additions & 6 deletions zapcore/json_encoder.go
Expand Up @@ -22,7 +22,7 @@ package zapcore

import (
"encoding/base64"
"encoding/json"
"io"
"math"
"sync"
"time"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you please add a test for the custom reflection encoder case?
We don't want us to introduce a new dependency for that,
so a fake implementation of the function will suffice.

}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should do this in NewJSONEncoder? We can set NewReflectedEncoder in there once if it's nil, and then we don't have to check it on the hot path.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@abhinav
This change was breaking TestJSONEncoderObjectFields test cases in json_encoder_impl_test.go so had to fix it by manually assigning default reflected encoder.
LMK if there are any concerns.

enc.reflectEnc = newReflectedEncoder(enc.reflectBuf)
} else {
enc.reflectBuf.Reset()
}
Expand Down
52 changes: 52 additions & 0 deletions zapcore/reflected_encoder.go
@@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// The underlying data stream is provided by zap during initialization. See EncoderConfig.NewReflectedEncoder
// The underlying data stream is provided by Zap during initialization. See EncoderConfig.NewReflectedEncoder.

type ReflectedEncoder interface {
// Encode encodes and writes to the underlying data stream
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Encode encodes and writes to the underlying data stream
// Encode encodes and writes to the underlying data stream.

Encode(interface{}) error
}

func GetDefaultReflectedEncoder() func(writer io.Writer) ReflectedEncoder {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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:

func defaultReflectedEncoder(w io.Writer) ReflectedEncoder {
  enc := json.NewEncoder(w)
  enc.SetEscapeHTML(false)
  return enc
}

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)
}