Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

MapObjectEncoder: Empty arrays should not be nil #614

Merged
merged 2 commits into from
Jul 31, 2018
Merged

Conversation

mh-park
Copy link
Contributor

@mh-park mh-park commented Jul 30, 2018

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.

@mh-park mh-park requested a review from abhinav July 30, 2018 21:00
@CLAassistant
Copy link

CLAassistant commented Jul 30, 2018

CLA assistant check
All committers have signed the CLA.

@codecov
Copy link

codecov bot commented Jul 30, 2018

Codecov Report

Merging #614 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #614   +/-   ##
=======================================
  Coverage   97.37%   97.37%           
=======================================
  Files          40       40           
  Lines        2095     2095           
=======================================
  Hits         2040     2040           
  Misses         47       47           
  Partials        8        8
Impacted Files Coverage Δ
zapcore/memory_encoder.go 98.66% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 55d3c26...9e0ecf5. Read the comment docs.

@mh-park mh-park changed the title Replace null with empty slices for MapObjectEncoder MapObjectEncoder: Empty arrays should not be nil Jul 30, 2018
@@ -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{}{}
Copy link
Collaborator

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)}
Copy link
Collaborator

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.

Copy link
Contributor Author

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

Derp, you're right.

@abhinav abhinav requested a review from akshayjshah July 30, 2018 22:13
Copy link
Contributor

@akshayjshah akshayjshah left a 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!

@mh-park mh-park merged commit 1f8c4ca into master Jul 31, 2018
@mh-park mh-park deleted the mpark/empty-array branch July 31, 2018 22:11
cgxxv pushed a commit to cgxxv/zap that referenced this pull request Mar 25, 2022
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.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants