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

json: AppendTimeLayout should add field separators #799

Merged
merged 1 commit into from
Mar 14, 2020

Conversation

abhinav
Copy link
Collaborator

@abhinav abhinav commented Mar 12, 2020

In #786, we added support for improving the performance of encoding time
by allowing encoder implementations to implement AppendTimeLayout.

If the encoder implements, AppendTimeLayout(time.Time, string), some
of the time encoders shipped with Zap will use that method, which in
turn can make use of Buffer.AppendTime.

That change inadvertently broke the JSON output for arrays of time that
make use of that functionality.

Compare the old version of AppendTimeLayout with AppendString (both
operations append a string to the array).

func (enc *jsonEncoder) AppendString(val string) {
  enc.addElementSeparator()
  enc.buf.AppendByte('"')
  // ...
}

func (enc *jsonEncoder) AppendTimeLayout(time time.Time, layout string) {
  enc.buf.AppendByte('"')
  // ...
}

Without the enc.addElementSeparator call, AppendTimeLayout does not
include the , separator between array elements, instead yielding the
following.

["2001-12-19T00:00:00Z""2002-12-18T00:00:00Z""2003-12-17T00:00:00Z"]

Fixes #798

In #786, we added support for improving the performance of encoding time
by allowing encoder implementations to implement AppendTimeLayout.

If the encoder implements, `AppendTimeLayout(time.Time, string)`, some
of the time encoders shipped with Zap will use that method, which in
turn can make use of `Buffer.AppendTime`.

That change inadvertently broke the JSON output for arrays of time that
make use of that functionality.

Compare the old version of AppendTimeLayout with AppendString (both
operations append a string to the array).

    func (enc *jsonEncoder) AppendString(val string) {
      enc.addElementSeparator()
      enc.buf.AppendByte('"')
      // ...
    }

    func (enc *jsonEncoder) AppendTimeLayout(time time.Time, layout string) {
      enc.buf.AppendByte('"')
      // ...
    }

Without the `enc.addElementSeparator` call, `AppendTimeLayout` does not
include the `,` separator between array elements, instead yielding the
following.

    ["2001-12-19T00:00:00Z""2002-12-18T00:00:00Z""2003-12-17T00:00:00Z"]

Fixes #798
@codecov
Copy link

codecov bot commented Mar 12, 2020

Codecov Report

Merging #799 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #799   +/-   ##
=======================================
  Coverage   98.31%   98.31%           
=======================================
  Files          43       43           
  Lines        2310     2311    +1     
=======================================
+ Hits         2271     2272    +1     
  Misses         32       32           
  Partials        7        7           
Impacted Files Coverage Δ
zapcore/json_encoder.go 100.00% <100.00%> (ø)

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 9646482...8ba5fb9. Read the comment docs.

Copy link
Contributor

@mh-park mh-park left a comment

Choose a reason for hiding this comment

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

Looks good :)

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.

Thanks for fixing my bug and adding tests to make sure this is caught in future!

@abhinav abhinav merged commit c2633d6 into master Mar 14, 2020
@abhinav abhinav deleted the abg/fix-time-array branch March 14, 2020 16:37
cgxxv pushed a commit to cgxxv/zap that referenced this pull request Mar 25, 2022
In uber-go#786, we added support for improving the performance of encoding time
by allowing encoder implementations to implement AppendTimeLayout.

If the encoder implements, `AppendTimeLayout(time.Time, string)`, some
of the time encoders shipped with Zap will use that method, which in
turn can make use of `Buffer.AppendTime`.

That change inadvertently broke the JSON output for arrays of time that
make use of that functionality.

Compare the old version of AppendTimeLayout with AppendString (both
operations append a string to the array).

    func (enc *jsonEncoder) AppendString(val string) {
      enc.addElementSeparator()
      enc.buf.AppendByte('"')
      // ...
    }

    func (enc *jsonEncoder) AppendTimeLayout(time time.Time, layout string) {
      enc.buf.AppendByte('"')
      // ...
    }

Without the `enc.addElementSeparator` call, `AppendTimeLayout` does not
include the `,` separator between array elements, instead yielding the
following.

    ["2001-12-19T00:00:00Z""2002-12-18T00:00:00Z""2003-12-17T00:00:00Z"]

Fixes uber-go#798
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.

zap.Times with development logger generates invalid JSON
3 participants