besok / jsonpath-rust

Support for json-path in Rust
https://crates.io/crates/jsonpath-rust
MIT License
104 stars 27 forks source link

Added negate operator '!' #54

Closed catalinstochita closed 1 year ago

catalinstochita commented 1 year ago

Hi. I'm using this library at work along with a java library too. We have some filters that are using the '!' to negate the expression and is not supported in rust. I have made the changes and it seems to works. I would appreciate it if you could review my implementation. Thanks

besok commented 1 year ago

Great to hear the library is useful. Thank you for the PR.

besok commented 1 year ago

closes #55

catalinstochita commented 1 year ago

Looks like this needs more work.

let json: Box<Value> = Box::new(json!({"first":{"second":{"active":1}}}));
let path: Box<JsonPathInst> = Box::from(
    JsonPathInst::from_str("$.first[?(!(@.second.active == 1) || @.second.active == 1)]")
        .expect("the path is correct"),
);
let finder = JsonPathFinder::new(json, path);
let v = finder.find_slice();
assert_eq!(v, vec![Slice(&json!({"second":{"active": 1}}), "$.['first']".to_string())]);
assertion `left == right` failed
left: [NoValue]
right: [Slice(Object {"second": Object {"active": Number(1)}}, "$.['first']")]

The negate evaluates only the first expression, we need to be able to chain them

besok commented 1 year ago

Thanks for the changes but There is a fail in rust fmt. Just kick off the following command:

cargo fmt --all -- --check

locally and reformat the files or the whole project using rustfmt if it is possible.