Skip to content

Commit b770d2c

Browse files
fix(python): boolean mapping
1 parent 1c607dc commit b770d2c

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

bindings/python/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ fn from(ob: &Bound<'_, PyAny>) -> Result<Value, PyErr> {
8585
else if let Ok(s) = ob.extract::<String>() {
8686
s.into()
8787
}
88+
// Boolean
89+
else if let Ok(b) = ob.extract::<bool>() {
90+
b.into()
91+
}
8892
// Numeric
8993
else if let Ok(v) = ob.extract::<i64>() {
9094
v.into()
@@ -93,10 +97,6 @@ fn from(ob: &Bound<'_, PyAny>) -> Result<Value, PyErr> {
9397
} else if let Ok(v) = ob.extract::<f64>() {
9498
v.into()
9599
}
96-
// Boolean
97-
else if let Ok(b) = ob.extract::<bool>() {
98-
b.into()
99-
}
100100
// None
101101
else if ob.downcast::<PyNone>().is_ok() {
102102
Value::Null

bindings/python/test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,3 +387,29 @@ def modify_set(st):
387387
assert st == {10, 12}, f"Unexpected set: {st}"
388388

389389
test_extension_types()
390+
391+
def test_boolean_mapping():
392+
rego = regorus.Engine()
393+
rego.add_policy("demo",
394+
"""
395+
package demo
396+
397+
result_b := data.a if {
398+
data.a == true
399+
}
400+
401+
result_i := data.b if {
402+
data.b == 1
403+
}
404+
""")
405+
rego.add_data({"a": True, "b": 1})
406+
407+
result_b = rego.eval_rule("data.demo.result_b")
408+
assert isinstance(result_b, bool), f"Expected bool, got {type(result_b)}"
409+
assert result_b, f"Unexpected result for 'result_b': {result_b}"
410+
411+
result_i = rego.eval_rule("data.demo.result_i")
412+
assert isinstance(result_i, float), f"Expected float, got {type(result_i)}"
413+
assert result_i == 1, f"Unexpected result for 'result_i': {result_i}"
414+
415+
test_boolean_mapping()

0 commit comments

Comments
 (0)