You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
let program = Program::compile("1 + numbers[0]").unwrap();letmut context = Context::default();// This could also come directly from Serde in a form of e.g. HashMap<String, Value> let numbers:Vec<serde_json::Value> = vec![2.into()];
context.add_variable("numbers", numbers).unwrap();let value = program.execute(&context).unwrap();
Expected return value: 3.
Actual result: Result::unwrap() on an Err value: UnsupportedBinaryOperator("add", Int(1), UInt(2))
Somehow serde_json::Value::Number is converted into cel_interpreter::Value::UInt by default, which
is quite unhelpful. Not sure how to work around it.
v0.9.0
The text was updated successfully, but these errors were encountered:
I'm looking at this in #123. The main problem with implicit casting between types (uint/int) for binary operations (add/sub/mul/div) is what happens when there's an overflow? Another question is, what is returned if I do uint + int, is it a uint? What if the int is negative and larger than the uint? You have a situation where the type of the result of the binary operation is dependent on the values of the inputs, not the types of the inputs.
Note that currently there are no automatic arithmetic conversions for the numeric types (int, uint, and double). The arithmetic operators typically contain overloads for arguments of the same numeric type, but not for mixed-type arguments. Therefore an expression like 1 + 1u is going to fail to dispatch. To perform mixed-type arithmetic, use explicit conversion functions such as uint(1) + 1u. Such explicit conversions will maintain their meaning even if arithmetic conversions are added in the future.
So the only other thing we could look at is why a JSON value by default serializes to a uint rather than an int which would probably solve the surface level issue you're facing, without addressing the underlying cause of your problem (binary ops across types).
No questions to inability to add int to uint, all according to spec. But it'd really help to cast serde's Number into Int (just as CEL does with number literals by default), unless that would violate the int type.
Consider this snippet
Expected return value:
3
.Actual result:
Result::unwrap()
on anErr
value:UnsupportedBinaryOperator("add", Int(1), UInt(2))
Somehow
serde_json::Value::Number
is converted intocel_interpreter::Value::UInt
by default, whichis quite unhelpful. Not sure how to work around it.
v0.9.0
The text was updated successfully, but these errors were encountered: