Skip to content

Commit

Permalink
Support for list values as binding to cel::Context
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Snaps <alex@wcgw.dev>
  • Loading branch information
alexsnaps committed Dec 3, 2024
1 parent ef45382 commit 345be88
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions limitador/src/limit/cel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ impl<'a> Context<'a> {
}
}

pub fn list_binding(&mut self, name: String, value: Vec<HashMap<String, String>>) {
let v = value
.iter()
.map(|values| {
let map = cel_interpreter::objects::Map::from(values.clone());
Value::Map(map)
})
.collect::<Vec<_>>();
self.variables.insert(name.clone());
self.ctx
.add_variable_from_value(name, Value::List(v.into()));
}

pub(crate) fn for_limit<'b>(&'b self, limit: &Limit) -> Self
where
'b: 'a,
Expand Down Expand Up @@ -412,6 +425,21 @@ mod tests {
);
}

#[test]
fn supports_list_bindings() {
let pred = Predicate::parse("root[0].key == '1' && root[1]['key'] == '2'")
.expect("failed to parse");
let mut ctx = Context::default();
ctx.list_binding(
"root".to_string(),
vec![
HashMap::from([("key".to_string(), "1".to_string())]),
HashMap::from([("key".to_string(), "2".to_string())]),
],
);
assert_eq!(pred.test(&ctx).map_err(|e| format!("{e}")), Ok(true));
}

fn ctx<'a>() -> Context<'a> {
Context {
variables: HashSet::default(),
Expand Down

0 comments on commit 345be88

Please sign in to comment.