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

Add a code generator for enum boilerplate #401

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Add a code generator for enum boilerplate #401

wants to merge 7 commits into from

Conversation

mcy
Copy link
Member

@mcy mcy commented Dec 18, 2024

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.

@mcy mcy requested a review from jhump December 18, 2024 23:11
Comment on lines 43 to 44
// - //enum:stringfunc Name provides a function to use for stringifying
// based on the name of a constant (see below).
Copy link
Member Author

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.

return false
}

func ParseDirectives(fs *token.FileSet, comments []*ast.CommentGroup) ([]Directive, error) {
Copy link
Member

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.

Copy link
Member Author

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.

@mcy mcy requested a review from jhump December 20, 2024 20:41
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