Skip to content
This repository was archived by the owner on Dec 27, 2022. It is now read-only.

Omitempty marshalling #27

Open
dillon-giacoppo opened this issue Mar 7, 2018 · 0 comments
Open

Omitempty marshalling #27

dillon-giacoppo opened this issue Mar 7, 2018 · 0 comments

Comments

@dillon-giacoppo
Copy link

dillon-giacoppo commented Mar 7, 2018

Quick note on using omit empty in the structs for requests.

The "omitempty" option specifies that the field should be omitted from the encoding if the field has an empty value, defined as false, 0, a nil pointer, a nil interface value, and any empty array, slice, map, or string.

This makes it impossible to zero a value in a request as they are always excluded meaning a boolean can never be set to false or strings never set to empty. (This is also true for the new branch).

I have encountered this issue in writing a terraform provider as often time fields that are empty do need to be omitted as they shouldn't update remotely, requests should only include the relevant fields but there are times when a property needs to be zeroed. If the omit empty is removed then every single field will need to be set to the correct value which is verbose and impractical.

As far as I know the only fix for this is to store all the struct fields as pointers which allows for a distinction between null and zero.

Example is:
https://github.com/google/go-github/blob/master/github/repos.go

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant