From c1e3221641918c034d22d05a471386600596ecfa Mon Sep 17 00:00:00 2001 From: Boris Zhguchev Date: Mon, 29 Apr 2024 22:59:26 +0200 Subject: [PATCH] Feature/63 quotes (#65) * add debug for Finder * add debug for Finder * add support for double qs * disable warning --------- Co-authored-by: Boris Zhguchev --- CHANGELOG.md | 55 ++++++++++++++++--------------- Cargo.toml | 2 +- src/lib.rs | 18 ++++++++-- src/parser/errors.rs | 1 + src/parser/grammar/json_path.pest | 2 +- src/parser/parser.rs | 4 +-- 6 files changed, 49 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d2eec56..0919803 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,45 +1,48 @@ * **`0.1.0`** - * Initial implementation + * Initial implementation * **`0.1.1`** - * Technical improvements + * Technical improvements * **`0.1.2`** - * added a trait to obtain the result from value - * added a method to get the cloned as Value - * change the name of the general method* + * added a trait to obtain the result from value + * added a method to get the cloned as Value + * change the name of the general method* * **`0.1.4`** - * add an ability to use references instead of values - * fix some clippy issues + * add an ability to use references instead of values + * fix some clippy issues * **`0.1.5`** - * correct grammar for `$.[..]` + * correct grammar for `$.[..]` * **`0.1.6`** - * add logical OR and logical And to filters - * fix bugs with objects in filters - * add internal macros to generate path objects + * add logical OR and logical And to filters + * fix bugs with objects in filters + * add internal macros to generate path objects * **`0.2.0`** - * add json path value as a result for the library - * add functions (size) - * change a logical operator `size` into function `size()` + * add json path value as a result for the library + * add functions (size) + * change a logical operator `size` into function `size()` * **`0.2.1`** - * changed the contract for length() function. + * changed the contract for length() function. * **`0.2.2`** - * add ..* + * add ..* * **`0.2.5`** - * build for tags + * build for tags * **`0.2.6`** - * make parser mod public + * make parser mod public * **`0.3.0`** - * introduce the different behaviour for empty results and non-existing result + * introduce the different behaviour for empty results and non-existing result * **`0.3.2`** - * make jsonpath inst cloneable. + * make jsonpath inst cloneable. * **`0.3.3`** - * fix a bug with the logical operators + * fix a bug with the logical operators * **`0.3.4`** - * add a result as a path + * add a result as a path * **`0.3.5`** - * add `!` negation operation in filters - * allow using () in filters + * add `!` negation operation in filters + * allow using () in filters * **`0.5`** - * add config for jsonpath - * add an option to add a regex cache for boosting performance + * add config for jsonpath + * add an option to add a regex cache for boosting performance +* **`0.5.1`** + * add double quotes for the expressions (before it was only possible to use single quotes) + * add Debug on the JsonPathFinder diff --git a/Cargo.toml b/Cargo.toml index 22854dc..5057e98 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "jsonpath-rust" description = "The library provides the basic functionality to find the set of the data according to the filtering query." -version = "0.5.0" +version = "0.5.1" authors = ["BorisZhguchev "] edition = "2018" license-file = "LICENSE" diff --git a/src/lib.rs b/src/lib.rs index e89b75d..7008164 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -120,7 +120,8 @@ use crate::path::config::JsonPathConfig; use crate::path::{json_path_instance, PathInstance}; use serde_json::Value; use std::convert::TryInto; -use std::fmt::Debug; +use std::fmt; +use std::fmt::{Debug, Formatter}; use std::ops::Deref; use std::str::FromStr; use JsonPathValue::{NewValue, NoValue, Slice}; @@ -167,7 +168,7 @@ pub trait JsonPathQuery { fn path(self, query: &str) -> Result; } -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct JsonPathInst { inner: JsonPath, } @@ -430,6 +431,17 @@ pub struct JsonPathFinder { cfg: JsonPathConfig, } +impl Debug for JsonPathFinder { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + let json_as_str = serde_json::to_string(&*self.json).map_err(|_| fmt::Error)?; + + f.write_str("JsonPathFinder:")?; + f.write_str(format!(" json:{}", json_as_str).as_str())?; + f.write_str(format!(" path:{:?}", self.path).as_str())?; + Ok(()) + } +} + impl JsonPathFinder { /// creates a new instance of [JsonPathFinder] pub fn new(json: Box, path: Box) -> Self { @@ -1191,7 +1203,7 @@ mod tests { let json: Box = Box::new(json!([{"verb": "TEST"},{"verb": "TEST"}, {"verb": "RUN"}])); let path: Box = Box::from( - JsonPathInst::from_str("$.[?(@.verb == 'RUN1')]").expect("the path is correct"), + JsonPathInst::from_str("$.[?(@.verb == \"RUN1\")]").expect("the path is correct"), ); let finder = JsonPathFinder::new(json, path); diff --git a/src/parser/errors.rs b/src/parser/errors.rs index aca5ce5..f171724 100644 --- a/src/parser/errors.rs +++ b/src/parser/errors.rs @@ -4,6 +4,7 @@ use thiserror::Error; use super::parser::Rule; #[derive(Error, Debug)] +#[allow(clippy::large_enum_variant)] pub enum JsonPathParserError<'a> { #[error("Failed to parse rule: {0}")] PestError(#[from] pest::error::Error), diff --git a/src/parser/grammar/json_path.pest b/src/parser/grammar/json_path.pest index 90a960e..5d2041e 100644 --- a/src/parser/grammar/json_path.pest +++ b/src/parser/grammar/json_path.pest @@ -10,7 +10,7 @@ word = _{ ('a'..'z' | 'A'..'Z')+ } specs = _{ "_" | "-" | "/" | "\\" | "#" } number = @{"-"? ~ ("0" | ASCII_NONZERO_DIGIT ~ ASCII_DIGIT*) ~ ("." ~ ASCII_DIGIT+)? ~ (^"e" ~ ("+" | "-")? ~ ASCII_DIGIT+)?} -string_qt = ${ "\'" ~ inner ~ "\'" } +string_qt = ${ ("\'" ~ inner ~ "\'") | ("\"" ~ inner ~ "\"") } inner = @{ char* } char = _{ !("\"" | "\\" | "\'") ~ ANY diff --git a/src/parser/parser.rs b/src/parser/parser.rs index b7d5c51..4155b7e 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -365,13 +365,13 @@ mod tests { test_failed("[]"); test("[-1,-2]", vec![path!(idx!(idx - 1, -2))]); test_failed("[abc,bcd]"); - test_failed("[\"abc\",\"bcd\"]"); + test("[\"abc\",\"bcd\"]", vec![path!(idx!("abc", "bcd"))]); } #[test] fn array_start_test() { test( - "$.[?(@.verb== 'TEST')]", + "$.[?(@.verb== \"TEST\")]", vec![ path!($), path!(idx!(?filter!(op!(chain!(path!(@,path!("verb")))),"==",op!("TEST")))),