-
Notifications
You must be signed in to change notification settings - Fork 65
Add support for generating enums as bitfields #301
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
base: master
Are you sure you want to change the base?
Conversation
abice
left a comment
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.
Minor tweaks, but otherwise looks good. When those are changed, will approve and commit. Thanks for the PR!
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.
Pull Request Overview
This PR adds support for generating bitfield enums in the go-enum tool. When the --bitfield flag is used, enum values are generated as bit fields using the 1<<iota pattern instead of sequential values.
- Adds a new
--bitfieldCLI flag to enable bitfield enum generation - Implements validation to prevent bitfields with string types or manually set values
- Updates the enum template to generate values using bit shift operations when bitfield mode is enabled
Reviewed Changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| main.go | Adds BitField field to rootT struct and CLI flag definition |
| main_test.go | Adds test coverage for the new --bitfield flag |
| generator/options.go | Adds BitField configuration field to GeneratorConfig |
| generator/generator.go | Implements bitfield validation logic and template data |
| generator/enum.tmpl | Updates template to generate 1<<iota pattern for bitfields |
| example/enum_bitfield.go | Provides example enum definition for bitfield usage |
| example/enum_bitfield_enum.go | Generated output demonstrating bitfield enum implementation |
| example/enum_bitfield_test.go | Test cases verifying bitfield enum behavior |
| README.md | Documents the new --bitfield flag in CLI options |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
updated error messages with you suggestions |
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.
Pull Request Overview
Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
main.go:244
- Missing validation for incompatible flag combination:
--bitfieldand--no-iotaare mutually exclusive. The bitfield feature requires iota to generate the bit shift expressions (1<<iota), but no-iota disables iota usage. This combination should be validated and rejected with a clear error message.
// Validate incompatible flag combinations
if argv.NoParse && argv.MustParse {
return fmt.Errorf("--noparse and --mustparse are incompatible: MustParse requires the Parse method to exist")
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
I have added the tests. I also notice that the error was never printed (only the enum was skipped). |
generator/generator.go
Outdated
| // Parse the enum doc statement | ||
| enum, pErr := g.parseEnum(ts) | ||
| if pErr != nil { | ||
| fmt.Println(pErr) |
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.
Please use Sentinel errors to determine if we should print the error or not. There are other errors in there that do not need to be printed, and would just cause a bunch of noise.
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.
And it should actually fail the call if these particular errors should stop processing, which it should do.
|
Moved the prints back where they were and add bitfield error as sentinel error that are returned out of the Generate function. |
As discussed in #172 it would be nice to have an option to generate the enum values as bit fields.
This pull request add a new option to generate the enums with
1<<iota.It tries this with minimal impact on the template and the rest of the code.
To keep it simple it forbids the combination of this new option with custom values.
It also adds an example with a test for the new bitfield option.