Skip to content
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

Merged
merged 4 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Use enumer to generate String() methods for most enums
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
  • Loading branch information
Christopher Swenson committed Feb 28, 2024
commit c3c1f8d541fc0c7df6d4bfb447db04db261265e8
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
Copy link
Collaborator

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...

Copy link
Contributor Author

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.

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
Loading