Skip to content
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(utils): add IsZeroValue #2760

Closed
wants to merge 7 commits into from

Conversation

sixcolors
Copy link
Member

@sixcolors sixcolors commented Dec 12, 2023

Description

Add utils function to support change to #2751

Adds IsZeroValue function to utils pkg. Which reports whether x is the zero value for its type. For basic types and error, it uses a fast path without reflection. For other types, it uses reflection which is slower.

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • If new dependencies exist, I have checked that they are really necessary and agreed with the maintainers/community (we want to have as few dependencies as possible)
  • I tried to make my code as fast as possible with as few allocations as possible
  • For new code I have written benchmarks so that they can be analyzed and improved

@sixcolors
Copy link
Member Author

basic types based on https://go.dev/tour/basics/11

utils/zero.go Outdated Show resolved Hide resolved
@gaby
Copy link
Member

gaby commented Dec 13, 2023

@sixcolors Probably worth comparing your implementation with this one: https://github.com/duke-git/lancet/blob/main/validator/validator.go#L390

Unit-Tests here: https://github.com/duke-git/lancet/blob/main/validator/validator_test.go#L413

Lancet is used by over +1k repos, so it's worth comparing.

@sixcolors sixcolors marked this pull request as draft December 13, 2023 05:50
@sixcolors
Copy link
Member Author

Set to draft while considering if related change is appropriate or not

@sixcolors
Copy link
Member Author

sixcolors commented Dec 13, 2023

As @nickajacks1 pointed out #2751 (comment) a IsZeroValue check for ContextKeys would cause issues for some types that could be valid keys. So I’m closing this PR.

@sixcolors sixcolors closed this Dec 13, 2023
@sixcolors sixcolors deleted the feat-add-zero-value branch December 13, 2023 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants