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:
Guy Korland 2019-08-26 15:41:43 +03:00 committed by freestrings
parent e0db04aed9
commit 17a8608392
6 changed files with 24 additions and 16 deletions

View File

@ -92,7 +92,7 @@ let result = selector_mut
0 0
}; };
json!(age) Some(json!(age))
}).unwrap() }).unwrap()
.take().unwrap(); .take().unwrap();
@ -353,7 +353,7 @@ let ret = jsonpath::replace_with(json_obj, "$..[?(@.age == 20)].age", &mut |v| {
0 0
}; };
json!(age) Some(json!(age))
}).unwrap(); }).unwrap();
assert_eq!(ret, json!({ assert_eq!(ret, json!({

View File

@ -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> pub fn replace_with<F>(value: Value, path: &str, fun: &mut F) -> Result<Value, JsonPathError>
where where
F: FnMut(Value) -> Value, F: FnMut(Value) -> Option<Value>,
{ {
let mut selector = SelectorMut::default(); let mut selector = SelectorMut::default();
let value = selector.str_path(path)?.value(value).replace_with(fun)?; let value = selector.str_path(path)?.value(value).replace_with(fun)?;

View File

@ -1027,15 +1027,15 @@ pub struct SelectorMut {
value: Option<Value>, value: Option<Value>,
} }
<<<<<<< HEAD
<<<<<<< HEAD <<<<<<< HEAD
fn replace_value<F: FnMut(Value) -> Value>( fn replace_value<F: FnMut(Value) -> Value>(
mut tokens: Vec<String>, mut tokens: Vec<String>,
value: &mut Value, value: &mut Value,
fun: &mut F, fun: &mut F,
) { ) {
=======
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 mut target = value;
let last_index = tokens.len() - 1; 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 let Ok(x) = token.parse::<usize>() {
if is_last { if is_last {
let v = std::mem::replace(&mut vec[x], Value::Null); 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; return;
} }
vec.get_mut(x) vec.get_mut(x)
@ -1167,7 +1171,11 @@ impl SelectorMut {
} }
pub fn delete(&mut self) -> Result<&mut Self, JsonPathError> { 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> { 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, &mut self,
fun: &mut F, fun: &mut F,
) -> Result<&mut Self, JsonPathError> { ) -> Result<&mut Self, JsonPathError> {

View File

@ -224,7 +224,7 @@ fn readme_selector_mut() {
0 0
}; };
json!(age) Some(json!(age))
}) })
.unwrap() .unwrap()
.take() .take()
@ -522,7 +522,7 @@ fn readme_replace_with() {
0 0
}; };
json!(age) Some(json!(age))
}) })
.unwrap(); .unwrap();

View File

@ -23,7 +23,7 @@ fn selector_mut() {
if let Value::Number(n) = v { if let Value::Number(n) = v {
nums.push(n.as_f64().unwrap()); nums.push(n.as_f64().unwrap());
} }
Value::String("a".to_string()) Some(Value::String("a".to_string()))
}) })
.unwrap() .unwrap()
.take() .take()

View File

@ -56,24 +56,24 @@ where
} }
} }
fn replace_fun(v: Value, fun: &js_sys::Function) -> Value { fn replace_fun(v: Value, fun: &js_sys::Function) -> Option<Value> {
match JsValue::from_serde(&v) { match JsValue::from_serde(&v) {
Ok(js_v) => match fun.call1(&JsValue::NULL, &js_v) { Ok(js_v) => match fun.call1(&JsValue::NULL, &js_v) {
Ok(result) => match into_serde_json(&result) { Ok(result) => match into_serde_json(&result) {
Ok(json) => json, Ok(json) => Some(json),
Err(e) => { Err(e) => {
console_error!("replace_with - closure returned a invalid JSON: {:?}", e); console_error!("replace_with - closure returned a invalid JSON: {:?}", e);
Value::Null Some(Value::Null)
} }
}, },
Err(e) => { Err(e) => {
console_error!("replace_with - fail to call closure: {:?}", e); console_error!("replace_with - fail to call closure: {:?}", e);
Value::Null Some(Value::Null)
} }
}, },
Err(e) => { Err(e) => {
console_error!("replace_with - invalid JSON object: {:?}", e); console_error!("replace_with - invalid JSON object: {:?}", e);
Value::Null Some(Value::Null)
} }
} }
} }