PR remarks

This commit is contained in:
Kacper Madej 2025-01-02 14:41:47 +07:00
parent 719eda7cf7
commit 7d7d202ffe
3 changed files with 26 additions and 9 deletions

View file

@ -6,19 +6,24 @@ use pest_derive::Parser;
#[grammar = "json/json_path.pest"]
struct Parser;
#[derive(Clone, Debug, PartialEq)]
pub enum PathElement {
Root(),
Key(String),
ArrayLocator(i32),
}
/// Describes a JSON path, which is a sequence of keys and/or array locators.
#[derive(Debug)]
pub struct JsonPath {
pub elements: Vec<PathElement>,
}
// Parses path into a Vec of Strings, where each string is a key or an array locator.
/// PathElement describes a single element of a JSON path.
#[derive(Clone, Debug, PartialEq)]
pub enum PathElement {
/// Root element: '$'
Root(),
/// JSON key
Key(String),
/// Array locator, eg. [2], [#-5]
ArrayLocator(i32),
}
/// Parses path into a Vec of Strings, where each string is a key or an array locator.
pub fn json_path(path: &str) -> crate::Result<JsonPath> {
let parsed = Parser::parse(Rule::path, path);

View file

@ -185,7 +185,7 @@ pub fn json_extract(value: &OwnedValue, paths: &[OwnedValue]) -> crate::Result<O
}
if paths.len() > 1 {
result.pop();
result.pop(); // remove the final comma
result.push(']');
}

View file

@ -113,6 +113,18 @@ do_execsql_test json_extract_number {
SELECT json_extract(1, '$')
} {{1}}
do_execsql_test json_extract_object_1 {
SELECT json_extract('{"a": [1,2,3]}', '$.a')
} {{[1,2,3]}}
do_execsql_test json_extract_object_2 {
SELECT json_extract('{"a": [1,2,3]}', '$.a', '$.a[0]', '$.a[1]', '$.a[3]')
} {{[[1,2,3],1,2,null]}}
do_execsql_test json_extract_object_3 {
SELECT json_extract('{"a": [1,2,3]}', '$.a', '$.a[0]', '$.a[1]', null, '$.a[3]')
} {{}}
# TODO: fix me
# \x61 is the ASCII code for 'a'
# do_execsql_test json_extract_with_escaping {