diff options
| author | Grégoire Duchêne <gduchene@awhk.org> | 2022-12-10 14:27:00 +0000 |
|---|---|---|
| committer | Grégoire Duchêne <gduchene@awhk.org> | 2022-12-10 14:41:04 +0000 |
| commit | cf1bfb5f93c5c8f5b923e2ed84a7ef45f188876c (patch) | |
| tree | fff4ba83a8a3025bcba053c7f0d833edd05dc3e3 /flag.go | |
| parent | 3cc43119b40d3a556ae818b69bad5d977cc24014 (diff) | |
Add ParseProtobufEnumv0.5.0
Diffstat (limited to 'flag.go')
| -rw-r--r-- | flag.go | 24 |
1 files changed, 24 insertions, 0 deletions
@@ -145,6 +145,30 @@ func (f *Feature) String() string { // value or an error. type ParseFunc[T any] func(string) (T, error) +// ParseProtobufEnum returns a ParseFunc that will return the +// appropriate enum value or a UnknownEnumValueError if the string +// passed did not match any of the values supplied. +// +// Strings are compared in uppercase only, so ‘FOO,’ ‘foo,’, and ‘fOo’ +// all refer to the same value. +// +// Callers should pass the protoc-generated *_value directly. See +// https://developers.google.com/protocol-buffers/docs/reference/go-generated#enum +// for more details. +func ParseProtobufEnum[T ~int32](values map[string]int32) ParseFunc[T] { + valid := make([]string, 0, len(values)) + for val := range values { + valid = append(valid, val) + } + return func(s string) (T, error) { + val, found := values[strings.ToUpper(s)] + if !found { + return 0, UnknownEnumValueError{s, valid} + } + return T(val), nil + } +} + // ParseStringEnum returns a ParseFunc that will return the string // passed if it matched any of the values supplied. If no such match is // found, an UnknownEnumValueError is returned. |
