Safe Evaluation Mode #425
Replies: 3 comments 1 reply
-
Instead of replacing unresolvable attributes with |
Beta Was this translation helpful? Give feedback.
-
I guess Optionals (proposal 246) are enough for handling the described problem. If so please feel free to close the discussion. |
Beta Was this translation helpful? Give feedback.
-
Hi @salvalcantara, It may not seem like it, but errors are the only safe way to handle this scenario for dynamic types. It's never clear whether the undefined field referenced is intentionally absent. Instead of coalescing to Whether an error is fail open or fail close is also an application decision that's beyond the purview of what CEL understands. But, in general, yes, the optionals are a better way to state intentionally undefined fields in a dynamic object -Tristan |
Beta Was this translation helpful? Give feedback.
-
Problem Statement
Examples
For the following JSON payload:
I want these expressions to evaluate to
false
, for example:As of now, all will fail due to missing fields. An obvious solution is to make the expressions safe beforehand like this:
But that's precisely what I want to avoid.
Proposed Solution: Safe Evaluation Mode
The main idea would be to replace unresolvable attributes with
null
.For
a.b == 1
that would give usnull == 1
which would evaluate tofalse
as desired.The problem is when you have something more like this
a.b > 1
because that would be transformed intonull > 1
and then evaluation would fail with ano such overload
error due to the null type not supporting the>
operator. The same might happen for other operators/functions. To account for this, the interpreter could consider an evaluation type of middleware which simply inspects if there are any (non-)errors like those due to missing fields and if that's the case simply returnfalse
.Beta Was this translation helpful? Give feedback.
All reactions