Skip to content
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

fix: fix jsonpath selector unwrap panic #53

Merged
merged 1 commit into from
Jul 18, 2024
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 benches/get_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn jsonb_get(data: &[u8], paths: &[&str], expected: &str) {
let mut result_data = vec![];
let mut result_offsets = vec![];

jsonb::get_by_path(data, json_path, &mut result_data, &mut result_offsets);
jsonb::get_by_path(data, json_path, &mut result_data, &mut result_offsets).unwrap();

let s = jsonb::as_str(&result_data).unwrap();
assert_eq!(s, expected);
Expand Down
6 changes: 6 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,9 @@ impl From<std::str::Utf8Error> for Error {
Error::InvalidUtf8
}
}

impl From<nom::Err<nom::error::Error<&[u8]>>> for Error {
fn from(_error: nom::Err<nom::error::Error<&[u8]>>) -> Self {
Error::InvalidJsonb
}
}
25 changes: 14 additions & 11 deletions src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,15 @@ pub fn array_length(value: &[u8]) -> Option<usize> {
}

/// Checks whether the JSON path returns any item for the `JSONB` value.
pub fn path_exists<'a>(value: &'a [u8], json_path: JsonPath<'a>) -> bool {
pub fn path_exists<'a>(value: &'a [u8], json_path: JsonPath<'a>) -> Result<bool, Error> {
let selector = Selector::new(json_path, Mode::Mixed);
if !is_jsonb(value) {
match parse_value(value) {
Ok(val) => {
let value = val.to_vec();
selector.exists(value.as_slice())
}
Err(_) => false,
Err(_) => Ok(false),
}
} else {
selector.exists(value)
Expand All @@ -188,16 +188,17 @@ pub fn get_by_path<'a>(
json_path: JsonPath<'a>,
data: &mut Vec<u8>,
offsets: &mut Vec<u64>,
) {
) -> Result<(), Error> {
let selector = Selector::new(json_path, Mode::Mixed);
if !is_jsonb(value) {
if let Ok(val) = parse_value(value) {
let value = val.to_vec();
selector.select(value.as_slice(), data, offsets)
selector.select(value.as_slice(), data, offsets)?;
}
} else {
selector.select(value, data, offsets)
selector.select(value, data, offsets)?;
}
Ok(())
}

/// Get the inner element of `JSONB` value by JSON path.
Expand All @@ -207,16 +208,17 @@ pub fn get_by_path_first<'a>(
json_path: JsonPath<'a>,
data: &mut Vec<u8>,
offsets: &mut Vec<u64>,
) {
) -> Result<(), Error> {
let selector = Selector::new(json_path, Mode::First);
if !is_jsonb(value) {
if let Ok(val) = parse_value(value) {
let value = val.to_vec();
selector.select(value.as_slice(), data, offsets)
selector.select(value.as_slice(), data, offsets)?;
}
} else {
selector.select(value, data, offsets)
selector.select(value, data, offsets)?;
}
Ok(())
}

/// Get the inner elements of `JSONB` value by JSON path.
Expand All @@ -226,16 +228,17 @@ pub fn get_by_path_array<'a>(
json_path: JsonPath<'a>,
data: &mut Vec<u8>,
offsets: &mut Vec<u64>,
) {
) -> Result<(), Error> {
let selector = Selector::new(json_path, Mode::Array);
if !is_jsonb(value) {
if let Ok(val) = parse_value(value) {
let value = val.to_vec();
selector.select(value.as_slice(), data, offsets)
selector.select(value.as_slice(), data, offsets)?;
}
} else {
selector.select(value, data, offsets)
selector.select(value, data, offsets)?;
}
Ok(())
}

/// Get the inner element of `JSONB` Array by index.
Expand Down
Loading
Loading