-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Add a helper for decoding protobuf enums #47230
Conversation
The PR changelog entry failed validation: Changelog entry not found in the PR body. Please add a "no-changelog" label to the PR, or changelog lines starting with |
fb07aff
to
c265007
Compare
func (r *RequireMFAType) decode(val any) error { | ||
err := decodeEnum(r, val, map[any]RequireMFAType{ | ||
"": RequireMFAType_OFF, // default to off | ||
false: RequireMFAType_OFF, | ||
true: RequireMFAType_SESSION, | ||
RequireMFATypeHardwareKeyString: RequireMFAType_SESSION_AND_HARDWARE_KEY, | ||
RequireMFATypeHardwareKeyTouchString: RequireMFAType_HARDWARE_KEY_TOUCH, | ||
RequireMFATypeHardwareKeyPINString: RequireMFAType_HARDWARE_KEY_PIN, | ||
RequireMFATypeHardwareKeyTouchAndPINString: RequireMFAType_HARDWARE_KEY_TOUCH_AND_PIN, | ||
}, RequireMFAType_name) | ||
return trace.Wrap(err, "failed to decode require mfa type") | ||
} |
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.
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.
// 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 { |
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.
General use helper functions like this should be covered by tests.
Add a helper for decoding protobuf enums from their valid representions, e.g. bool, string, or number.
This is intended to make the code a bit more readable around
decodeEnum
and more reusable.Prereq for #47233