-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Use enumer to generate String() methods for most enums #25705
Conversation
We have many hand-written String() methods (and similar) for enums. These require more maintenance and are more error-prone than using automatically generated methods. In addition, the auto-generated versions can be more efficient. Here, we switch to using https://github.com/loggerhead/enumer, itself a fork of https://github.com/diegostamigni/enumer, no longer maintained, and a fork of the mostly standard tool https://pkg.go.dev/golang.org/x/tools/cmd/stringer. We use this fork of enumer for Go 1.20+ compatibility and because we require the `-transform` flag to be able to generate constants that match our current code base. Some enums were not targeted for this change: * Bit sets, which are not supported by enumer or stringer. * Enums with non-standard naming schemes, like having a common suffix instead of a prefix, or using spaces instead of underscores or dashes. * Enums that don't have a type associated
CI Results: |
Build Results: |
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.
Neat transform, but my concern is it doesn't preserve the default case for String(), which code may be relying on.
case PluginRuntimeTypeContainer: | ||
return "container" | ||
default: | ||
return "unsupported" |
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.
Curious how enumer handles the defaults?
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.
The default will be TypeName(number)
. For most of these, I tried to ensure that the default wasn't really used, or that the particular String()
value wasn't relied on.
@sgmiller it doesn't preserve the default case we were using, which was lowercase, but where that mattered, I used the I didn't convert any enums that we couldn't match the current |
type tidyStatusState int | ||
|
||
const ( | ||
tidyStatusInactive tidyStatusState = iota |
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.
I guess my other question is whether it's necessary to add full enum functionality to those that were simply used with iota. Adds code that isn't used...
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.
If the code isn't used then it will be trimmed out by the compiler at build time, so I'm not super worried about it adding to the binary. And I figured it is better to be as complete as possible in this conversion in case someone needs the extra functions for debugging.
@@ -252,6 +252,7 @@ type Check interface { | |||
Evaluate(e *Executor) ([]*Result, error) | |||
} | |||
|
|||
//go:generate enumer -type=ResultStatus -trimprefix=Result -transform=snake |
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.
Contrast with this one where we're definitely cleaning up things using enumer.
Thanks! |
We have many hand-written String() methods (and similar) for enums. These require more maintenance and are more error-prone than using automatically generated methods. In addition, the auto-generated versions can be more efficient. Here, we switch to using https://github.com/loggerhead/enumer, itself a fork of https://github.com/diegostamigni/enumer, no longer maintained, and a fork of the mostly standard tool https://pkg.go.dev/golang.org/x/tools/cmd/stringer. We use this fork of enumer for Go 1.20+ compatibility and because we require the `-transform` flag to be able to generate constants that match our current code base. Some enums were not targeted for this change:
We have many hand-written String() methods (and similar) for enums. These require more maintenance and are more error-prone than using automatically generated methods. In addition, the auto-generated versions can be more efficient. Here, we switch to using https://github.com/loggerhead/enumer, itself a fork of https://github.com/diegostamigni/enumer, no longer maintained, and a fork of the mostly standard tool https://pkg.go.dev/golang.org/x/tools/cmd/stringer. We use this fork of enumer for Go 1.20+ compatibility and because we require the `-transform` flag to be able to generate constants that match our current code base. Some enums were not targeted for this change: Co-authored-by: Christopher Swenson <christopher.swenson@hashicorp.com>
If you encountered an error during
Please run |
We have many hand-written String() methods (and similar) for enums. These require more maintenance and are more error-prone than using automatically generated methods. In addition, the auto-generated versions can be more efficient.
Here, we switch to using https://github.com/loggerhead/enumer, itself a fork of https://github.com/diegostamigni/enumer, no longer maintained, and a fork of the mostly standard tool
https://pkg.go.dev/golang.org/x/tools/cmd/stringer. We use this fork of enumer for Go 1.20+ compatibility and because we require the
-transform
flag to be able to generate constants that match our current code base.Some enums were not targeted for this change: