From 17a860839200fbb09dc8e92eaed44526f73ea263 Mon Sep 17 00:00:00 2001 From: Guy Korland Date: Mon, 26 Aug 2019 15:41:43 +0300 Subject: [PATCH] Return Option from fun on replace_with to all user to return None (remove the value and not replace with Null) --- README.md | 4 ++-- src/lib.rs | 2 +- src/select/mod.rs | 18 +++++++++++++----- tests/readme.rs | 4 ++-- tests/selector.rs | 2 +- wasm/src/lib.rs | 10 +++++----- 6 files changed, 24 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index eac2230..e23bcee 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ let result = selector_mut 0 }; - json!(age) + Some(json!(age)) }).unwrap() .take().unwrap(); @@ -353,7 +353,7 @@ let ret = jsonpath::replace_with(json_obj, "$..[?(@.age == 20)].age", &mut |v| { 0 }; - json!(age) + Some(json!(age)) }).unwrap(); assert_eq!(ret, json!({ diff --git a/src/lib.rs b/src/lib.rs index 7ee96da..9c1ab8a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -460,7 +460,7 @@ pub fn delete(value: Value, path: &str) -> Result { /// ``` pub fn replace_with(value: Value, path: &str, fun: &mut F) -> Result where - F: FnMut(Value) -> Value, + F: FnMut(Value) -> Option, { let mut selector = SelectorMut::default(); let value = selector.str_path(path)?.value(value).replace_with(fun)?; diff --git a/src/select/mod.rs b/src/select/mod.rs index 808c7a4..b6098aa 100644 --- a/src/select/mod.rs +++ b/src/select/mod.rs @@ -1027,15 +1027,15 @@ pub struct SelectorMut { value: Option, } +<<<<<<< HEAD <<<<<<< HEAD fn replace_value Value>( mut tokens: Vec, value: &mut Value, fun: &mut F, ) { -======= + fn replace_value Value>(mut tokens: Vec, 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 Value>(mut tokens: Vec, value: &mut if let Ok(x) = token.parse::() { 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, JsonPathError> { @@ -1185,7 +1193,7 @@ impl SelectorMut { } } - pub fn replace_with Value>( + pub fn replace_with Option>( &mut self, fun: &mut F, ) -> Result<&mut Self, JsonPathError> { diff --git a/tests/readme.rs b/tests/readme.rs index ae2a02c..f1b8729 100644 --- a/tests/readme.rs +++ b/tests/readme.rs @@ -224,7 +224,7 @@ fn readme_selector_mut() { 0 }; - json!(age) + Some(json!(age)) }) .unwrap() .take() @@ -522,7 +522,7 @@ fn readme_replace_with() { 0 }; - json!(age) + Some(json!(age)) }) .unwrap(); diff --git a/tests/selector.rs b/tests/selector.rs index 81374c7..b2b87bc 100644 --- a/tests/selector.rs +++ b/tests/selector.rs @@ -23,7 +23,7 @@ fn selector_mut() { if let Value::Number(n) = v { nums.push(n.as_f64().unwrap()); } - Value::String("a".to_string()) + Some(Value::String("a".to_string())) }) .unwrap() .take() diff --git a/wasm/src/lib.rs b/wasm/src/lib.rs index 85ae631..8278b01 100644 --- a/wasm/src/lib.rs +++ b/wasm/src/lib.rs @@ -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 { match JsValue::from_serde(&v) { Ok(js_v) => match fun.call1(&JsValue::NULL, &js_v) { Ok(result) => match into_serde_json(&result) { - Ok(json) => json, + Ok(json) => Some(json), Err(e) => { console_error!("replace_with - closure returned a invalid JSON: {:?}", e); - Value::Null + Some(Value::Null) } }, Err(e) => { console_error!("replace_with - fail to call closure: {:?}", e); - Value::Null + Some(Value::Null) } }, Err(e) => { console_error!("replace_with - invalid JSON object: {:?}", e); - Value::Null + Some(Value::Null) } } }