-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
MapObjectEncoder: Empty arrays should not be nil #614
Conversation
Codecov Report
@@ Coverage Diff @@
## master #614 +/- ##
=======================================
Coverage 97.37% 97.37%
=======================================
Files 40 40
Lines 2095 2095
=======================================
Hits 2040 2040
Misses 47 47
Partials 8 8
Continue to review full report at Codecov.
|
zapcore/memory_encoder.go
Outdated
@@ -45,6 +45,9 @@ func NewMapObjectEncoder() *MapObjectEncoder { | |||
func (m *MapObjectEncoder) AddArray(key string, v ArrayMarshaler) error { | |||
arr := &sliceArrayEncoder{} | |||
err := v.MarshalLogArray(arr) | |||
if arr.elems == nil { | |||
arr.elems = []interface{}{} |
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.
I would make this the default value of arr.elems
since it'll be appended to anyway.
arr := &sliceArrayEncoder{elems: make([]interface{})}
@@ -43,7 +43,7 @@ func NewMapObjectEncoder() *MapObjectEncoder { | |||
|
|||
// AddArray implements ObjectEncoder. | |||
func (m *MapObjectEncoder) AddArray(key string, v ArrayMarshaler) error { | |||
arr := &sliceArrayEncoder{} | |||
arr := &sliceArrayEncoder{elems: make([]interface{}, 0)} |
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.
0 unnecessary in make(..)
because it's the default.
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.
I initially tried it without the 0 and zapcore/memory_encoder.go:46:39: missing len argument to make([]interface {})
popped up
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.
Derp, you're right.
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.
Looks great - thanks for the bugfix!
This change fixes a bug in MapObjectEncoder.AddArray where an empty slice would log nil instead of an empty array. Logging an empty array makes the MapObjectEncoder's behavior correctly reflect the JSON encoder's.
This change fixes a bug in MapObjectEncoder.AddArray where an empty slice would log nil instead of an empty array. Logging an empty array makes the MapObjectEncoder's behavior correctly reflect the JSON encoder's.