atomic.Time: add MarshalJSON / UnmarshalJSON#208
Open
c-tonneslan wants to merge 1 commit into
Open
Conversation
atomic.Time has no JSON methods, so json.Marshal(t) returns "{}" and the
wrapped time is lost on the wire. Other wrapped types in this package
(Duration, Bool, Pointer) all have these methods. Adding them to Time
fixes uber-go#124.
Implementation mirrors duration.go (delegate to json.Marshal /
json.Unmarshal on the wrapped value). Did this in time_ext.go rather
than the generated time.go so I didn't have to touch the gen-atomic
template just for one type, but the long-term home is probably
re-running gen-atomicwrapper with -json.
Added TestTimeJSON covering MarshalJSON, UnmarshalJSON, and a struct
round-trip that demonstrates the issue from the report.
Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #124.
`atomic.Time` had no JSON methods, so `json.Marshal(t)` returned `"{}"` and any wrapped time silently disappeared on the wire. Other wrapped types in this package (`Duration`, `Bool`, `Pointer`) all have these, so this just fills in the gap.
Implementation matches `duration.go`: delegate to `json.Marshal` / `json.Unmarshal` on the loaded value. I added the methods directly in `time_ext.go` rather than touching the generator template for a single type, but the long-term home is probably re-running `gen-atomicwrapper` with `-json` like the others.
Test plan
```
make test
```
Added `TestTimeJSON` covering `MarshalJSON`, `UnmarshalJSON`, and a struct round-trip that reproduces the issue (so `atomic.Time` inside a struct now serializes as the real time value instead of `{}`).
Changelog entry added under Unreleased / Added.