Skip to content

Commit

Permalink
fix logic operations (> and >=) (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
besok authored Oct 26, 2023
1 parent 6d5b0e5 commit 8a3457e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@
* **`0.3.0`**
* introduce the different behaviour for empty results and non-existing result
* **`0.3.2`**
* make jsonpath inst cloneable
* make jsonpath inst cloneable.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,8 @@ pub enum JsonPathIndex {
### How to contribute

TBD

### How to update version
- update files
- add tag `git tag -a v<Version> -m "message"`
- git push origin <tag_name>
27 changes: 26 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ impl JsonPathFinder {
#[cfg(test)]
mod tests {
use crate::JsonPathQuery;
use crate::JsonPathValue::{NoValue, Slice};
use crate::JsonPathValue::{NewValue, NoValue, Slice};
use crate::{json_path_value, JsonPathFinder, JsonPathInst, JsonPathValue};
use serde_json::{json, Value};
use std::ops::Deref;
Expand Down Expand Up @@ -1071,6 +1071,31 @@ mod tests {
assert_eq!(v.deref(), &json!("Sayings of the Century"));
}

#[test]
fn logical_exp_test() {
let json: Box<Value> =
Box::new(json!({"first":{"second":[{"active":1},{"passive":1}]}}));

let path: Box<JsonPathInst> = Box::from(
JsonPathInst::from_str("$.first[?(@.does_not_exist && @.does_not_exist >= 1.0)]")
.expect("the path is correct"),
);
let finder = JsonPathFinder::new(json.clone(), path);

let v = finder.find_slice();
assert_eq!(v, vec![NoValue]);


let path: Box<JsonPathInst> = Box::from(
JsonPathInst::from_str("$.first[?(@.does_not_exist >= 1.0)]")
.expect("the path is correct"),
);
let finder = JsonPathFinder::new(json, path);

let v = finder.find_slice();
assert_eq!(v, vec![NoValue]);
}

// #[test]
// fn no_value_len_field_test() {
// let json: Box<Value> =
Expand Down
42 changes: 36 additions & 6 deletions src/path/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,11 @@ impl<'a> FilterPath<'a> {
FilterSign::LeOrEq => {
FilterPath::compound(&FilterSign::Less, &FilterSign::Equal, left, right)
}
FilterSign::Greater => !FilterPath::process_atom(&FilterSign::LeOrEq, left, right),
FilterSign::GrOrEq => !FilterPath::process_atom(&FilterSign::Less, left, right),
FilterSign::Greater => less(
JsonPathValue::into_data(right),
JsonPathValue::into_data(left),
),
FilterSign::GrOrEq => FilterPath::compound(&FilterSign::Greater, &FilterSign::Equal, left, right),
FilterSign::Regex => regex(
JsonPathValue::into_data(left),
JsonPathValue::into_data(right),
Expand Down Expand Up @@ -485,23 +488,50 @@ mod tests {
"key":[
{"field":1},
{"field":10},
{"field":4},
{"field":5},
{"field":1},
]
});
let exp1 = json!( {"field":10});
let exp2 = json!( {"field":5});
let exp3 = json!( {"field":4});
let exp4 = json!( {"field":1});


let index = path!(
idx!(?filter!(op!(path!(@, path!("field"))), ">", op!(chain!(path!($), path!("threshold")))))
);
let chain = chain!(path!($), path!("key"), index);
let path_inst = json_path_instance(&chain, &json);
let expected_res = json_path_value![&exp1, &exp2];
assert_eq!(path_inst.find((&json).into()), expected_res);


let index = path!(
idx!(?filter!(op!(path!(@, path!("field"))), ">=", op!(chain!(path!($), path!("threshold")))))
);
let chain = chain!(path!($), path!("key"), index);
let path_inst = json_path_instance(&chain, &json);
let expected_res = json_path_value![&exp1, &exp3, &exp2];
assert_eq!(path_inst.find((&json).into()), expected_res);

let index = path!(
idx!(?filter!(op!(path!(@, path!("field"))), "<", op!(chain!(path!($), path!("threshold")))))
);
let chain = chain!(path!($), path!("key"), index);
let path_inst = json_path_instance(&chain, &json);
let expected_res = json_path_value![&exp4, &exp4];
assert_eq!(path_inst.find((&json).into()), expected_res);

let exp1 = json!( {"field":10});
let exp2 = json!( {"field":5});
let expected_res = json_path_value![&exp1, &exp2];
assert_eq!(path_inst.find((&json).into()), expected_res)

let index = path!(
idx!(?filter!(op!(path!(@, path!("field"))), "<=", op!(chain!(path!($), path!("threshold")))))
);
let chain = chain!(path!($), path!("key"), index);
let path_inst = json_path_instance(&chain, &json);
let expected_res = json_path_value![&exp4,&exp3, &exp4];
assert_eq!(path_inst.find((&json).into()), expected_res);
}

#[test]
Expand Down

0 comments on commit 8a3457e

Please sign in to comment.