google / cel-spec

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

Merging maps #245

Closed alexec closed 2 years ago

alexec commented 2 years ago

Consider this CeL expression:

    {
      "specversion": "1.0",
      "source": "intuit",
      "id": event.source+"/"+event.id,
      "type": event.type,
      "subject": has(event.subject) ? event.subject : "",
      "intuitaccountid": has(event.intuitaccountid) ? event.intuitaccountid : "",
      "datacontentype": "application/json",
      "data": event.data
    }

You'll see that it maps an input event to an output event (CloudEvent to be specific).

Some of the fields in the input event are optional and may not exist. Because CeL does not have a nill value, I insert empty strings. But really, I want those missing input fields to also be missing output fields. Essentially, I think I want to merge maps.

    merge({
      "specversion": "1.0",
      "source": "intuit",
      "id": event.source+"/"+event.id,
      "type": event.type,
      "datacontentype": "application/json",
      "data": event.data
    },  
    has(event.subject) ? {"subject": event.subject} : {}, 
    has(event.intuitaccoutid) ? {"intuitaccountid": event.intuitaccountid} : {}
     )
TristonianJones commented 2 years ago

@alexec I have a design forthcoming for optional values as a canonical extension of CEL. Give me a bit of time to put together the design doc in wiki form, but you can expect to see development in this direction in both the cel-go and cel-cpp repos.

The exact syntax will look as follows:

{
      "specversion": "1.0",
      "source": "intuit",
      "id": event.source+"/"+event.id,
      "type": event.type,
      "datacontentype": "application/json",
      "data": event.data,
       // the following statements are equivalent to:
       // "subject" = has(event.subject) ? optional.of(event.subject) : optional.none()
       // where a value of optional.none() will ensure the value is unset in the result.
       ?"subject": event.?subject,
       ?"intuitaccountid": event.?intiuitaccountid,
}
TristonianJones commented 2 years ago

I'm going to close this issue for now since it should be captured in #246 with the details present in https://github.com/google/cel-spec/wiki/proposal-246