feat: Added not equl operator in jsonpath predicate

This commit is contained in:
sandeshbhusal 2024-10-03 21:30:28 -06:00 committed by hurl-bot
parent 625a0a3aad
commit 3b843238a2
No known key found for this signature in database
GPG Key ID: 1283A2B4A0DCAF8D
5 changed files with 71 additions and 3 deletions

View File

@ -56,7 +56,9 @@ pub enum PredicateFunc {
KeyExist, KeyExist,
EqualBool(bool), EqualBool(bool),
EqualString(String), EqualString(String),
NotEqualString(String),
Equal(Number), Equal(Number),
NotEqual(Number),
GreaterThan(Number), GreaterThan(Number),
GreaterThanOrEqual(Number), GreaterThanOrEqual(Number),
LessThan(Number), LessThan(Number),

View File

@ -172,6 +172,12 @@ impl Predicate {
(serde_json::Value::String(v), PredicateFunc::EqualString(ref s)) => { (serde_json::Value::String(v), PredicateFunc::EqualString(ref s)) => {
v == *s v == *s
} }
(serde_json::Value::String(v), PredicateFunc::NotEqualString(ref s)) => {
v != *s
}
(serde_json::Value::Number(v), PredicateFunc::NotEqual(ref num)) => {
!approx_eq!(f64, v.as_f64().unwrap(), num.to_f64(), ulps = 2)
}
(serde_json::Value::Bool(v), PredicateFunc::EqualBool(ref s)) => v == *s, (serde_json::Value::Bool(v), PredicateFunc::EqualBool(ref s)) => v == *s,
_ => false, _ => false,
} }

View File

@ -31,7 +31,9 @@ predicate-key = "@." key-name
predicate-func = key-exist-predicate-func predicate-func = key-exist-predicate-func
| equal-string-predicate-func | equal-string-predicate-func
| notequal-string-predicate-func
| equal-number-predicate-func | equal-number-predicate-func
| notequal-number-predicate-func
| greater-than-predicate-func | greater-than-predicate-func
| greater-or-equal-than-predicate-func | greater-or-equal-than-predicate-func
@ -40,6 +42,10 @@ equal-string-predicate-func = "=" string-value
equal-number-predicate-func- = "=" number equal-number-predicate-func- = "=" number
notequal-string-predicate-func = "!=" string-value
notequal-number-predicate-func = "!=" number
# #
# Primitives # Primitives
@ -50,6 +56,3 @@ key-name = <alpha + "-" + "_">
string-value = "'" <alphanum> "'" string-value = "'" <alphanum> "'"
number = <floating-point number> number = <floating-point number>

View File

@ -232,6 +232,8 @@ fn predicate_func(reader: &mut Reader) -> ParseResult<PredicateFunc> {
less_than_or_equal_predicate_func, less_than_or_equal_predicate_func,
equal_boolean_predicate_func, equal_boolean_predicate_func,
equal_string_predicate_func, equal_string_predicate_func,
notequal_string_predicate_func,
notequal_number_func
], ],
reader, reader,
) )
@ -286,6 +288,20 @@ fn equal_string_predicate_func(reader: &mut Reader) -> ParseResult<PredicateFunc
Ok(PredicateFunc::EqualString(s)) Ok(PredicateFunc::EqualString(s))
} }
fn notequal_string_predicate_func(reader: &mut Reader) -> ParseResult<PredicateFunc> {
try_literal("!=", reader)?;
whitespace(reader);
let s = string_value(reader)?;
Ok(PredicateFunc::NotEqualString(s))
}
fn notequal_number_func(reader: &mut Reader) -> ParseResult<PredicateFunc> {
try_literal("!=", reader)?;
whitespace(reader);
let num = number(reader)?;
Ok(PredicateFunc::NotEqual(num))
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use hurl_core::reader::Pos; use hurl_core::reader::Pos;
@ -637,6 +653,24 @@ mod tests {
); );
assert_eq!(reader.cursor().index, 9); assert_eq!(reader.cursor().index, 9);
let mut reader = Reader::new("!='hello'");
assert_eq!(
predicate_func(&mut reader).unwrap(),
PredicateFunc::NotEqualString("hello".to_string())
);
let mut reader = Reader::new("!=2");
assert_eq!(
predicate_func(&mut reader).unwrap(),
PredicateFunc::NotEqual(Number { int: 2, decimal: 0 })
);
let mut reader = Reader::new("!=2.5");
assert_eq!(
predicate_func(&mut reader).unwrap(),
PredicateFunc::NotEqual(Number { int: 2, decimal: 500_000_000_000_000_000 })
);
let mut reader = Reader::new(">5"); let mut reader = Reader::new(">5");
assert_eq!( assert_eq!(
predicate_func(&mut reader).unwrap(), predicate_func(&mut reader).unwrap(),

View File

@ -253,6 +253,29 @@ fn test_bookstore_path() {
JsonpathResult::Collection(vec![book0_value(), book2_value()]) JsonpathResult::Collection(vec![book0_value(), book2_value()])
); );
// get all books whose title is not "hamlet".
let expr = jsonpath::parse("$..book[?(@.title!='Moby Dick')]").unwrap();
assert_eq!(
expr.eval(&bookstore_value()).unwrap(),
JsonpathResult::Collection(vec![
book0_value(),
book1_value(),
book3_value()
])
);
// get all books whose price is not 8.95 (first book)
let expr = jsonpath::parse("$..book[?(@.price!=8.95)]").unwrap();
assert_eq!(
expr.eval(&bookstore_value()).unwrap(),
JsonpathResult::Collection(vec![
book1_value(),
book2_value(),
book3_value()
])
);
// All members of JSON structure // All members of JSON structure
let expr = jsonpath::parse("$..*").unwrap(); let expr = jsonpath::parse("$..*").unwrap();
// Order is reproducible // Order is reproducible