google / cel-spec

Common Expression Language -- specification and binary representation
https://cel.dev
Apache License 2.0
2.67k stars 216 forks source link

Optional Values Support #246

Closed TristonianJones closed 1 year ago

TristonianJones commented 1 year ago

CEL supports presence testing using the has macro which can be useful for building ternary statements to provide alternative values during map, and protobuf object literals; however, there's no way to indicate that the value should be unset at the end of the computation. For example, it is possible to construct a protobuf message literal where the field is set to the default value, but once the field is referenced it cannot be marked as conditionally "unset" based on the result of the field initialization expression.

Optional values should leverage CEL's opaque types to be able to specify that a value with a strong type "might" exist, and if it does, to perform some sort of computation on it. This is akin to the feature requested in #245 where keys in a map are optionally set.

The following would indicate that if the event.user value is present, it should be set to the value of "user" in the map literal:

{
   ?"user": event.?user
}

This is roughly equivalent to the following:

{
  ?"user": has(event.user) ? optional.of(event.user) : optional.none()
}

The CEL grammar and type-checker would need to be updated to support this feature, and it should probably be opt-in as it expands the expressive power of CEL in ways that users may not have anticipated.

TristonianJones commented 1 year ago

See: https://github.com/google/cel-spec/wiki/proposal-246

zhqu1148980644 commented 1 year ago

when will this proposal be implemented in cel-go?

TristonianJones commented 1 year ago

@zhqu1148980644 some of the groundwork has been implemented in cel-go, but there are a couple more sizable chunks of work.

TristonianJones commented 1 year ago

@zhqu1148980644 The majority of the cel-go optional value implementation will be committed by the end of this week. The only features missing will be the macros, and they should follow in another week or so.