-
Notifications
You must be signed in to change notification settings - Fork 19
codec: do not dereference pointers/interfaces for omitempty support. #8
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
Conversation
This cherry-picks empty value checking changes in 006e153 and 66dd47f . This is in line with the standard set by encoding/json i.e. if omitempty=true, pointers/interfaces are omitted iff nil. Previously, I "mistakenly" thought that encoding/json dereferenced pointers/interfaces when checking if empty. Fixes ugorji#67
mkeeler
left a comment
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.
LGTM
For others who may look and are wondering what this changes I have dug in and figured it out.
Without this change *bool pointer values that were non-nil and pointed to a false value were being omitted from the encoding. With this change it allows it to only omit the value when the pointer is nil and if not will always encode the bool value.
|
Thanks! Sorry I should have elaborated in the description a bit more -- Without this change, omitempty used to treat pointer to empty values as empty (e.g. if a field is This fixes the issue to be inline with json/encoding - where pointer is treated as empty and omitted only if it's a nil pointer. |
To pick up the fix in hashicorp/go-msgpack#8 .
full diff: hashicorp/go-msgpack@71c2886...v0.5.5 - hashicorp/go-msgpack#3 Add go.mod - hashicorp/go-msgpack#7 Do not attempt to set unsettable types - hashicorp/go-msgpack#8 codec: do not dereference pointers/interfaces for omitempty support - backport of hashicorp/go-msgpack@006e153 - backport of hashicorp/go-msgpack@006e153 - fixes ugorji/go#67 "omitempty" fails on pointers to bools Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
full diff: hashicorp/go-msgpack@71c2886...v0.5.5 - hashicorp/go-msgpack#3 Add go.mod - hashicorp/go-msgpack#7 Do not attempt to set unsettable types - hashicorp/go-msgpack#8 codec: do not dereference pointers/interfaces for omitempty support - backport of hashicorp/go-msgpack@006e153 - backport of hashicorp/go-msgpack@006e153 - fixes ugorji/go#67 "omitempty" fails on pointers to bools Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
full diff: hashicorp/go-msgpack@71c2886...v0.5.5 - hashicorp/go-msgpack#3 Add go.mod - hashicorp/go-msgpack#7 Do not attempt to set unsettable types - hashicorp/go-msgpack#8 codec: do not dereference pointers/interfaces for omitempty support - backport of hashicorp/go-msgpack@006e153 - backport of hashicorp/go-msgpack@006e153 - fixes ugorji/go#67 "omitempty" fails on pointers to bools Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
full diff: hashicorp/go-msgpack@71c2886...v0.5.5 - hashicorp/go-msgpack#3 Add go.mod - hashicorp/go-msgpack#7 Do not attempt to set unsettable types - hashicorp/go-msgpack#8 codec: do not dereference pointers/interfaces for omitempty support - backport of hashicorp/go-msgpack@006e153 - backport of hashicorp/go-msgpack@006e153 - fixes ugorji/go#67 "omitempty" fails on pointers to bools Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
full diff: hashicorp/go-msgpack@71c2886...v0.5.5 - hashicorp/go-msgpack#3 Add go.mod - hashicorp/go-msgpack#7 Do not attempt to set unsettable types - hashicorp/go-msgpack#8 codec: do not dereference pointers/interfaces for omitempty support - backport of hashicorp/go-msgpack@006e153 - backport of hashicorp/go-msgpack@006e153 - fixes ugorji/go#67 "omitempty" fails on pointers to bools Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This backports empty value checking changes from https://github.com/ugorji/go that are found in 006e153 and 66dd47f .
Without this change, omitempty treats pointer to empty values as empty (e.g. if a field is
*booland value is a reference to false), and treating a struct pointer is empty if all elements of struct as empty (e.g. if a field is of type*struct { A string }, thenniland&{""}values would be treated as empty and omitted).This fixes the issue to be inline with json/encoding - where pointer is treated as empty and omitted only if it's a nil pointer.
Original Comment description from 66dd47f :
I've also added a test for the case that wasn't in original repo. The style of repo tests is a bit odd, and I conformed with it rather than establish a new test style.