mirror of
https://github.com/fluencelabs/jsonpath
synced 2025-06-25 05:41:45 +00:00
Return Option<Value> from fun on replace_with to all user to return None (remove the value and not replace with Null)
This commit is contained in:
@ -460,7 +460,7 @@ pub fn delete(value: Value, path: &str) -> Result<Value, JsonPathError> {
|
||||
/// ```
|
||||
pub fn replace_with<F>(value: Value, path: &str, fun: &mut F) -> Result<Value, JsonPathError>
|
||||
where
|
||||
F: FnMut(Value) -> Value,
|
||||
F: FnMut(Value) -> Option<Value>,
|
||||
{
|
||||
let mut selector = SelectorMut::default();
|
||||
let value = selector.str_path(path)?.value(value).replace_with(fun)?;
|
||||
|
@ -1027,15 +1027,15 @@ pub struct SelectorMut {
|
||||
value: Option<Value>,
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
<<<<<<< HEAD
|
||||
fn replace_value<F: FnMut(Value) -> Value>(
|
||||
mut tokens: Vec<String>,
|
||||
value: &mut Value,
|
||||
fun: &mut F,
|
||||
) {
|
||||
=======
|
||||
|
||||
fn replace_value<F: FnMut(Value) -> Value>(mut tokens: Vec<String>, value: &mut Value, fun: &mut F) {
|
||||
>>>>>>> improve performance avoid remove & insert to map
|
||||
let mut target = value;
|
||||
|
||||
let last_index = tokens.len() - 1;
|
||||
@ -1061,7 +1061,11 @@ fn replace_value<F: FnMut(Value) -> Value>(mut tokens: Vec<String>, value: &mut
|
||||
if let Ok(x) = token.parse::<usize>() {
|
||||
if is_last {
|
||||
let v = std::mem::replace(&mut vec[x], Value::Null);
|
||||
vec[x] = fun(v);
|
||||
if let Some(res) = fun(v) {
|
||||
vec[x] = res;
|
||||
} else {
|
||||
vec.remove(x);
|
||||
}
|
||||
return;
|
||||
}
|
||||
vec.get_mut(x)
|
||||
@ -1167,7 +1171,11 @@ impl SelectorMut {
|
||||
}
|
||||
|
||||
pub fn delete(&mut self) -> Result<&mut Self, JsonPathError> {
|
||||
self.replace_with(&mut |_| Value::Null)
|
||||
self.replace_with(&mut |_| Some(Value::Null))
|
||||
}
|
||||
|
||||
pub fn remove(&mut self) -> Result<&mut Self, JsonPathError> {
|
||||
self.replace_with(&mut |_| None)
|
||||
}
|
||||
|
||||
fn select(&self) -> Result<Vec<&Value>, JsonPathError> {
|
||||
@ -1185,7 +1193,7 @@ impl SelectorMut {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn replace_with<F: FnMut(Value) -> Value>(
|
||||
pub fn replace_with<F: FnMut(Value) -> Option<Value>>(
|
||||
&mut self,
|
||||
fun: &mut F,
|
||||
) -> Result<&mut Self, JsonPathError> {
|
||||
|
Reference in New Issue
Block a user