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

Allow representations of nillable primitive field values #758

Merged
merged 5 commits into from
Nov 13, 2019

Conversation

jbizzle
Copy link
Contributor

@jbizzle jbizzle commented Nov 7, 2019

Fixes #753 by exporting util functions to handle nillable primitives. The version here uses Reflect() under the hood to encode nil, and makes corresponding changes in json_encoder.go to efficiently handle encoding nil field values.

@CLAassistant
Copy link

CLAassistant commented Nov 7, 2019

CLA assistant check
All committers have signed the CLA.

@codecov
Copy link

codecov bot commented Nov 7, 2019

Codecov Report

Merging #758 into master will increase coverage by 0.1%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master     #758     +/-   ##
=========================================
+ Coverage   98.09%   98.19%   +0.1%     
=========================================
  Files          42       42             
  Lines        2150     2269    +119     
=========================================
+ Hits         2109     2228    +119     
  Misses         33       33             
  Partials        8        8
Impacted Files Coverage Δ
zapcore/encoder.go 84.61% <ø> (ø) ⬆️
field.go 100% <100%> (ø) ⬆️
zapcore/json_encoder.go 100% <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 a6015e1...b4126bf. Read the comment docs.

@jbizzle jbizzle changed the title Nil1 Allow representations of nillable primitive field values Nov 7, 2019
buffer/buffer.go Outdated
// AppendBytes appends a byte slice to the Buffer.
func (b *Buffer) AppendBytes(bs []byte) {
b.bs = append(b.bs, bs...)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why create this method instead of just using Write?

Since this package and type is public, we'd prefer to not introduce new
methods if possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just figured it helped avoid checking for errors via Write() which were known-impossible, but not worth disrupting the larger picture just for that (and in a previous, unsubmitted version, I was calling it from more than one site, so it was a mild ergonomic improvement).

// AddReflected uses reflection to serialize arbitrary objects, so it's slow
// and allocation-heavy.
// AddReflected uses reflection to serialize arbitrary objects, so it can be
// slow and allocation-heavy.
Copy link
Collaborator

Choose a reason for hiding this comment

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

heh nice detail

enc.addKey(key)
_, err = enc.buf.Write(enc.reflectBuf.Bytes())
return err
enc.buf.AppendBytes(valueBytes)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use of Write here should be fine

Copy link
Collaborator

@abhinav abhinav left a comment

Choose a reason for hiding this comment

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

LGTM besides lack of AppendReflected support.

CC @prashantv

// Only invoke the standard JSON encoder if there is actually something to
// encode; otherwise write JSON null literal directly. For now, we only
// apply this optimization here, but not in AppendReflected. We could apply
// it there as well if needed (some discussion in
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since we're treating this as an optimization, I'm in favor of doin it to AppendReflected too.
It already supports nil entries inside slices. https://play.golang.org/p/bZb6t58qhpy

Copy link
Collaborator

Choose a reason for hiding this comment

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

+1

Copy link
Collaborator

@prashantv prashantv left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the improvement!

// Only invoke the standard JSON encoder if there is actually something to
// encode; otherwise write JSON null literal directly. For now, we only
// apply this optimization here, but not in AppendReflected. We could apply
// it there as well if needed (some discussion in
Copy link
Collaborator

Choose a reason for hiding this comment

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

+1

Copy link
Collaborator

@abhinav abhinav left a comment

Choose a reason for hiding this comment

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

Thanks!

@abhinav abhinav merged commit 5637143 into uber-go:master Nov 13, 2019
abhinav added a commit that referenced this pull request Nov 13, 2019
This prepares release v1.13.0 with #758 in it.
@abhinav abhinav mentioned this pull request Nov 13, 2019
abhinav added a commit that referenced this pull request Nov 13, 2019
This prepares release v1.13.0 with #758 in it.
cgxxv pushed a commit to cgxxv/zap that referenced this pull request Mar 25, 2022
This adds support for building fields from pointers to primitive types
we already accept.

These new field constructors accept and handle `nil` values in addition
to the non-nil primitive values.

For nil values, we fall back to the behavior of `zap.Reflect(..)`,
which we optimize for in the JSON encoder so that we don't use
`encoding/json` just to build the string `"null"`.

Resolves uber-go#753
cgxxv pushed a commit to cgxxv/zap that referenced this pull request Mar 25, 2022
This prepares release v1.13.0 with uber-go#758 in it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Pointer to T Field constructors.
4 participants