-
-
Notifications
You must be signed in to change notification settings - Fork 16
feat: OmitZero and OmitEmpty #30
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
feat: OmitZero and OmitEmpty #30
Conversation
I am using This was hard to see with |
Apologies for the delay, I've been pondering this. I don't think I like the proliferation of options specific to this. I'm also okay slightly breaking backwards compat while it's still 0.x. WDYT about having |
Works for me. I just cobbled this together while I was debugging some things, happy to change it. I imagine we can then piggy-back of the stdlib trick to check for an interface https://cs.opensource.google/go/go/+/master:src/encoding/json/encode.go;drc=97571f36103b045a7e9c15a92e9a35ab95fa6be5;l=1067 before checking for |
Agreed, I think it would be good to have the same semantics |
This splits OmitEmpty into OmitEmpty and OmitZero. OmitEmpty will omit anything that is zero as well as a slice and map of length 0. OmitZero only omits the zero type, either by checking the return value of the IsZero method or if that's not implemented by reflect.Value.IsZero. This by default retains the empty map and slice, since initialised but 0-length maps and slices are not zero.
49b4a4e
to
fe1114d
Compare
I think that should do it. |
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.
Nice, thanks!
B bool | ||
ZC zeroTest | ||
C zeroTest | ||
}{[]string{}, []string{"a", "b"}, map[string]string{}, map[string]string{"a": "b"}, 0, 1, "", "a", false, true, zeroTest{i: 100}, zeroTest{i: 10}} |
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.
For future reference, I think this would be easier to read with a concrete type declared in the function.
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.
Yeah probably 😅.
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 think the only issue with that is I can't declare functions on those, can I?
Introduce an OmitEmpty option.