Skip to content

Commit

Permalink
key variable with full expression
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 adead33 commit 6fa31ac
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions limitador/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ tonic = { version = "0.12.3", optional = true }
tonic-reflection = { version = "0.12.3", optional = true }
prost = { version = "0.13.3", optional = true }
prost-types = { version = "0.13.3", optional = true }
cel-parser = "0.8.0"
cel-interpreter = { version = "0.9.0", features = ["chrono", "regex"] }
cel-interpreter = { git = "https://github.com/clarkmcc/cel-rust", rev = "5b02b08", features = ["json", "regex", "chrono"] }
cel-parser = { git = "https://github.com/clarkmcc/cel-rust", rev = "5b02b08" }

[dev-dependencies]
serial_test = "3.0"
Expand Down
15 changes: 9 additions & 6 deletions limitador/src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,22 @@ mod tests {

#[test]
fn resolves_variables() {
let var = "timestamp(ts).getHours()";
let limit = Limit::new(
"",
10,
60,
Vec::default(),
["int(x) * 3".try_into().expect("failed parsing!")],
[var.try_into().expect("failed parsing!")],
);
let key = "x".to_string();
let counter = Counter::new(limit, HashMap::from([(key.clone(), "14".to_string())]))
.expect("failed creating counter");
let counter = Counter::new(
limit,
HashMap::from([("ts".to_string(), "2019-10-12T13:20:50.52Z".to_string())]),
)
.expect("failed creating counter");
assert_eq!(
counter.set_variables.get(&key),
Some("42".to_string()).as_ref()
counter.set_variables.get(var),
Some("13".to_string()).as_ref()
);
}
}
2 changes: 1 addition & 1 deletion limitador/src/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl Limit {
let ctx = Context::new(self, String::default(), &vars);
let mut map = BTreeMap::new();
for variable in &self.variables {
let name = variable.variables().concat().to_string();
let name = variable.source().into();
let value = variable.eval(&ctx)?;
map.insert(name, value);
}
Expand Down

0 comments on commit 6fa31ac

Please sign in to comment.