Fix omitempty on aliased types#377
Merged
klauspost merged 2 commits intotinylib:masterfrom Oct 29, 2024
Merged
Conversation
Instead of doing return, insert an if block to skip emitting zero fields. This also allows the check to be inserted at any level. Fixes tinylib#376 Emitted code: ``` // MarshalMsg implements msgp.Marshaler func (z TypeSample) MarshalMsg(b []byte) (o []byte, err error) { o = msgp.Require(b, z.Msgsize()) // check for omitted fields zb0001Len := uint32(2) var zb0001Mask uint8 /* 2 bits */ _ = zb0001Mask if z.K == 0 { zb0001Len-- zb0001Mask |= 0x1 } if z.V == 0 { zb0001Len-- zb0001Mask |= 0x2 } // variable map header, size zb0001Len o = append(o, 0x80|uint8(zb0001Len)) // Skip if no fields are to be emitted if zb0001Len != 0 { if (zb0001Mask & 0x1) == 0 { // if not omitted // string "k" o = append(o, 0xa1, 0x6b) o = msgp.AppendUint32(o, z.K) } if (zb0001Mask & 0x2) == 0 { // if not omitted // string "v" o = append(o, 0xa1, 0x76) o = msgp.AppendUint32(o, z.V) } } return } ``` If only 1 field, the check is omitted (and there is similar behavior on clearomitted).
philhofer
approved these changes
Oct 29, 2024
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.
Instead of doing return, insert an
ifblock to skip emitting zero fields.This also allows the check to be inserted at any level.
Fixes #376
Emitted code:
If only 1 field, the check is omitted (and there is similar behavior on clearomitted).