8000
Skip to content

Allow representations of nillable primitive field values#758

Merged
abhinav merged 5 commits intouber-go:masterfrom
jbizzle:nil1
Nov 13, 2019
Merged

Allow representations of nillable primitive field values#758
abhinav merged 5 commits intouber-go:masterfrom
jbizzle:nil1

Conversation

@jbizzle
Copy link
Copy Markdown
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
Copy Markdown
CLAassistant commented Nov 7, 2019

CLA assistant check
All committers have signed the CLA.

@codecov
Copy link
Copy Markdown
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
Comment thread 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
Copy Markdown
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
Copy Markdown
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).

Comment thread zapcore/encoder.go
// 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
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

heh nice detail

Comment thread zapcore/json_encoder.go Outdated
enc.addKey(key)
_, err = enc.buf.Write(enc.reflectBuf.Bytes())
return err
enc.buf.AppendBytes(valueBytes)
Copy link
Copy Markdown
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
Copy Markdown
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

Comment thread zapcore/json_encoder.go Outdated
// 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
Copy Markdown
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
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

+1

Copy link
Copy Markdown
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!

Comment thread zapcore/json_encoder.go Outdated
// 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
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

+1

Copy link
Copy Markdown
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.
RenovZ pushed a commit to RenovZ/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
RenovZ pushed a commit to RenovZ/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

0