Skip to content

fix #92 #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub(super) struct JSPathParser;
const MAX_VAL: i64 = 9007199254740991; // Maximum safe integer value in JavaScript
const MIN_VAL: i64 = -9007199254740991; // Minimum safe integer value in JavaScript

pub(super) type Parsed<T> = Result<T, JsonPathError>;
pub type Parsed<T> = Result<T, JsonPathError>;

/// Parses a string into a [JsonPath].
///
Expand Down
2 changes: 1 addition & 1 deletion src/parser/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::str::ParseBoolError;
use thiserror::Error;

/// This error type is used to represent errors that can occur during the parsing of JSONPath expressions.
#[derive(Error, Debug, PartialEq)]
#[derive(Error, Debug, PartialEq, Clone)]
pub enum JsonPathError {
#[error("Failed to parse rule: {0}")]
PestError(#[from] Box<pest::error::Error<Rule>>),
Expand Down
41 changes: 37 additions & 4 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ mod test;
mod test_function;

use crate::parser::errors::JsonPathError;
use crate::parser::parse_json_path;
use crate::parser::model::JpQuery;
use crate::parser::{parse_json_path, Parsed};
use crate::query::queryable::Queryable;
use crate::query::state::{Data, Pointer};
use state::State;
Expand Down Expand Up @@ -62,7 +63,16 @@ impl<'a, T: Queryable> From<Pointer<'a, T>> for QueryRef<'a, T> {
/// The main function to process a JSONPath query.
/// It takes a path and a value, and returns a vector of `QueryResult` thus values + paths.
pub fn js_path<'a, T: Queryable>(path: &str, value: &'a T) -> Queried<Vec<QueryRef<'a, T>>> {
match parse_json_path(path)?.process(State::root(value)).data {
js_path_process(&parse_json_path(path)?, value)
}

/// A convenience function to process a JSONPath query
/// and return a vector of values, omitting the path.
pub fn js_path_process<'a, 'b, T: Queryable>(
path: &'b JpQuery,
value: &'a T,
) -> Queried<Vec<QueryRef<'a, T>>> {
match path.process(State::root(value)).data {
Data::Ref(p) => Ok(vec![p.into()]),
Data::Refs(refs) => Ok(refs.into_iter().map(Into::into).collect()),
Data::Value(v) => Err(v.into()),
Expand All @@ -89,9 +99,9 @@ pub fn js_path_path<T: Queryable>(path: &str, value: &T) -> Queried<Vec<QueryPat
#[cfg(test)]
mod tests {
use crate::parser::errors::JsonPathError;
use crate::parser::Parsed;
use crate::parser::{parse_json_path, Parsed};
use crate::query::queryable::Queryable;
use crate::query::{js_path, Queried, QueryRef};
use crate::query::{js_path, js_path_process, Queried, QueryRef};
use crate::JsonPath;
use serde_json::{json, Value};

Expand Down Expand Up @@ -807,4 +817,27 @@ mod tests {

Ok(())
}

#[test]
fn prepared_query() -> Queried<()> {
let json1 = json!({
"a": 1,
"b": 2,
"c": 3
});
let json2 = json!({
"a": 1,
"b": 2,
"c": 3
});

let jq = parse_json_path("$[?@<3]")?;

let v1 = js_path_process(&jq, &json1)?;
let v2 = js_path_process(&jq, &json2)?;

assert_eq!(v1, v2);

Ok(())
}
}
Loading