-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add a helper for decoding protobuf enums #47230
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| Copyright 2024 Gravitational, Inc. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package types | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| "github.com/gravitational/trace" | ||
| ) | ||
|
|
||
| // decodeEnum decodes a protobuf enum from a representational value, usually a bool, | ||
| // string, or from the actual enum (int32) value. If the value is valid, it is saved | ||
| // in the given enum pointer. | ||
| func decodeEnum[T ~int32](p *T, val any, representationMap map[any]T, enumMap map[int32]string) error { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. General use helper functions like this should be covered by tests. |
||
| if v, ok := representationMap[val]; ok { | ||
| *p = v | ||
| return nil | ||
| } | ||
|
|
||
| // try parsing as a bool value | ||
| if v, ok := val.(string); ok { | ||
| switch strings.ToLower(v) { | ||
| case "yes", "yeah", "y", "true", "1", "on": | ||
| if v, ok := representationMap[true]; ok { | ||
| *p = v | ||
| return nil | ||
| } | ||
| case "no", "nope", "n", "false", "0", "off": | ||
| if v, ok := representationMap[false]; ok { | ||
| *p = v | ||
| return nil | ||
| } | ||
| } | ||
| return trace.BadParameter("unknown enum value %v", val) | ||
| } | ||
|
|
||
| // parse as enum | ||
| var enumVal T | ||
| switch v := val.(type) { | ||
| case int: | ||
| enumVal = T(v) | ||
| case int32: | ||
| enumVal = T(v) | ||
| case int64: | ||
| enumVal = T(v) | ||
| case float64: | ||
| enumVal = T(v) | ||
| case float32: | ||
| enumVal = T(v) | ||
| default: | ||
| return trace.BadParameter("unknown enum value %v", val) | ||
| } | ||
|
|
||
| if err := checkEnum(enumMap, int32(enumVal)); err != nil { | ||
| return trace.BadParameter("unknown enum value %v", val) | ||
| } | ||
|
|
||
| *p = enumVal | ||
| return nil | ||
| } | ||
|
|
||
| // checkEnum checks if the given enum is valid. | ||
| func checkEnum(enumMap map[int32]string, val int32) error { | ||
| if _, ok := enumMap[val]; ok { | ||
| return nil | ||
| } | ||
| return trace.NotFound("enum %v not found in enum map", val) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I prefer the more explicit implementation than farming things out to this helper. The call site here is quite dense and a bit hard to grok what is going on without jumping to the decodeEnum function.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough. My thinking here is that I like the readability of the helper's usage - you can see at a glance what values map to what enum without having to parse through the type switches, bool option handling, etc. But I'm not married to the change at all, I can just close this one.
Edit: it also did turn out more complex than I envisioned, so I understand the confusion concern.