Skip to content

Conversation

@Fabianexe
Copy link

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.

@Fabianexe Fabianexe requested a review from abice as a code owner October 31, 2025 14:13
Copy link
Owner

@abice abice left a 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!

@abice abice requested a review from Copilot October 31, 2025 15:52
Copy link

Copilot AI left a 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 --bitfield CLI 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.

@coveralls
Copy link

coveralls commented Oct 31, 2025

Coverage Status

coverage: 92.776% (+0.1%) from 92.662%
when pulling f4f4f4a on Fabianexe:master
into 9d73c76 on abice:master.

@Fabianexe
Copy link
Author

updated error messages with you suggestions

@abice abice requested a review from Copilot October 31, 2025 20:17
Copy link

Copilot AI left a 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: --bitfield and --no-iota are 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.

@Fabianexe
Copy link
Author

I have added the tests.

I also notice that the error was never printed (only the enum was skipped).
To change this i moved the printing from parseEnum to the Generate function.
If you want a other solution let me know and i will change it.

// Parse the enum doc statement
enum, pErr := g.parseEnum(ts)
if pErr != nil {
fmt.Println(pErr)
Copy link
Owner

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.

Copy link
Owner

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.

@Fabianexe
Copy link
Author

Moved the prints back where they were and add bitfield error as sentinel error that are returned out of the Generate function.

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.

3 participants