+
Skip to content

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

Merged
merged 1 commit into from
Mar 21, 2025

Conversation

daenney
Copy link
Contributor

@daenney daenney commented Mar 17, 2025

Introduce an OmitEmpty option.

@daenney
Copy link
Contributor Author

daenney commented Mar 17, 2025

I am using repr as part of debugging some issues, and in the project I'm working on there's a semantic difference between nil and empty maps and slices. Nil meaning their absence, whereas non-nil-but-zero meaning present but without any members.

This was hard to see with OmitEmpty, as it eliminates zero-length maps and slices. So this PR adds two toggles for those so that those can be retained without that causing empty string and nil maps etc. to also show up.

@alecthomas
Copy link
Owner

alecthomas commented Mar 21, 2025

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 repr.OmitEmpty() and repr.OmitZero() mirror the new semantics of the stdlib JSON package?

@daenney
Copy link
Contributor Author

daenney commented Mar 21, 2025

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 reflect.Value.IsZero()?

@alecthomas
Copy link
Owner

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.
@daenney daenney force-pushed the omit-empty-not-nil-slice-map branch from 49b4a4e to fe1114d Compare March 21, 2025 16:33
@daenney daenney changed the title feat: Allow retaining zero length maps and slices feat: OmitZero and OmitEmpty Mar 21, 2025
@daenney
Copy link
Contributor Author

daenney commented Mar 21, 2025

I think that should do it.

Copy link
Owner

@alecthomas alecthomas left a 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}}
Copy link
Owner

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah probably 😅.

Copy link
Contributor Author

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?

@alecthomas alecthomas merged commit 17a3fb5 into alecthomas:master Mar 21, 2025
2 checks passed
@daenney daenney deleted the omit-empty-not-nil-slice-map branch March 21, 2025 21:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载