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

@@ -16,6 +16,7 @@ import (
"github.com/hashicorp/vault/http"
)
//go:generate enumer -type=EnforceConsistency -trimprefix=EnforceConsistency
type EnforceConsistency int
const (
@@ -23,6 +24,7 @@ const (
EnforceConsistencyAlways
)
//go:generate enumer -type=WhenInconsistentAction -trimprefix=WhenInconsistent
type WhenInconsistentAction int
const (

View File

@@ -0,0 +1,49 @@
// Code generated by "enumer -type=EnforceConsistency -trimprefix=EnforceConsistency"; DO NOT EDIT.
package cache
import (
"fmt"
)
const _EnforceConsistencyName = "NeverAlways"
var _EnforceConsistencyIndex = [...]uint8{0, 5, 11}
func (i EnforceConsistency) String() string {
if i < 0 || i >= EnforceConsistency(len(_EnforceConsistencyIndex)-1) {
return fmt.Sprintf("EnforceConsistency(%d)", i)
}
return _EnforceConsistencyName[_EnforceConsistencyIndex[i]:_EnforceConsistencyIndex[i+1]]
}
var _EnforceConsistencyValues = []EnforceConsistency{0, 1}
var _EnforceConsistencyNameToValueMap = map[string]EnforceConsistency{
_EnforceConsistencyName[0:5]: 0,
_EnforceConsistencyName[5:11]: 1,
}
// EnforceConsistencyString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum.
func EnforceConsistencyString(s string) (EnforceConsistency, error) {
if val, ok := _EnforceConsistencyNameToValueMap[s]; ok {
return val, nil
}
return 0, fmt.Errorf("%s does not belong to EnforceConsistency values", s)
}
// EnforceConsistencyValues returns all values of the enum
func EnforceConsistencyValues() []EnforceConsistency {
return _EnforceConsistencyValues
}
// IsAEnforceConsistency returns "true" if the value is listed in the enum definition. "false" otherwise
func (i EnforceConsistency) IsAEnforceConsistency() bool {
for _, v := range _EnforceConsistencyValues {
if i == v {
return true
}
}
return false
}

View File

@@ -0,0 +1,50 @@
// Code generated by "enumer -type=WhenInconsistentAction -trimprefix=WhenInconsistent"; DO NOT EDIT.
package cache
import (
"fmt"
)
const _WhenInconsistentActionName = "FailRetryForward"
var _WhenInconsistentActionIndex = [...]uint8{0, 4, 9, 16}
func (i WhenInconsistentAction) String() string {
if i < 0 || i >= WhenInconsistentAction(len(_WhenInconsistentActionIndex)-1) {
return fmt.Sprintf("WhenInconsistentAction(%d)", i)
}
return _WhenInconsistentActionName[_WhenInconsistentActionIndex[i]:_WhenInconsistentActionIndex[i+1]]
}
var _WhenInconsistentActionValues = []WhenInconsistentAction{0, 1, 2}
var _WhenInconsistentActionNameToValueMap = map[string]WhenInconsistentAction{
_WhenInconsistentActionName[0:4]: 0,
_WhenInconsistentActionName[4:9]: 1,
_WhenInconsistentActionName[9:16]: 2,
}
// WhenInconsistentActionString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum.
func WhenInconsistentActionString(s string) (WhenInconsistentAction, error) {
if val, ok := _WhenInconsistentActionNameToValueMap[s]; ok {
return val, nil
}
return 0, fmt.Errorf("%s does not belong to WhenInconsistentAction values", s)
}
// WhenInconsistentActionValues returns all values of the enum
func WhenInconsistentActionValues() []WhenInconsistentAction {
return _WhenInconsistentActionValues
}
// IsAWhenInconsistentAction returns "true" if the value is listed in the enum definition. "false" otherwise
func (i WhenInconsistentAction) IsAWhenInconsistentAction() bool {
for _, v := range _WhenInconsistentActionValues {
if i == v {
return true
}
}
return false
}