-
Notifications
You must be signed in to change notification settings - Fork 2
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
add omitempty option #2
Conversation
i -= len(m.XXX_unrecognized) | ||
copy(dAtA[i:], m.XXX_unrecognized) | ||
} | ||
if !m.InnerOmitEmpty.IsEmpty() { |
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.
This line is pretty much what the option does.
Nice idea! Out of curiosity, why require an |
} | ||
return nil | ||
} | ||
func (this *OmitEmpty) Equal(that interface{}) bool { |
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.
nit: if gogoproto is generating a method called Equal
, would it be more consistent to rename IsEmpty
to Empty
?
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.
Done.
} | ||
return nil | ||
} | ||
func (this *OmitEmpty) Equal(that interface{}) bool { |
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.
Should we follow the pattern set by Equal
of exposing a gogoproto.empty
option to generate an IsEmpty
/Empty
method?
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.
I'm not supercomfortable with working in this repo, I'd rather not complicate things here. It will be trivial to write it manually wherever we want to use this.
I figured it could in some cases be faster than a full struct compare. For example for the timestamp I think it's enough to check the WallTime. Also it would allow non-strucr casttypes (which is the case for KVNemesisSeq in test builds). |
This change adds a `gogoproto.omitempty` option that can be used (in conjunction with `(gogoproto.nullable) = false`) for non-nullable message fields. When a message field is not nullable, its tag is always encoded - even if all message fields are unset. The `omitempty` option changes the marshalling code to check an `Empty()` method on that message, which must be defined by the user. The result is that when the message is empty, the encoding matches the one we would get if the field was nullable and the pointer is unset.
You claim in the commit msg:
But i'm not sure that's always true. Consider the Do you understand why the empty fields of the MVCCValueheader are not encoded in the |
Good question. It looks like the test disables that metamorphic constant: |
I'd like to merge this soon to clean up the hacks in crdb. |
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 🚢
TFTR! |
This change adds a
gogoproto.omitempty
option that can be used (in conjunction with(gogoproto.nullable) = false
) for non-nullable message fields.When a message field is not nullable, its tag is always encoded - even if all message fields are unset. The
omitempty
option changes the marshalling code to check anIsEmpty()
method on that message, which can be defined by the user. The result is that when the message is empty, the encoding matches the one we would get if the field was nullable and the pointer is unset.