10000 Add support for `required` and `nonzero` yaml fields by RyanBrushett · Pull Request #728 · goccy/go-yaml · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

RyanBrushett
Copy link

This adds support for two new options in the yaml tag for structs, namely, required, and nonzero.

  • The required option only applies for decoding, and it functions as an opposite to omitempty. 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.
  • The 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 as omitempty. 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 by IsZeroer types. Trying to encode or decode a field specified as nonzero which has a zero value results in an error. Similar to the required 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 and encode_test.go for examples!

  • Describe the purpose for which you created this PR.
  • Create test code that corresponds to the modification

@RyanBrushett RyanBrushett force-pushed the required-yaml-fields branch from 90fa810 to baa4a17 Compare May 8, 2025 18:50
@codecov-commenter
Copy link
codecov-commenter commented May 8, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 66.37168% with 38 lines in your changes missing coverage. Please review.

Project coverage is 77.98%. Comparing base (52dacb8) to head (246452a).

❗ 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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@shuheiktgw shuheiktgw requested a review from Copilot May 9, 2025 07:06
Copy link
@Copilot Copilot AI left a 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 {

RyanBrushett and others added 3 commits May 30, 2025 10:51
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>
@RyanBrushett RyanBrushett force-pushed the required-yaml-fields branch from b8d4def to 246452a Compare May 30, 2025 14:52
@RyanBrushett
Copy link
Author

We updated this branch to differentiate nonempty and nonzero as omitzero was added alongside omitempty since we've opened this PR.

@lugray
Copy link
lugray commented Jun 9, 2025

@goccy - we addressed the comment that the copilot review produced, and have also rebased to incorporate the conflicting changes in #729. Are these changes that you're interested in? Is there another approach you'd rather see us take for similar behaviour?

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.

3 participants
0