Skip to content

Commit

Permalink
Inject Limit id & name into Context
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Snaps <[email protected]>
  • Loading branch information
alexsnaps committed Nov 27, 2024
1 parent 80c994d commit f1e9c2c
Showing 1 changed file with 61 additions and 10 deletions.
71 changes: 61 additions & 10 deletions limitador/src/limit/cel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,24 @@ impl Context<'_> {
ctx.add_variable_from_value(root, Value::Map(map));
}

let limit_data = cel_interpreter::objects::Map::from(HashMap::from([(
"name",
limit
.name
.as_ref()
.map(|n| Value::String(Arc::new(n.to_string())))
.unwrap_or(Value::Null),
)]));
let limit_data = cel_interpreter::objects::Map::from(HashMap::from([
(
"name",
limit
.name
.as_ref()
.map(|n| Value::String(Arc::new(n.to_string())))
.unwrap_or(Value::Null),
),
(
"id",
limit
.id
.as_ref()
.map(|n| Value::String(Arc::new(n.to_string())))
.unwrap_or(Value::Null),
),
]));
ctx.add_variable_from_value("limit", Value::Map(limit_data));

Self {
Expand Down Expand Up @@ -218,7 +228,12 @@ impl Predicate {
}

pub fn test(&self, ctx: &Context) -> Result<bool, EvaluationError> {
if !self.variables.iter().all(|v| ctx.variables.contains(v)) {
if !self
.variables
.iter()
.filter(|binding| binding.as_str() != "limit")
.all(|v| ctx.variables.contains(v))
{
return Ok(false);
}
match self.expression.resolve(ctx)? {
Expand Down Expand Up @@ -279,7 +294,8 @@ impl From<Predicate> for String {
#[cfg(test)]
mod tests {
use super::{Context, Expression, Predicate};
use std::collections::HashSet;
use crate::limit::Limit;
use std::collections::{HashMap, HashSet};

#[test]
fn expression() {
Expand Down Expand Up @@ -320,6 +336,41 @@ mod tests {
);
}

#[test]
fn context_has_limit_info() {
let mut limit = Limit::new(
"ns",
42,
10,
vec!["limit.name == 'named_limit'"],

Check warning on line 345 in limitador/src/limit/cel.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

Diff in /home/runner/work/limitador/limitador/limitador/src/limit/cel.rs
Vec::<String>::default(),
)
.expect("failed to create");
assert!(!limit.applies(&HashMap::default()));
limit.set_name("named_limit".to_string());
assert!(limit.applies(&HashMap::default()));
let limit = Limit::with_id(
"my_id",
"ns",
42,
10,
vec!["limit.id == 'my_id'"],

Check warning on line 357 in limitador/src/limit/cel.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

Diff in /home/runner/work/limitador/limitador/limitador/src/limit/cel.rs
Vec::<String>::default(),
)
.expect("failed to create");
assert!(limit.applies(&HashMap::default()));
let limit = Limit::with_id(
"my_id",
"ns",
42,
10,
vec!["limit.id == 'other_id'"],

Check warning on line 367 in limitador/src/limit/cel.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

Diff in /home/runner/work/limitador/limitador/limitador/src/limit/cel.rs
Vec::<String>::default(),
)
.expect("failed to create");
assert!(!limit.applies(&HashMap::default()));
}

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

0 comments on commit f1e9c2c

Please sign in to comment.