From 9fe7d80f8f246925f6dd3636e70fba8e72a42bd3 Mon Sep 17 00:00:00 2001 From: Alex Snaps Date: Wed, 27 Nov 2024 17:29:06 -0500 Subject: [PATCH] key variable with full expression Signed-off-by: Alex Snaps --- Cargo.lock | 8 ++++---- limitador/Cargo.toml | 4 ++-- limitador/src/counter.rs | 15 +++++++++------ limitador/src/limit.rs | 2 +- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6ed6c974..3b5909bf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -622,23 +622,23 @@ dependencies = [ [[package]] name = "cel-interpreter" version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5675bdc8ece076b0a4f5ac6c20724e7373919ed698e00dbf33825682a7b3a679" +source = "git+https://github.com/clarkmcc/cel-rust?rev=5b02b08#5b02b0817ced05c7cfc1c72bab03bc97bbfa2dea" dependencies = [ + "base64 0.22.1", "cel-parser", "chrono", "nom", "paste", "regex", "serde", + "serde_json", "thiserror 1.0.69", ] [[package]] name = "cel-parser" version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e1df6c220727ff1f7b52a41699d8c71ec52e8ae58d01a9768469a916e1f22eb" +source = "git+https://github.com/clarkmcc/cel-rust?rev=5b02b08#5b02b0817ced05c7cfc1c72bab03bc97bbfa2dea" dependencies = [ "lalrpop", "lalrpop-util", diff --git a/limitador/Cargo.toml b/limitador/Cargo.toml index fdaaf03a..ed822b1d 100644 --- a/limitador/Cargo.toml +++ b/limitador/Cargo.toml @@ -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" diff --git a/limitador/src/counter.rs b/limitador/src/counter.rs index 2c5494bf..7dfebe0b 100644 --- a/limitador/src/counter.rs +++ b/limitador/src/counter.rs @@ -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() ); } } diff --git a/limitador/src/limit.rs b/limitador/src/limit.rs index a67af038..5ad0c56e 100644 --- a/limitador/src/limit.rs +++ b/limitador/src/limit.rs @@ -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); }