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

code review #249

Closed
wants to merge 3 commits into from
Closed

code review #249

wants to merge 3 commits into from

Conversation

ccoVeille
Copy link
Contributor

  • remove useless receivers
  • remove useless else statement
  • avoid wrapping issues

leaving as soon as possible avoid complexity
- errors should be checked with errors.Is
- errors should be compared with errors.As
- errors should be wrapped with %w in fmt.Errorf
@ccoVeille
Copy link
Contributor Author

I started looking at the code and I found bad practice that could lead to panic or error if the code is refactored

@itchyny
Copy link
Owner

itchyny commented Apr 4, 2024

This is meaningless change applying unthoughtful best practices. The errors are controlled in the package and errors.Is is reasonable only when the error comes from another package, also I don't like for performance reasons. Return statement in else is intentional, not to catch unexpected failure of forgetting return in previous branches, possibly caused by nested branches. The first commit about useless receivers is the only one I can merge in the changes.

@ccoVeille
Copy link
Contributor Author

I get your points. I disagree with some of them, but OK, I don't mind.

I'll drop the commits you find useless.

var syntaxErr *json.SyntaxError
if errors.As(err, &syntaxErr) {
syntaxErr.Offset -= i.offset
offset, line = &syntaxErr.Offset, &i.line
Copy link
Owner

Choose a reason for hiding this comment

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

This part is good, the error comes from json package.

var syntaxErr *json.SyntaxError
if errors.As(err, &syntaxErr) {
syntaxErr.Offset -= i.offset
offset, line = &syntaxErr.Offset, &i.line
Copy link
Owner

Choose a reason for hiding this comment

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

ditto

@@ -2065,7 +2068,7 @@ func compileRegexp(re, flags string) (*regexp.Regexp, error) {
}
r, err := regexp.Compile(re)
if err != nil {
return nil, fmt.Errorf("invalid regular expression %q: %s", re, err)
return nil, fmt.Errorf("invalid regular expression %q: %w", re, err)
Copy link
Owner

@itchyny itchyny Apr 4, 2024

Choose a reason for hiding this comment

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

This change is fine, but I don't think gojq library users unwrap the execution errors though.

@@ -98,7 +99,7 @@ func (l *moduleLoader) LoadJSONWithMeta(name string, meta map[string]any) (any,
for {
var val any
if err := dec.Decode(&val); err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
Copy link
Owner

Choose a reason for hiding this comment

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

good.

@ccoVeille
Copy link
Contributor Author

I will need time and review from you to make sure the place you want me to use errors.Is/errors.As

I'm splitting this PR in two then.

#250
#251

I'm dropping the refactoring on switch/else.

@ccoVeille ccoVeille closed this Apr 5, 2024
@ccoVeille ccoVeille deleted the code-review branch April 5, 2024 10:39
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.

2 participants