Skip to content

Conversation

@notnoop
Copy link

@notnoop notnoop commented Apr 30, 2019

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 *bool and 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 }, then nil and &{""} 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 :

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

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.

@notnoop notnoop requested review from jefferai and preetapan April 30, 2019 21:57
@notnoop notnoop force-pushed the b-nil-omit-fields branch from 4ab3b66 to d05b984 Compare May 2, 2019 00:33
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
@notnoop notnoop force-pushed the b-nil-omit-fields branch from d05b984 to ce224ea Compare May 2, 2019 01:45
@banks banks requested a review from mkeeler May 2, 2019 10:12
Copy link
Member

@mkeeler mkeeler left a 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.

@notnoop
Copy link
Author

notnoop commented May 2, 2019

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 *bool and 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 }, then nil and &{""} 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.

@notnoop notnoop merged commit ad60660 into master May 2, 2019
notnoop pushed a commit to hashicorp/nomad that referenced this pull request May 2, 2019
thaJeztah added a commit to thaJeztah/libnetwork that referenced this pull request Aug 26, 2019
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>
thaJeztah added a commit to thaJeztah/libnetwork that referenced this pull request Aug 26, 2019
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>
thaJeztah added a commit to thaJeztah/libnetwork that referenced this pull request Aug 26, 2019
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>
thaJeztah added a commit to thaJeztah/libnetwork that referenced this pull request Feb 26, 2020
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>
thaJeztah added a commit to thaJeztah/libnetwork that referenced this pull request May 11, 2021
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>
kffc-eng-us added a commit to kffc-eng-us/sysm-parking-ui that referenced this pull request Dec 2, 2025
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.

"omitempty" fails on pointers to bools

2 participants