Skip to content

Commit

Permalink
Don’t change the simple key encoding so that redis keys don’t change.
Browse files Browse the repository at this point in the history
  • Loading branch information
chirino committed Jun 5, 2024
1 parent 5007c15 commit c063001
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
33 changes: 25 additions & 8 deletions limitador/src/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::fmt::{Debug, Display, Formatter};
use std::hash::{Hash, Hasher};

use cel_interpreter::{Context, Expression, Value};
use cel_parser::{Atom, parse};
use cel_parser::{Atom, parse, RelationOp};
use cel_parser::RelationOp::{Equals, NotEquals};
use serde::{Deserialize, Serialize, Serializer};
#[cfg(feature = "lenient_conditions")]
Expand Down Expand Up @@ -120,7 +120,7 @@ impl TryFrom<&str> for Condition {
impl Condition {

fn try_from_cel(source: String) -> Result<Self, ConditionParsingError> {
match parse(&source) {
match parse(&source.strip_prefix("cel:").unwrap().to_string()) {
Ok(expression) => Ok(Condition { source, expression }),
Err(_err) => Err(ConditionParsingError {
error: SyntaxError {
Expand All @@ -133,6 +133,23 @@ impl Condition {
}
}

fn simple_source(var_name: String, op: RelationOp, lit: String) -> String {
let predicate = match op {
Equals => "==",
NotEquals => "!=",
_ => unreachable!(),
};
let quotes = if lit.contains('"') {
'\''
} else {
'"'
};
format!(
"{} {} {}{}{}",
var_name, predicate, quotes, lit, quotes
)
}

fn try_from_simple(source: String) -> Result<Self, ConditionParsingError> {
match conditions::Scanner::scan(source.clone()) {
Ok(tokens) => match tokens.len().cmp(&(3_usize)) {
Expand All @@ -158,7 +175,7 @@ impl Condition {
_ => unreachable!(),
};
Ok(Condition {
source,
source: Condition::simple_source(var_name.clone(), predicate.clone(), operand.clone()),
expression: Expression::Relation(
Box::new(Expression::Ident(var_name.clone().into())),
predicate,
Expand Down Expand Up @@ -187,7 +204,7 @@ impl Condition {
_ => unreachable!(),
};
Ok(Condition {
source,
source: Condition::simple_source(var_name.clone(), predicate.clone(), operand.clone()),
expression: Expression::Relation(
Box::new(Expression::Atom(Atom::String(operand.clone().into()))),
predicate,
Expand All @@ -209,7 +226,7 @@ impl Condition {
{
deprecated::deprecated_syntax_used();
Ok(Condition {
source,
source: Condition::simple_source(var_name.clone(), Equals, operand.clone()),
expression: Expression::Relation(
Box::new(Expression::Ident(var_name.clone().into())),
Equals,
Expand All @@ -231,11 +248,11 @@ impl Condition {
{
deprecated::deprecated_syntax_used();
Ok(Condition {
source,
source: Condition::simple_source(var_name.clone(), Equals, operand.to_string()),
expression: Expression::Relation(
Box::new(Expression::Ident(var_name.clone().into())),
Equals,
Box::new(Expression::Atom(Atom::String(operand.clone().into()))),
Box::new(Expression::Atom(Atom::String(operand.to_string().into()))),
),
})
} else {
Expand Down Expand Up @@ -295,7 +312,7 @@ impl TryFrom<String> for Condition {

fn try_from(value: String) -> Result<Self, Self::Error> {
if value.clone().starts_with("cel:") {
return Condition::try_from_cel(value.strip_prefix("cel:").unwrap().to_string());
return Condition::try_from_cel(value);
}
return Condition::try_from_simple(value);
}
Expand Down
2 changes: 1 addition & 1 deletion limitador/src/storage/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ mod tests {
vec!["app_id"],
);
assert_eq!(
"namespace:{example.com},counters_of_limit:{\"namespace\":\"example.com\",\"seconds\":60,\"conditions\":[\"req.method == 'GET'\"],\"variables\":[\"app_id\"]}",
"namespace:{example.com},counters_of_limit:{\"namespace\":\"example.com\",\"seconds\":60,\"conditions\":[\"req.method == \\\"GET\\\"\"],\"variables\":[\"app_id\"]}",
key_for_counters_of_limit(&limit))
}

Expand Down

0 comments on commit c063001

Please sign in to comment.