undistro / cel-playground

CEL Playground provides a simple and user-friendly interface to write and quickly evaluate CEL expressions.
https://playcel.undistro.io
Apache License 2.0
118 stars 20 forks source link

Can't compile expression that return objects with mixed properties types #98

Open sgendre opened 1 week ago

sgendre commented 1 week ago

Question

Hi, I am playing with playground to scope what is achievable or not with CEL and to spot what is valid CEL syntax or not.

Further Information

I try to implement an expression that return an object base on name This example is for simplicity purpose only. input: person: name: 'sam'

expression KO person.name == 'sam' ? { 'status': 'adult', 'age': 39} : { 'status': 'unknown', 'age': 0} is failing to compile

but as soon as I change "age" and "adult" properties to string expression OKperson.name == 'sam' ? { "status": "adult", "age": "39"} : { "status": "adult", "age": "0"} it compiles and is evaluated

and even inconsistent object shape return is fine as long as all properties does have the same type expression OK2 person.name == 'sam' ? { 'status': 'adult', 'age': '39'} : { 'status': 'unknown'}

Why is there such constraint?

You can play directly with
https://playcel.undistro.io/?content=H4sIAAAAAAAAA51TTUvDQBD9K2M8RKG2gicDRUQKflQrioiQy9hM08Xd2bAfrUX6351Ng6WHipgc8rIz897bmd2vbEo6K7LBAF5JT60hCBbCnOBqNIZHjava2cjVQcmSsrsGygMyKA7kcBrUgoTj%2FdJ7Mu96BY1dkqMKiBfKWTbEIXHTZ6OtI6msEian2shShflG1xpjGUafjSPvlcAxch2xJjgS%2FeN%2BctK6OYFXpwLBykbXWqNtzVyku6QXTy0xOkKQUMJeVQQz68R8EwNUGLAnGN4u78cgy7fPk4cUNxg6lsfEDPlT5LzdxQJ1xD3iWKNiH1qlrUBHNOr2n%2BqmVmuSzkmNnQkDmkaT74z5RjlMsbJ9pVjG0yjdrsHdpGRpnrfcZ5SpDYeQezQ5XMCXoIAh%2BryAHKuoQ94TUJP8n52vodjNiPzBdsnbnFNYd4KTu%2FT5u8xe7v8S%2FnjKz85%2FYc96WerwTeq1HOZDuJbxp%2BO5O4E9E%2B4ncxtPRckgT3JWbHyVLOTGViS86aqsvwHRXEQqMgMAAA%3D%3D

Screenshots

image

System Information

Thank for your time or for that online tool, it is so helpful to ramp-up 👍

matheusfm commented 6 days ago

Hi @sgendre, thank you for opening this issue!

This expression is failing because CEL Playground disables mixed types for lists and maps. It's being explicitly disabled by setting cel.HomogeneousAggregateLiterals() and cel.ValidateHomogeneousAggregateLiterals() options.

These options are set to follow the same configuration as Kubernetes.

Note, it's still possible to have mixed types when provided as input variable (right text area).

You can also define your CEL environment without these options. Here’s a version without them: https://heterogeneous-aggregates.cel-playground.pages.dev/ Available starting from this PR: https://github.com/undistro/cel-playground/pull/99.