mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-01 19:17:58 +00:00
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:
committed by
GitHub
parent
55241c2b09
commit
961bf20bdb
50
sdk/database/dbplugin/v5/credentialtype_enumer.go
Normal file
50
sdk/database/dbplugin/v5/credentialtype_enumer.go
Normal file
@@ -0,0 +1,50 @@
|
||||
// Code generated by "enumer -type=CredentialType -trimprefix=CredentialType -transform=snake"; DO NOT EDIT.
|
||||
|
||||
package dbplugin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const _CredentialTypeName = "passwordrsa_private_keyclient_certificate"
|
||||
|
||||
var _CredentialTypeIndex = [...]uint8{0, 8, 23, 41}
|
||||
|
||||
func (i CredentialType) String() string {
|
||||
if i < 0 || i >= CredentialType(len(_CredentialTypeIndex)-1) {
|
||||
return fmt.Sprintf("CredentialType(%d)", i)
|
||||
}
|
||||
return _CredentialTypeName[_CredentialTypeIndex[i]:_CredentialTypeIndex[i+1]]
|
||||
}
|
||||
|
||||
var _CredentialTypeValues = []CredentialType{0, 1, 2}
|
||||
|
||||
var _CredentialTypeNameToValueMap = map[string]CredentialType{
|
||||
_CredentialTypeName[0:8]: 0,
|
||||
_CredentialTypeName[8:23]: 1,
|
||||
_CredentialTypeName[23:41]: 2,
|
||||
}
|
||||
|
||||
// CredentialTypeString retrieves an enum value from the enum constants string name.
|
||||
// Throws an error if the param is not part of the enum.
|
||||
func CredentialTypeString(s string) (CredentialType, error) {
|
||||
if val, ok := _CredentialTypeNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to CredentialType values", s)
|
||||
}
|
||||
|
||||
// CredentialTypeValues returns all values of the enum
|
||||
func CredentialTypeValues() []CredentialType {
|
||||
return _CredentialTypeValues
|
||||
}
|
||||
|
||||
// IsACredentialType returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||
func (i CredentialType) IsACredentialType() bool {
|
||||
for _, v := range _CredentialTypeValues {
|
||||
if i == v {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -144,6 +144,8 @@ type NewUserResponse struct {
|
||||
Username string
|
||||
}
|
||||
|
||||
//go:generate enumer -type=CredentialType -trimprefix=CredentialType -transform=snake
|
||||
|
||||
// CredentialType is a type of database credential.
|
||||
type CredentialType int
|
||||
|
||||
@@ -153,19 +155,6 @@ const (
|
||||
CredentialTypeClientCertificate
|
||||
)
|
||||
|
||||
func (k CredentialType) String() string {
|
||||
switch k {
|
||||
case CredentialTypePassword:
|
||||
return "password"
|
||||
case CredentialTypeRSAPrivateKey:
|
||||
return "rsa_private_key"
|
||||
case CredentialTypeClientCertificate:
|
||||
return "client_certificate"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
}
|
||||
|
||||
// ///////////////////////////////////////////////////////
|
||||
// UpdateUser()
|
||||
// ///////////////////////////////////////////////////////
|
||||
|
||||
Reference in New Issue
Block a user