Skip to content

Avoid returning an error for null document #721

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
May 7, 2025
Merged

Avoid returning an error for null document #721

merged 1 commit into from
May 7, 2025

Conversation

shuheiktgw
Copy link
Collaborator

Closes #711

When a YAML document is null or ~, instead of returning io.EOF, I believe decode.Decode should decode the value into nil, which is also compatible with go-yaml/yaml.

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

@shuheiktgw shuheiktgw requested a review from Copilot May 7, 2025 07:02
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 updates the decode behavior to handle YAML documents that are explicitly null (using null or ~) by decoding them as nil instead of returning an error.

  • Adjusted conditional check in decode.go to include documents with a null type.
  • Added corresponding test code to ensure that a null document decodes correctly.

@@ -1892,7 +1892,7 @@ func (d *Decoder) parse(ctx context.Context, bytes []byte) (*ast.File, error) {
if err != nil {
return nil, err
}
if v != nil {
if v != nil || (doc.Body != nil && doc.Body.Type() == ast.NullType) {
Copy link
Preview

Copilot AI May 7, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider extracting the null-check logic into a helper function or clearly documenting the rationale inline to improve the readability and maintainability of the code.

Copilot uses AI. Check for mistakes.

@codecov-commenter
Copy link

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

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 77.92%. Comparing base (0203d69) to head (367a04a).

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #721      +/-   ##
==========================================
+ Coverage   77.87%   77.92%   +0.05%     
==========================================
  Files          22       22              
  Lines        7986     7986              
==========================================
+ Hits         6219     6223       +4     
+ Misses       1352     1348       -4     
  Partials      415      415              
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

"v: ~",
map[string]interface{}{"v": nil},
source: "~",
value: (*struct{})(nil),
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sorry for the large diff 🙇 This is the only new test case I added. I wanted to distinguish the io.EOF error from others, so I added an eof field to the test struct.

@goccy
Copy link
Owner

goccy commented May 7, 2025

Thank you for your great PR !! LGTM 👍

@goccy goccy merged commit 427b18e into master May 7, 2025
20 checks passed
@goccy goccy deleted the return_null branch May 7, 2025 12:29
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.

[for yq] Null YAML document throws error
3 participants