Skip to content

Add opt-in StrongEnums mode for strongly typed protobuf enums#1366

Draft
wasimat404 wants to merge 1 commit into
cel-expr:masterfrom
wasimat404:strong-enums
Draft

Add opt-in StrongEnums mode for strongly typed protobuf enums#1366
wasimat404 wants to merge 1 commit into
cel-expr:masterfrom
wasimat404:strong-enums

Conversation

@wasimat404

Copy link
Copy Markdown

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:

  • Enum constants like GlobalEnum.GAZ evaluate to a value of the enum's own type, and type() returns the fully qualified enum type.
  • Reading an enum field from a message gives an enum typed value. This also works for default values and for numbers that have no declared name.
  • Equality checks both the enum type and the number. An enum value is never equal to a plain int or to a value of a different enum type.
  • Conversions are explicit. int(e) gives the underlying number. Each enum type can be called as a conversion function: MyEnum(n) accepts any number that fits in int32 (an error otherwise), and MyEnum('NAME') looks up the value by name (an error if the name does not exist).
  • The type checker agrees with all of the above, and message construction accepts enum typed values.
  • Works the same for proto2 and proto3.

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.

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.
@TristonianJones

Copy link
Copy Markdown
Collaborator

/gcbrun

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants