tinylib / msgp

A Go code generator for MessagePack / msgpack.org[Go]
MIT License
1.77k stars 189 forks source link

omitisempty support #325

Closed bradleypeabody closed 1 year ago

bradleypeabody commented 1 year ago

This PR adds support for an omitisempty tag, which works like omitempty but emits additional checks for emptiness using the interface { IsEmpty() bool }.

This should have no effect on code generated without omitisempty. But when omitisempty 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: