Use enumer to generate String() methods for most enums (#25705)

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:
This commit is contained in:
Christopher Swenson
2024-04-17 11:14:14 -07:00
committed by GitHub
parent 55241c2b09
commit 961bf20bdb
55 changed files with 1345 additions and 146 deletions

View File

@@ -31,6 +31,7 @@ var (
DefaultRenewerRenewBuffer = 5
)
//go:generate enumer -type=RenewBehavior -trimprefix=RenewBehavior
type RenewBehavior uint
const (

View File

@@ -9,11 +9,9 @@ package api
import "fmt"
var PluginRuntimeTypes = []PluginRuntimeType{
PluginRuntimeTypeUnsupported,
PluginRuntimeTypeContainer,
}
var PluginRuntimeTypes = _PluginRuntimeTypeValues
//go:generate enumer -type=PluginRuntimeType -trimprefix=PluginRuntimeType -transform=snake
type PluginRuntimeType uint32
// This is a list of PluginRuntimeTypes used by Vault.
@@ -22,20 +20,11 @@ const (
PluginRuntimeTypeContainer
)
func (r PluginRuntimeType) String() string {
switch r {
case PluginRuntimeTypeContainer:
return "container"
default:
return "unsupported"
}
}
// ParsePluginRuntimeType is a wrapper around PluginRuntimeTypeString kept for backwards compatibility.
func ParsePluginRuntimeType(PluginRuntimeType string) (PluginRuntimeType, error) {
switch PluginRuntimeType {
case "container":
return PluginRuntimeTypeContainer, nil
default:
t, err := PluginRuntimeTypeString(PluginRuntimeType)
if err != nil {
return PluginRuntimeTypeUnsupported, fmt.Errorf("%q is not a supported plugin runtime type", PluginRuntimeType)
}
return t, nil
}

View File

@@ -0,0 +1,49 @@
// Code generated by "enumer -type=PluginRuntimeType -trimprefix=PluginRuntimeType -transform=snake"; DO NOT EDIT.
package api
import (
"fmt"
)
const _PluginRuntimeTypeName = "unsupportedcontainer"
var _PluginRuntimeTypeIndex = [...]uint8{0, 11, 20}
func (i PluginRuntimeType) String() string {
if i >= PluginRuntimeType(len(_PluginRuntimeTypeIndex)-1) {
return fmt.Sprintf("PluginRuntimeType(%d)", i)
}
return _PluginRuntimeTypeName[_PluginRuntimeTypeIndex[i]:_PluginRuntimeTypeIndex[i+1]]
}
var _PluginRuntimeTypeValues = []PluginRuntimeType{0, 1}
var _PluginRuntimeTypeNameToValueMap = map[string]PluginRuntimeType{
_PluginRuntimeTypeName[0:11]: 0,
_PluginRuntimeTypeName[11:20]: 1,
}
// PluginRuntimeTypeString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum.
func PluginRuntimeTypeString(s string) (PluginRuntimeType, error) {
if val, ok := _PluginRuntimeTypeNameToValueMap[s]; ok {
return val, nil
}
return 0, fmt.Errorf("%s does not belong to PluginRuntimeType values", s)
}
// PluginRuntimeTypeValues returns all values of the enum
func PluginRuntimeTypeValues() []PluginRuntimeType {
return _PluginRuntimeTypeValues
}
// IsAPluginRuntimeType returns "true" if the value is listed in the enum definition. "false" otherwise
func (i PluginRuntimeType) IsAPluginRuntimeType() bool {
for _, v := range _PluginRuntimeTypeValues {
if i == v {
return true
}
}
return false
}

View File

@@ -0,0 +1,50 @@
// Code generated by "enumer -type=RenewBehavior -trimprefix=RenewBehavior"; DO NOT EDIT.
package api
import (
"fmt"
)
const _RenewBehaviorName = "IgnoreErrorsRenewDisabledErrorOnErrors"
var _RenewBehaviorIndex = [...]uint8{0, 12, 25, 38}
func (i RenewBehavior) String() string {
if i >= RenewBehavior(len(_RenewBehaviorIndex)-1) {
return fmt.Sprintf("RenewBehavior(%d)", i)
}
return _RenewBehaviorName[_RenewBehaviorIndex[i]:_RenewBehaviorIndex[i+1]]
}
var _RenewBehaviorValues = []RenewBehavior{0, 1, 2}
var _RenewBehaviorNameToValueMap = map[string]RenewBehavior{
_RenewBehaviorName[0:12]: 0,
_RenewBehaviorName[12:25]: 1,
_RenewBehaviorName[25:38]: 2,
}
// RenewBehaviorString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum.
func RenewBehaviorString(s string) (RenewBehavior, error) {
if val, ok := _RenewBehaviorNameToValueMap[s]; ok {
return val, nil
}
return 0, fmt.Errorf("%s does not belong to RenewBehavior values", s)
}
// RenewBehaviorValues returns all values of the enum
func RenewBehaviorValues() []RenewBehavior {
return _RenewBehaviorValues
}
// IsARenewBehavior returns "true" if the value is listed in the enum definition. "false" otherwise
func (i RenewBehavior) IsARenewBehavior() bool {
for _, v := range _RenewBehaviorValues {
if i == v {
return true
}
}
return false
}