Update doc for jsonpath module

This commit is contained in:
Fabrice Reix 2023-05-05 19:22:18 +02:00 committed by jcamiel
parent fc9e8a97f6
commit 5b911602d5
No known key found for this signature in database
GPG Key ID: 07FF11CFD55356CC
3 changed files with 21 additions and 14 deletions

View File

@ -16,12 +16,11 @@
* *
*/ */
mod selector;
pub mod query; pub mod query;
mod selector;
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum JsonpathResult { pub enum JsonpathResult {
SingleEntry(serde_json::Value), // returned by a "definite" path SingleEntry(serde_json::Value), // returned by a "definite" path
Collection(Vec<serde_json::Value>), // returned by a "indefinite" path Collection(Vec<serde_json::Value>), // returned by a "indefinite" path
} }

View File

@ -20,6 +20,8 @@ use crate::jsonpath::ast::Query;
use crate::jsonpath::JsonpathResult; use crate::jsonpath::JsonpathResult;
impl Query { impl Query {
/// Eval a JSONPath `Query` for a `serde_json::Value` input.
/// It returns an Option<`JsonResultPath`>.
pub fn eval(&self, value: &serde_json::Value) -> Option<JsonpathResult> { pub fn eval(&self, value: &serde_json::Value) -> Option<JsonpathResult> {
let mut result = JsonpathResult::SingleEntry(value.clone()); let mut result = JsonpathResult::SingleEntry(value.clone());
for selector in &self.selectors { for selector in &self.selectors {
@ -53,8 +55,6 @@ mod tests {
use crate::jsonpath::JsonpathResult; use crate::jsonpath::JsonpathResult;
use serde_json::json; use serde_json::json;
pub fn json_root() -> serde_json::Value { pub fn json_root() -> serde_json::Value {
json!({ "store": json_store() }) json!({ "store": json_store() })
} }

View File

@ -19,18 +19,19 @@
//! JSONPath specs. //! JSONPath specs.
//! //!
//! There is no proper specifications for JSONPath. //! There is no proper specifications for JSONPath.
//! The de-facto one is still <https://goessner.net/articles/JsonPath/> //! The de-facto one is still <https://goessner.net/articles/JsonPath/>.
//!
//! Hurl will try to follow this one as closely as possible //! Hurl will try to follow this one as closely as possible
//! //!
//! There are a few edge cases for which several implementations differ //! There are a few edge cases for which several implementations differ.
//! The online app <https://jsonpath.herokuapp.com/> might be used to test them //!
//! We describe below the behaviour that we expect in Hurl. //! We describe below the behaviour that we expect in Hurl.
//! //!
//! Specify a field key in a subscript operator: `$['name']` //! Specify a field key in a subscript operator: `$['name']`.
//! The key must be enclosed within single quotes only. //! The key must be enclosed within single quotes only.
//! The following expressions will not be valid: `$["name"]` and `$[name]` //! The following expressions will not be valid: `$["name"]` and `$[name]`.
//! //!
//! Accessing a key containing a single quote must be escape: `$['\'']` //! Accessing a key containing a single quote must be escape: `$['\'']`.
//! Key with unicode are supported: `$['✈']` //! Key with unicode are supported: `$['✈']`
//! //!
//! Any character within these quote won't have a specific meaning: //! Any character within these quote won't have a specific meaning:
@ -38,10 +39,10 @@
//! - `$['.']` selects the element with key '.'. //! - `$['.']` selects the element with key '.'.
//! //!
//! The dot notation is usually more readable the the bracket notation //! The dot notation is usually more readable the the bracket notation
//! but it is more limited in terms of allowed characters //! but it is more limited in terms of allowed characters.
//! The following characters are allowed: //! The following characters are allowed:
//! alphanumeric //! - alphanumeric
//! _ (underscore) //! - _ (underscore)
//! //!
//! Filters can be applied to element of an array with the `?(@.key PREDICATE)` notation. //! Filters can be applied to element of an array with the `?(@.key PREDICATE)` notation.
//! The key can can specify one or more levels. //! The key can can specify one or more levels.
@ -49,7 +50,14 @@
//! The predicate if not present just checks the key existence. //! The predicate if not present just checks the key existence.
//! //!
//! The Hurl API for evaluating a jsonpath expression does not always return a collection (as defined in the jsonpath spec). //! The Hurl API for evaluating a jsonpath expression does not always return a collection (as defined in the jsonpath spec).
//! It returns an optional value, which is either a collection or a single value. //! It returns an optional value, which is either a collection or a single value (scalar).
//! Note that other implementations (such as the Java lib <https://github.com/json-path/JsonPath>) also distinguish between node value (definite path) and collection (indefinite path).
//!
//! Note that the only selectors returning a scalar are:
//! - array index selector ($.store.book[2])
//! - object key selector ($.store.bicycle.color/$.store.bicycle['color'])
//!
//! This will make testing the value a bit easier.
//! //!
pub use self::eval::JsonpathResult; pub use self::eval::JsonpathResult;