Skip to content

Conversation

@ARR4N
Copy link
Collaborator

@ARR4N ARR4N commented Dec 13, 2024

Why this should be merged

Replaces #86. That approach couldn't be replicated for generated JSON marshalling, and this once also reduces the upstream delta.

How this works

AST manipulation of a specified file, which finds specified methods and modifies their names to be unexported.

How this was tested

Inspection of the output (core/types/gen_header_rlp.go remains unchanged).

Copy link

@qdm12 qdm12 left a comment

Choose a reason for hiding this comment

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

Nice lower deltas idea 💯

@ARR4N ARR4N marked this pull request as ready for review December 16, 2024 11:49
@ARR4N ARR4N requested a review from qdm12 December 16, 2024 12:08
@ARR4N ARR4N enabled auto-merge (squash) December 17, 2024 15:54
@ARR4N ARR4N requested review from a team, ceyonur and darioush and removed request for a team December 17, 2024 15:54
Copy link

@qdm12 qdm12 left a comment

Choose a reason for hiding this comment

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

Nice 👍
Just 2-3 tiny comments on error wrappings (I'm a fanatic of error wrappings ❌ 📓 )

// Since we're not creating, the zero perm/mode is ignored.
f, err := os.OpenFile(fileName, os.O_TRUNC|os.O_WRONLY, 0) //nolint:gosec // Variable file is under our direct control in go:generate
if err != nil {
return fmt.Errorf("os.OpenFile(%q, [write-only, truncate]): %v", fileName, err)
Copy link

Choose a reason for hiding this comment

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

nit always wrap errors with %w (aka wrap), not %v; also regarding error wrapping, I would suggest something a bit less code-focused such as:

Suggested change
return fmt.Errorf("os.OpenFile(%q, [write-only, truncate]): %v", fileName, err)
return fmt.Errorf("opening source file to override %q: %w", fileName, err)
  • The programmer when debugging could then easily find the relevant code os.OpenFile(fileName, os.O_TRUNC|os.O_WRONLY, 0) so there is no need to wrap this information I think
  • the programmer does not need to maintain the error message wrap in case the function call os.OpenFile changes (just a general rule, it probably won't change much here)

return fmt.Errorf("os.OpenFile(%q, [write-only, truncate]): %v", fileName, err)
}
if err := format.Node(f, fset, parsed); err != nil {
return fmt.Errorf("format.Node(%T): %v", parsed, err)
Copy link

Choose a reason for hiding this comment

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

same here 😉 I don't think having the parsed type (always *ast.File) is really useful, and we don't really want to log it out either 😄

Suggested change
return fmt.Errorf("format.Node(%T): %v", parsed, err)
return fmt.Errorf("formatting node to file: %w", err)

mode := parser.SkipObjectResolution | parser.ParseComments
parsed, err := parser.ParseFile(fset, fileName, nil, mode)
if err != nil {
return fmt.Errorf("parser.ParseFile(%q): %v", fileName, err)
Copy link

Choose a reason for hiding this comment

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

nit the wrapping is a bit misleading, especially since the ParseFile signature isn't the same. Why not just dumb old human-like wording as follows? Also wrap errors with %w not %v 😉

Suggested change
return fmt.Errorf("parser.ParseFile(%q): %v", fileName, err)
return fmt.Errorf("parsing source file %q: %w", fileName, err)

@ARR4N ARR4N merged commit 43878f4 into main Dec 17, 2024
4 checks passed
@ARR4N ARR4N deleted the arr4n/internalise-cmd branch December 17, 2024 16:20
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