Selector's "select_to" function is deprecated

This commit is contained in:
freestrings 2019-05-14 22:48:36 +09:00
parent e2a6b13c9a
commit 135d3c319b
5 changed files with 43 additions and 16 deletions

View File

@ -217,7 +217,7 @@ pub fn compile<'a>(path: &'a str) -> impl FnMut(&Value) -> result::Result<Value,
move |json| {
let s: &mut Selector = selector.borrow_mut();
let _ = s.value(&json);
s.select_to_value()
s.select_as_value()
}
}
@ -261,7 +261,7 @@ pub fn selector<'a>(json: &Value) -> impl FnMut(&'a str) -> result::Result<Value
let mut selector = Box::new(selector);
move |path: &'a str| {
let s: &mut Selector = selector.borrow_mut();
s.path(path)?.select_to_value()
s.path(path)?.select_as_value()
}
}
@ -312,7 +312,7 @@ pub fn selector_as<T: serde::de::DeserializeOwned>(json: &Value) -> impl FnMut(&
let mut selector = Selector::new();
let _ = selector.value(json.into());
move |path: &str| {
selector.path(path)?.select_to()
selector.path(path)?.select_as()
}
}
@ -349,7 +349,7 @@ pub fn reader<'a>(json: &Value) -> impl FnMut(&'a str) -> result::Result<Value,
/// ```
pub fn select(json: &Value, path: &str) -> result::Result<Value, String> {
let mut selector = Selector::new();
selector.path(path)?.value(json.into())?.select_to_value()
selector.path(path)?.value(json.into())?.select_as_value()
}
#[deprecated(since = "0.1.4", note = "Please use the select function instead")]
@ -389,7 +389,7 @@ pub fn select_as_str(json: &str, path: &str) -> result::Result<String, String> {
Selector::new()
.path(path)?
.value_from_str(json)?
.select_to_str()
.select_as_str()
}
/// This function compile a jsonpath everytime and it convert `&str` to `jsonpath's RefValue` everytime and then it return a deserialized-instance of type `T`.
@ -434,5 +434,5 @@ pub fn select_as<T: serde::de::DeserializeOwned>(json: &str, path: &str) -> resu
Selector::new()
.path(path)?
.value_from_str(json)?
.select_to()
.select_as()
}

View File

@ -130,15 +130,30 @@ impl Selector {
}
}
#[deprecated(since = "0.1.13", note = "Please use the select_as_str function instead")]
pub fn select_to_str(&self) -> result::Result<String, String> {
self.select_as_str()
}
#[deprecated(since = "0.1.13", note = "Please use the select_as_value function instead")]
pub fn select_to_value(&self) -> result::Result<Value, String> {
self.select_as_value()
}
#[deprecated(since = "0.1.13", note = "Please use the select_as function instead")]
pub fn select_to<T: serde::de::DeserializeOwned>(&self) -> result::Result<T, String> {
self.select_as()
}
pub fn select_as_str(&self) -> result::Result<String, String> {
serde_json::to_string(self.select()?.deref()).map_err(|e| e.to_string())
}
pub fn select_to_value(&self) -> result::Result<Value, String> {
pub fn select_as_value(&self) -> result::Result<Value, String> {
Ok((&self.select()?).into())
}
pub fn select_to<T: serde::de::DeserializeOwned>(&self) -> result::Result<T, String> {
pub fn select_as<T: serde::de::DeserializeOwned>(&self) -> result::Result<T, String> {
T::deserialize(self.select()?.deref()).map_err(|e| e.to_string())
}

View File

@ -14,6 +14,8 @@ It is Webassembly version of [jsonpath_lib](https://github.com/freestrings/jsonp
### jsonpath.Selector
> Selector's selectTo function is deprecated since 0.1.3
```javascript
let jsonObj = {
"school": {
@ -31,19 +33,19 @@ let jsonObj = {
let selector = new jsonpath.Selector().value(jsonObj);
{
let jsonObj = selector.path('$..[?(@.age >= 30)]').selectTo();
let jsonObj = selector.path('$..[?(@.age >= 30)]').selectAs();
let resultObj = [{"name": "친구3", "age": 30}];
console.log(JSON.stringify(jsonObj) === JSON.stringify(resultObj));
}
{
let jsonObj = selector.path('$..[?(@.age == 20)]').selectTo();
let jsonObj = selector.path('$..[?(@.age == 20)]').selectAs();
let resultObj = [{"name": "친구1", "age": 20}, {"name": "친구2", "age": 20}];
console.log(JSON.stringify(jsonObj) === JSON.stringify(resultObj));
}
{
let jsonObj = selector.value({"friends": [ {"name": "친구5", "age": 20} ]}).selectTo();
let jsonObj = selector.value({"friends": [ {"name": "친구5", "age": 20} ]}).selectAs();
let resultObj = [{"name": "친구5", "age": 20}];
console.log(JSON.stringify(jsonObj) === JSON.stringify(resultObj));
}

View File

@ -55,12 +55,12 @@ fn into_serde_json<D>(js_value: &JsValue) -> Result<D, String>
if js_value.is_string() {
match serde_json::from_str(js_value.as_string().unwrap().as_str()) {
Ok(json) => Ok(json),
Err(e) => Err(format!("{:?}", e))
Err(e) => Err(e.to_string())
}
} else {
match js_value.into_serde() {
Ok(json) => Ok(json),
Err(e) => Err(format!("{:?}", e))
Err(e) => Err(e.to_string())
}
}
}
@ -203,13 +203,23 @@ impl Selector {
#[wasm_bindgen(catch, js_name = selectToStr)]
pub fn select_to_str(&mut self) -> result::Result<JsValue, JsValue> {
let json_str = self.selector.select_to_str()?;
self.select_as_str()
}
#[wasm_bindgen(catch, js_name = selectAsStr)]
pub fn select_as_str(&mut self) -> result::Result<JsValue, JsValue> {
let json_str = self.selector.select_as_str()?;
Ok(JsValue::from_str(&json_str))
}
#[wasm_bindgen(catch, js_name = selectTo)]
pub fn select_to(&mut self) -> result::Result<JsValue, JsValue> {
let ref_value = self.selector.select_to::<RefValue>()
self.select_as()
}
#[wasm_bindgen(catch, js_name = selectAs)]
pub fn select_as(&mut self) -> result::Result<JsValue, JsValue> {
let ref_value = self.selector.select_as::<RefValue>()
.map_err(|e| JsValue::from_str(&e))?;
Ok(JsValue::from_serde(&ref_value)
.map_err(|e| JsValue::from_str(&format!("{:?}", e)))?)

View File

@ -113,6 +113,6 @@ fn selector_struct() {
let mut selector = jsonpath::Selector::new();
selector.path("$..book[2]").unwrap();
selector.value(JsValue::from_str(json_str())).unwrap();
let json: Value = selector.select_to().unwrap().into_serde().unwrap();
let json: Value = selector.select_as().unwrap().into_serde().unwrap();
assert_eq!(json, target_json());
}