markphelps / optional

Optional is a library of optional Go types
https://godoc.org/github.com/markphelps/optional
MIT License
209 stars 19 forks source link

Marshal empty as null #9

Closed openly-engineering closed 5 years ago

openly-engineering commented 5 years ago

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!

markphelps commented 5 years ago

@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 commented 5 years ago

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 commented 5 years ago

Cheers! Thanks again!