Skip to content

Add a helper for decoding protobuf enums#47230

Closed
Joerger wants to merge 1 commit intomasterfrom
joerger/enum-helper
Closed

Add a helper for decoding protobuf enums#47230
Joerger wants to merge 1 commit intomasterfrom
joerger/enum-helper

Conversation

@Joerger
Copy link
Copy Markdown
Contributor

@Joerger Joerger commented Oct 4, 2024

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

@Joerger Joerger requested a review from rosstimothy October 4, 2024 20:55
@Joerger Joerger mentioned this pull request Oct 4, 2024
@github-actions github-actions Bot requested a review from capnspacehook October 4, 2024 20:55
@github-actions github-actions Bot requested a review from probakowski October 4, 2024 20:55
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Oct 4, 2024

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 changelog: followed by the changelog entries for the PR.

@Joerger Joerger added the no-changelog Indicates that a PR does not require a changelog entry label Oct 4, 2024
@Joerger Joerger force-pushed the joerger/enum-helper branch from fb07aff to c265007 Compare October 4, 2024 21:10
@Joerger Joerger mentioned this pull request Oct 5, 2024
Comment on lines +1121 to 1132
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")
}
Copy link
Copy Markdown
Contributor

@rosstimothy rosstimothy Oct 8, 2024

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.

Copy link
Copy Markdown
Contributor Author

@Joerger Joerger Oct 8, 2024

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.

Comment thread api/types/enum.go
// 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 {
Copy link
Copy Markdown
Contributor

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.

@Joerger Joerger closed this Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-changelog Indicates that a PR does not require a changelog entry size/sm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants