Add opt-in StrongEnums mode for strongly typed protobuf enums#1366
Draft
wasimat404 wants to merge 1 commit into
Draft
Add opt-in StrongEnums mode for strongly typed protobuf enums#1366wasimat404 wants to merge 1 commit into
wasimat404 wants to merge 1 commit into
Conversation
Adds a StrongEnums environment option (cel.feature.strong_enums) that treats protobuf enum values as distinct enum types rather than ints: enum constants, field selections, and conversions produce enum-typed values, with explicit int/string conversions per enum type. Legacy integer behavior is unchanged when the option is not enabled. Relates to cel-expr#450, cel-expr#815.
Collaborator
|
/gcbrun |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR adds an opt-in mode where protobuf enums are treated as their own types instead of plain integers. It follows the direction described in #450, where strongly typed enums with explicit conversions were mentioned as the long term goal. Also related to #815.
What it does when the option is enabled:
The mode is off by default. Nothing changes for existing users. It can be enabled with cel.StrongEnums(true) or through config files with the feature name cel.feature.strong_enums.
How it is built:
A new Enum value type carries the enum type name and the int32 number. The registry gets a strongEnums flag (same pattern as JSONFieldNames), and when it is on, EnumValue, FindIdent and field access return enum typed values. The checker takes the constant type from the value the provider returns instead of always using int, so legacy behavior is untouched when the flag is off. The conversion functions are registered as normal function declarations per enum type, so both checked and parse-only programs can use them.
Tests cover literals, type(), equality, field selection (defaults and unnamed numbers), conversions with range and name errors, message construction, parse-only evaluation, checker output types, and the legacy default, for both proto2 and proto3. All existing tests pass.
I am happy to adjust naming, the option surface, or the checker wiring if you prefer a different design.