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
serialise empty values as null
(JSON) or empty element (XML) instead of the zero value of the type
#3
base: main
Are you sure you want to change the base?
Conversation
…d of the zero value of the type
unmarshal null into empty
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for contributing this fix!
@@ -277,7 +280,7 @@ func Example_jsonMarshalPresent() { | |||
// } | |||
} | |||
|
|||
func Example_jsonUnmarshalEmpty() { | |||
func TestExample_jsonUnmarshalEmpty(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we keep this as an example? Examples already assert on the output.
@@ -335,6 +338,64 @@ func Example_jsonUnmarshalEmpty() { | |||
// Uint64: false | |||
// Uint: false | |||
// Uintptr: false | |||
|
|||
t.Run("unmarshal null values to empty", func(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you put this in a new example? Not a test please. The examples show up in docs and so help people understand how the library works.
@@ -82,17 +82,28 @@ func (o Optional[T]) ElseZero() (value T) { | |||
// representation of the zero value of the type wrapped if there is no value | |||
// wrapped by this optional. | |||
func (o Optional[T]) String() string { | |||
return fmt.Sprintf("%v", o.ElseZero()) | |||
if v, ok := o.Get(); ok { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment for the function needs updating as it says if no value is set that the zero value of the type is returned, which is no longer the case.
See #2 for proposal and justification behind this change.