-
-
Notifications
You must be signed in to change notification settings - Fork 529
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
Support IsZeroers when checking for isZero #353
Conversation
Do you know if there is any precedent for the |
@arp242 Not really, I also only know about |
One potential issue with this is compatibility: what if people already use a type ( There's a discussion on the Go issue about adding this to stdlib encoders; I didn't read through it all yet, but if that goes through (eventually...) we'll be "stuck" with a different implementation than stdlib; not that stdlib is always right or the best way, but I find it confusing if it's different here especially for struct tags (which are really just "comments"); also see omitzero/omitempty we have now (added before Go's encoders used I think) which I even find confusing myself! So those are my concerns: compatibility and being different from stdlib. Compatibility can be fixed by adding this to v2, stdlib compatibility is harder, but also not strictly a deal-breaker (just unfortunate). I do think this is a problem worth addressing though, either with this solution or with something else. It might be worth to check what other libraries (yaml, hson, whatnot) do with this, if anything. Maybe they have a better idea/implementation? I also need to read through that issue 11939 in full because there might be some good comments there. PS. also note that the |
A common use case for this is time.Time, which will get marshaled otherwise. A struct with private fields set is *not* considered to be "empty"; not sure yet if this is the best behaviour 🤔 It is the easiest to implement though. Fixes #353
Possible alternative: #356 This way you don't need to add an IsZero() method everywhere, and backward compatibility issues are also not as big of a deal. Downside is that an "empty struct" is not an easy question; is a struct "empty" when all fields are the zero value, including private fields? Or only exported fields? Need to think about this for a bit, but overall I think that's a better approach to solve this issue. |
A common use case for this is time.Time, which will get marshaled otherwise. A struct with private fields set is *not* considered to be "empty"; not sure yet if this is the best behaviour 🤔 It is the easiest to implement though. Fixes #353
A common use case for this is time.Time, which will get marshaled otherwise. A struct with private fields set is *not* considered to be "empty"; this is because some structs (such as time.Time) contain only private fields. Fixes #353
Fixed/implemented via #356. |
Expanding the capabilities of
omitzero
by allowing to check if the value implements theisZeroer
interface below:This enables values, such as of the type
time.Time
, to be treated as expected when they have a zero value.