-
Notifications
You must be signed in to change notification settings - Fork 2k
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
tools: structs method generation #10817
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very excited about this, but the parsing code is daunting. More code comments might help, but I think my only ask before merging is a README describing what this is and how to use.
tools/nomad-generate/main.go
Outdated
//go:embed structs.copy.tmpl | ||
var copyTmpl embed.FS | ||
|
||
//go:embed structs.equals.tmpl | ||
var equalsTmpl embed.FS | ||
|
||
//go:embed structs.diff.tmpl | ||
var diffTmpl embed.FS | ||
|
||
//go:embed structs.merge.tmpl | ||
var mergeTmpl embed.FS |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super excited to start using builtin embed!
@@ -0,0 +1,82 @@ | |||
package structs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To make Go happy I think we should emit a comment matching:
^// Code generated .* DO NOT EDIT\.$
@schmichael @tgross I'll take a stab at a README, similar to what I am doing in this jobspec update PR #10819. Also, I had some readability/maintainability refactorings in another branch that didn't land in time for Friday. I'll take a stab at rebasing it off of this + adding comments and then see if we want to merge. |
@tgross @schmichael Here's the other branch. Let me know if you'd prefer a PR targeting this one. https://github.com/hashicorp/nomad/tree/generate-structs-refactor |
You can probably just drop commits onto this PR if you want, but I'd strongly recommend splitting the file rename refactor from any other refactors so that folks can review them separately. Ex. right now generate-structs...generate-structs-refactor just show the whole file getting replaced. |
Developers are required to implement methods like `Copy`, `Equals`, `Diff`, and `Merge` for many of the types in the `structs` package, and this is both time-consuming and error prone, having resulted in several correctness bugs from failing to copy objects retrieved from go-memdb or failing to compare objects during plans. This changeset introduces a prototype tool to use with `go:generate` directives that can generate methods automatically for our developers. Future changesets will include documentation for developers and porting the existing methods to use this tool. We'll continue to debug and refine the tool as we go. Co-authored-by: Derek Strickland <[email protected]> Co-authored-by: Tim Gross <[email protected]>
* use docstring comments rather than long command lines to signal the top-level target types and methods * break up into separate analyzer and generator types for clarity and reduced state sharing * make only 2 passes thru AST
Developers are required to implement methods like
Copy
,Equals
,Diff
,and
Merge
for many of the types in thestructs
package, and this is bothtime-consuming and error prone, having resulted in several correctness bugs
from failing to copy objects retrieved from go-memdb or failing to compare
objects during plans.
This changeset introduces a prototype tool to use with
go:generate
directives that can generate methods automatically for our developers. Future
changesets will include documentation for developers and porting the existing
methods to use this tool. We'll continue to debug and refine the tool as we
go.
(Co-authored with @DerekStrickland )