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

Refactor overflowError to be pretty-printable #470

Merged
merged 1 commit into from
Oct 28, 2024

Conversation

oxzi
Copy link
Contributor

@oxzi oxzi commented Jul 29, 2024

When trying to yaml.Unmarshal a negative number into an uint, the resulting error indicates an overflow with type information, but without the position. For example:

cannot unmarshal -23 into Go value of type uint64 ( overflow )

From an end user's perspective reading an error for an invalid YAML, this is quite unintuitive. Especially compared to the error message when trying to unmarshal a string into an uint.

[1:4] cannot unmarshal string into Go struct field Foo.A of type uint64
    >  1 | a: 'foo'
              ^

This change has moved overflowError to internal.errors.overflowError, implementing both the PrettyPrinter and Formatter. Its implementation is uniform with those of the syntaxError and TypeError.
Thus, the error from above now looks like:

[1:4] cannot unmarshal -23 into Go value of type uint64 ( overflow )
    >  1 | a: -23
              ^

@codecov-commenter
Copy link

codecov-commenter commented Jul 29, 2024

⚠️ 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 78.76%. Comparing base (237df0e) to head (8bfb9ef).
Report is 1 commits behind head on master.

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

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #470      +/-   ##
==========================================
- Coverage   78.77%   78.76%   -0.01%     
==========================================
  Files          13       13              
  Lines        4004     4003       -1     
==========================================
- Hits         3154     3153       -1     
  Misses        591      591              
  Partials      259      259              

decode.go Outdated
}
return errOverflow(valueType, fmt.Sprint(v))
return errTypeMismatch(valueType, reflect.TypeOf(v), src.GetToken())
Copy link
Owner

Choose a reason for hiding this comment

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

I need the overflow information, so could you modify the errOverflow function to pass src.GetToken ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for your review. I am going to modify the error as you suggested.

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 have changed the approach of this PR. Instead of using another error, I have refactored the overflowError to be a PrettyPrinter and Formatter like the other error types.

Please take another look and thanks again for your review and providing this library.

@goccy goccy added the reviewed label Jul 29, 2024
When trying to yaml.Unmarshal a negative number into an uint, the
resulting error indicates an overflow with type information, but without
the position. For example:

> cannot unmarshal -23 into Go value of type uint64 ( overflow )

From an end user's perspective reading an error for an invalid YAML,
this is quite unintuitive. Especially compared to the error message when
trying to unmarshal a string into an uint.

> [1:4] cannot unmarshal string into Go struct field Foo.A of type uint64
>     >  1 | a: 'foo'
>               ^

This change has moved overflowError to internal.errors.overflowError,
implementing both the PrettyPrinter and Formatter. Its implementation is
uniform with those of the syntaxError and TypeError.

Thus, the error from above now looks like:

> [1:4] cannot unmarshal -23 into Go value of type uint64 ( overflow )
>     >  1 | a: -23
>               ^
@oxzi oxzi force-pushed the decode-negative-uint-no-overflow-error branch from 525e201 to 8bfb9ef Compare August 1, 2024 09:00
@oxzi oxzi changed the title Type mismatch for decoding a negative number as an uint Refactor overflowError to be pretty-printable Aug 1, 2024
@oxzi oxzi requested a review from goccy August 1, 2024 09:04
Copy link
Owner

@goccy goccy left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution ! LGTM 👍

@goccy goccy merged commit 56f8b29 into goccy:master Oct 28, 2024
17 checks passed
@oxzi oxzi deleted the decode-negative-uint-no-overflow-error branch October 28, 2024 09:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants