-
Notifications
You must be signed in to change notification settings - Fork 20
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
Add a code generator for enum boilerplate #401
base: main
Are you sure you want to change the base?
Conversation
internal/enum/enum.go
Outdated
// - //enum:stringfunc Name provides a function to use for stringifying | ||
// based on the name of a constant (see below). |
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.
This functionality is used in only one place. I am so-so on whether it pulls its weight. Feel free to veto this.
internal/enum/enum.go
Outdated
return false | ||
} | ||
|
||
func ParseDirectives(fs *token.FileSet, comments []*ast.CommentGroup) ([]Directive, error) { |
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.
These seems like serious overkill. How about just define the enums (name, doc, constants) in a yaml file and then parsing the YAML and generating Go code (including for the type definition and constants) is trivial. I don't think it needs to be very configurable -- you could make it always emit some sort of "Lookup" factory function or maybe just make it a simple bool flag in the yaml.
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.
Lol using a config file hadn't occurred to me.
Go does not have enums, but they are quite useful, so we emulate them with int-kinded types and use switches and whatnot to convert them to and from strings.
There are enough of these in the new compiler stack at this point that maintaining all of these stringification functions is getting tedious and error-prone, so this PR contains a very simple, 350-line
//go:generate
helper for generating those functions.I surveyed other existing packages of this kind, but unfortunately they all want to put varying strong constraints on how we organize and name our "enums", so I felt that we should just write the simplest possible thing to fit our use-case.