-
Notifications
You must be signed in to change notification settings - Fork 193
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
omitisempty support #326
omitisempty support #326
Conversation
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.
The only thing that annoys me is that omitisempty
is very hard to read - and easy to mistake for omitempty
.
I don't know if iszero
would make it clearer, and then use the IsZero() bool
signature as seen on time.Time
?
However, I can surely live with it. Just wondering if that makes sense to anyone else.
I'm not opposed to renaming the tag option to |
@philhofer Do you see that as a valuable change? |
Yeah, I'm in agreement that |
Thanks for the feedback. I went to start making these changes, a couple questions came up:
This way they are just two totally separate features and people can mix and match as they want to.
|
Agree. IMO if
Forcing Does that sound reasonable to you? Please add tests for |
@klauspost Yup, agreed on all points and thanks. I'll need a few days or so but I'll hammer this out and get the changes pushed back here. |
This was simpler than I thought. See #334, which replaces this PR. |
This PR adds support for an
omitisempty
tag, which works likeomitempty
but emits additional checks for emptiness using the interface{ IsEmpty() bool }
.This should have no effect on code generated without
omitisempty
. But whenomitisempty
is included on a field, the result is to perform the existing emptiness check on that field plus|| checkIsEmptyf(x)
, which calls IsEmpty() via interface if possible. This also emits the definition of checkIsEmpty (3 lines) once at the top of each MarshalMsg or EncodeMsg method as appropriate.I've also include some test coverage (could use more work, but I think what's here is pretty good proof this is a viable feature and doesn't break anything).
Some additional notes on the implementation:
checkIsEmptyIntf
once per function, but ended up being easy since encodeGen and marshalGen each provide a good place to manage this state.