feat: honor the omitzero struct-tag option#392
Open
gdamore wants to merge 1 commit into
Open
Conversation
encoding/json gained the `omitzero` field option in Go 1.24: a field is omitted when it holds the zero value for its type, as determined by an `IsZero() bool` method if present, otherwise by reflect.Value.IsZero(). The tag parser already recognizes the option, but it was silently ignored, so a zero-valued field tagged `,omitzero` was always encoded. Honor it in field.Omit, independently of `omitempty`. A new isZeroValue helper mirrors encoding/json's semantics: a type's own IsZero takes precedence, a nil pointer/interface is treated as zero up front (so a typed-nil whose type carries an IsZero method is not dereferenced), and otherwise reflect.Value.IsZero() decides. Unlike omitempty's isEmptyValue, it does not treat a non-nil empty slice, map, or string as omittable. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
encoding/jsongained theomitzerofield option in Go 1.24: a field is omitted when it holds the zero value for its type, as determined by anIsZero() boolmethod if present, otherwise byreflect.Value.IsZero(). msgpack's tag parser already recognizes the option (tag.HasOption("omitzero")), but it was silently ignored, so a zero-valued field tagged,omitzerowas always encoded.This honors
omitzeroinfield.Omit, independently ofomitempty.Details
isZeroValuehelper mirrorsencoding/json's omitzero semantics:IsZero()takes precedence;IsZeromethod (e.g.(*time.Time)(nil)) is not dereferenced — the same guardencoding/jsonuses;reflect.Value.IsZero()decides.omitempty'sisEmptyValue, it does not treat a non-nil empty slice, map, or string as omittable —omitemptyandomitzeroare intentionally distinct, as inencoding/json.SetCustomStructTag("json")) too, since the option is parsed generically.Test
TestOmitZerocovers int/string/bool zero values, a type with a value-receiverIsZero, and a nil*time.Time(exercising the nil-pointer guard). It fails onv5without this change and passes with it.Notes
Related to #390, which discusses aligning omit semantics with
encoding/jsonand notesomitzeroas the path Go took. This PR adds theomitzerooption specifically; it does not change existingomitemptybehavior.