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

Marshal empty as null #9

Merged

Conversation

openly-engineering
Copy link

The current implementation marshals non-present values as their zero value. When the zero values are read back in, we lose the fidelity of knowing that the value was actually not-present vs. explicitly set to the zero value (the whole purpose of using the optional library). I don't think this should be expected outcome.

This pull request proposes a modification to the UnmarshalJSON and MarshalJSON functions, marshaling JSON null when the value is nil, and unmarshaling JSON null to nil. This seems to be more in-line with what should be expected when writing this out to JSON.

Note: this is a breaking change for dependents that rely on the existing functionality of marshaling to the zero value.

Thanks!

Matt Wielbut added 5 commits December 6, 2018 14:10
This change adds support for marshaling and unmarshaling “null” and reflecting that the value is not present, rather than relying on the “zero” value
@markphelps
Copy link
Owner

@mwielbut thanks for the PR! I agree this makes sense. I'll create a new minor release and note that it's a breaking change.

Would you mind removing the go mod files from the PR? I haven't yet switched the project over from dep but am planning to soon.

@mwielbut
Copy link
Contributor

mwielbut commented Dec 7, 2018

Awesome! Just removed the go.mod and go.sum files. We needed them to make it work locally with modules but it should be fine once it's in your release.

@markphelps markphelps merged commit 0f79a1a into markphelps:master Dec 7, 2018
@markphelps
Copy link
Owner

Cheers! Thanks again!

@mwielbut mwielbut deleted the marshal-empty-as-null branch December 7, 2018 16:05
mwielbut pushed a commit to openly-engineering/optional that referenced this pull request Dec 7, 2018
* Support “null”

This change adds support for marshaling and unmarshaling “null” and reflecting that the value is not present, rather than relying on the “zero” value

* Fix to specify {{ .VariableName }}

* Add support for go modules

* Fix go mod path

* Update golden test

* Remove go.mod per pull request
markphelps added a commit that referenced this pull request May 4, 2021
* Marshal empty as null (#9)

* Support “null”

This change adds support for marshaling and unmarshaling “null” and reflecting that the value is not present, rather than relying on the “zero” value

* Fix to specify {{ .VariableName }}

* Add support for go modules

* Fix go mod path

* Update golden test

* Remove go.mod per pull request

* Add example test

* Update README.md

* MustGet: Add ability to get value and panic if not present

Co-authored-by: Openly, Inc <openlyinsured@users.noreply.github.com>
Co-authored-by: Mateusz Wielbut <mwielbut@users.noreply.github.com>
Co-authored-by: Matt Wielbut <matt@openly.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants