Skip to content

Commit

Permalink
Feature/63 quotes (#65)
Browse files Browse the repository at this point in the history
* add debug for Finder

* add debug for Finder

* add support for double qs

* disable warning

---------

Co-authored-by: Boris Zhguchev <[email protected]>
  • Loading branch information
besok and Boris Zhguchev authored Apr 29, 2024
1 parent fc6964f commit c1e3221
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 33 deletions.
55 changes: 29 additions & 26 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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


2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
edition = "2018"
license-file = "LICENSE"
Expand Down
18 changes: 15 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -167,7 +168,7 @@ pub trait JsonPathQuery {
fn path(self, query: &str) -> Result<Value, String>;
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct JsonPathInst {
inner: JsonPath,
}
Expand Down Expand Up @@ -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<Value>, path: Box<JsonPathInst>) -> Self {
Expand Down Expand Up @@ -1191,7 +1203,7 @@ mod tests {
let json: Box<Value> =
Box::new(json!([{"verb": "TEST"},{"verb": "TEST"}, {"verb": "RUN"}]));
let path: Box<JsonPathInst> = 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);

Expand Down
1 change: 1 addition & 0 deletions src/parser/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Rule>),
Expand Down
2 changes: 1 addition & 1 deletion src/parser/grammar/json_path.pest
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")))),
Expand Down

0 comments on commit c1e3221

Please sign in to comment.