Skip to content

Commit

Permalink
Use enumer to generate String() methods for most enums (#25705)
Browse files Browse the repository at this point in the history
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:
  • Loading branch information
Christopher Swenson authored Apr 17, 2024
1 parent 55241c2 commit 961bf20
Show file tree
Hide file tree
Showing 55 changed files with 1,345 additions and 146 deletions.
1 change: 1 addition & 0 deletions .github/actions/build-vault/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ runs:
- uses: ./.github/actions/set-up-go
with:
github-token: ${{ inputs.github-token }}
- uses: ./.github/actions/install-external-tools
- if: inputs.vault-edition != 'ce'
name: Configure Git
shell: bash
Expand Down
2 changes: 2 additions & 0 deletions .github/actions/install-external-tools/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ runs:
shell: bash
- run: go install github.com/golangci/revgrep/cmd/revgrep@latest
shell: bash
- run: go install github.com/loggerhead/enumer@latest
shell: bash
1 change: 1 addition & 0 deletions api/lifetime_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
DefaultRenewerRenewBuffer = 5
)

//go:generate enumer -type=RenewBehavior -trimprefix=RenewBehavior
type RenewBehavior uint

const (
Expand Down
23 changes: 6 additions & 17 deletions api/plugin_runtime_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
49 changes: 49 additions & 0 deletions api/pluginruntimetype_enumer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions api/renewbehavior_enumer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions builtin/logical/pki/defaultdirectorypolicytype_enumer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions builtin/logical/pki/ifmodifiedreqtype_enumer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions builtin/logical/pki/path_config_acme.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ func getDefaultDirectoryPolicyType(defaultDirectoryPolicy string) (DefaultDirect
}
}

//go:generate enumer -type=DefaultDirectoryPolicyType
type DefaultDirectoryPolicyType int

const (
Expand Down
13 changes: 7 additions & 6 deletions builtin/logical/pki/path_tidy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ import (

var tidyCancelledError = errors.New("tidy operation cancelled")

//go:generate enumer -type=tidyStatusState -trimprefix=tidyStatus
type tidyStatusState int

const (
tidyStatusInactive tidyStatusState = iota
tidyStatusStarted = iota
tidyStatusFinished = iota
tidyStatusError = iota
tidyStatusCancelling = iota
tidyStatusCancelled = iota
tidyStatusInactive tidyStatusState = iota
tidyStatusStarted
tidyStatusFinished
tidyStatusError
tidyStatusCancelling
tidyStatusCancelled
)

type tidyStatus struct {
Expand Down
53 changes: 53 additions & 0 deletions builtin/logical/pki/tidystatusstate_enumer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions builtin/logical/pki/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,16 @@ func parseIfNotModifiedSince(req *logical.Request) (time.Time, error) {
return headerTimeValue, nil
}

//go:generate enumer -type=ifModifiedReqType -trimprefix=ifModified
type ifModifiedReqType int

const (
ifModifiedUnknown ifModifiedReqType = iota
ifModifiedCA = iota
ifModifiedCRL = iota
ifModifiedDeltaCRL = iota
ifModifiedUnifiedCRL = iota
ifModifiedUnifiedDeltaCRL = iota
ifModifiedUnknown ifModifiedReqType = iota
ifModifiedCA
ifModifiedCRL
ifModifiedDeltaCRL
ifModifiedUnifiedCRL
ifModifiedUnifiedDeltaCRL
)

type IfModifiedSinceHelper struct {
Expand Down
Loading

0 comments on commit 961bf20

Please sign in to comment.