-
-
Notifications
You must be signed in to change notification settings - Fork 185
Add support for required
and nonzero
yaml fields
#728
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
base: master
Are you sure you want to change the base?
Conversation
90fa810
to
baa4a17
Compare
Codecov ReportAttention: Patch coverage is
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## master #728 +/- ##
==========================================
+ Coverage 77.97% 77.98% +0.01%
==========================================
Files 22 23 +1
Lines 8108 8139 +31
==========================================
+ Hits 6322 6347 +25
- Misses 1370 1374 +4
- Partials 416 418 +2 🚀 New features to boost your workflow:
|
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.
Pull Request Overview
This PR introduces support for two new YAML tag options, "required" and "nonzero", to enforce that fields must be provided and must not have a zero value during decoding/encoding.
- Added IsRequired and IsNonZero flags to struct field metadata.
- Extended error handling with new error types (RequiredFieldError and ZeroFieldError) and their corresponding error messages.
- Updated encoder/decoder logic and test cases to validate the new behavior.
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
File | Description |
---|---|
zero.go | Introduces helper isZeroValue for checking zero values. |
struct.go | Adds IsRequired and IsNonZero options for struct fields. |
internal/errors/error.go | Defines new error types and functions for required/zero fields. |
encode.go | Incorporates nonzero check before omitempty logic in encoding. |
encode_test.go & decode_test.go | Adds tests verifying proper error generation on violations. |
decode.go | Validates required and nonzero constraints during decoding. |
Comments suppressed due to low confidence (1)
decode.go:1406
- [nitpick] When a field is missing and both 'required' and 'nonzero' flags are set, the current implementation overwrites the required error with the nonzero error. Consider explicitly prioritizing one error message to avoid potential ambiguity.
if !exists {
Co-authored-by: Lisa Ugray <lisa.ugray@gmail.com>
Co-authored-by: Lisa Ugray <lisa.ugray@gmail.com>
Co-authored-by: Lisa Ugray <lisa.ugray@gmail.com>
b8d4def
to
246452a
Compare
We updated this branch to differentiate nonempty and nonzero as omitzero was added alongside omitempty since we've opened this PR. |
This adds support for two new options in the yaml tag for structs, namely,
required
, andnonzero
.required
option only applies for decoding, and it functions as an opposite toomitempty
. It demands that a field be explicitly provided in order for the yaml to decode without error. When we're expecting an end user to include a value for a field (e.g. a name) that cannot be omitted, this feature would be extremely useful.nonzero
option matters for both encoding and decoding. It states that the yaml may not have the zero value of the type. We use the same logic for determining what a zero value is asomitempty
. This means that[]
and{}
count as zero values (in contrast to how the Go language itself sees these) and that we respect what's reported byIsZeroer
types. Trying to encode or decode a field specified asnonzero
which has a zero value results in an error. Similar to therequired
option above, this is useful when we're expecting a user to provide a value for a field (e.g. a name) and we don't want that value to be blank (""
).See the tests in
decode_test.go
andencode_test.go
for examples!