v0.1.12 - value function in Selector parameter type change. from RefValue to &Value

This commit is contained in:
freestrings 2019-05-06 22:29:28 +09:00
parent e4a50bd689
commit 3792e0014d
17 changed files with 213 additions and 1289 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "jsonpath_lib"
version = "0.1.11"
version = "0.1.12"
authors = ["Changseok Han <freestrings@gmail.com>"]
description = "It is JsonPath engine written in Rust. it provide a similar API interface in Webassembly and Javascript also. - Webassembly Demo: https://freestrings.github.io/jsonpath"

View File

@ -2,9 +2,7 @@
[![Build Status](https://travis-ci.org/freestrings/jsonpath.svg?branch=master)](https://travis-ci.org/freestrings/jsonpath)
![crates.io](https://img.shields.io/crates/v/jsonpath_lib.svg)
![Crates.io](https://img.shields.io/crates/d/jsonpath_lib.svg?label=%60jsonpath_lib%60%20downloads)
![npm](https://img.shields.io/npm/v/jsonpath-rs.svg?label=npm%20%60jsonpath-rs%60)
![npm](https://img.shields.io/npm/dt/jsonpath-rs.svg?label=%60jsonpath-rs%60%20downloads)
![npm](https://img.shields.io/npm/v/jsonpath-wasm.svg?label=npm%20%60jsonpath-wasm%60)
`Rust` 버전 [JsonPath](https://goessner.net/articles/JsonPath/) 구현이다. `Webassembly``Javascript`에서도 유사한 API 인터페이스를 제공 한다.
@ -77,7 +75,7 @@ let result = selector
.path("$..[?(@.age >= 30)]").unwrap()
// .value_from_str(&serde_json::to_string(&json_obj).unwrap() /*&str*/).unwrap()
// .value_from(&json_obj /*&impl serde::ser::Serialize*/).unwrap()
.value((&json_obj).into() /*Parameter type will be changed from `RefValue` to `&Value` since `0.1.12`*/ ).unwrap()
.value(&json_obj /*serde_json::value::Value*/ ).unwrap()
.select_to_value().unwrap();
assert_eq!(json!([{"name": "친구3", "age": 30}]), result);

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@ -91,11 +91,11 @@
/******/ "__wbindgen_rethrow": function(p0i32) {
/******/ return installedModules["../all_pkg/jsonpath_wasm.js"].exports["__wbindgen_rethrow"](p0i32);
/******/ },
/******/ "__wbindgen_closure_wrapper104": function(p0i32,p1i32,p2i32) {
/******/ return installedModules["../all_pkg/jsonpath_wasm.js"].exports["__wbindgen_closure_wrapper104"](p0i32,p1i32,p2i32);
/******/ "__wbindgen_closure_wrapper111": function(p0i32,p1i32,p2i32) {
/******/ return installedModules["../all_pkg/jsonpath_wasm.js"].exports["__wbindgen_closure_wrapper111"](p0i32,p1i32,p2i32);
/******/ },
/******/ "__wbindgen_closure_wrapper106": function(p0i32,p1i32,p2i32) {
/******/ return installedModules["../all_pkg/jsonpath_wasm.js"].exports["__wbindgen_closure_wrapper106"](p0i32,p1i32,p2i32);
/******/ "__wbindgen_closure_wrapper113": function(p0i32,p1i32,p2i32) {
/******/ return installedModules["../all_pkg/jsonpath_wasm.js"].exports["__wbindgen_closure_wrapper113"](p0i32,p1i32,p2i32);
/******/ }
/******/ }
/******/ };
@ -195,7 +195,7 @@
/******/ promises.push(installedWasmModuleData);
/******/ else {
/******/ var importObject = wasmImportObjects[wasmModuleId]();
/******/ var req = fetch(__webpack_require__.p + "" + {"../all_pkg/jsonpath_wasm_bg.wasm":"ddd003b015308ef8b732"}[wasmModuleId] + ".module.wasm");
/******/ var req = fetch(__webpack_require__.p + "" + {"../all_pkg/jsonpath_wasm_bg.wasm":"89ad85706c624c3a6274"}[wasmModuleId] + ".module.wasm");
/******/ var promise;
/******/ if(importObject instanceof Promise && typeof WebAssembly.compileStreaming === 'function') {
/******/ promise = Promise.all([WebAssembly.compileStreaming(req), importObject]).then(function(items) {

10
docs/bootstrap.js vendored
View File

@ -91,11 +91,11 @@
/******/ "__wbindgen_rethrow": function(p0i32) {
/******/ return installedModules["../all_pkg/jsonpath_wasm.js"].exports["__wbindgen_rethrow"](p0i32);
/******/ },
/******/ "__wbindgen_closure_wrapper104": function(p0i32,p1i32,p2i32) {
/******/ return installedModules["../all_pkg/jsonpath_wasm.js"].exports["__wbindgen_closure_wrapper104"](p0i32,p1i32,p2i32);
/******/ "__wbindgen_closure_wrapper111": function(p0i32,p1i32,p2i32) {
/******/ return installedModules["../all_pkg/jsonpath_wasm.js"].exports["__wbindgen_closure_wrapper111"](p0i32,p1i32,p2i32);
/******/ },
/******/ "__wbindgen_closure_wrapper106": function(p0i32,p1i32,p2i32) {
/******/ return installedModules["../all_pkg/jsonpath_wasm.js"].exports["__wbindgen_closure_wrapper106"](p0i32,p1i32,p2i32);
/******/ "__wbindgen_closure_wrapper113": function(p0i32,p1i32,p2i32) {
/******/ return installedModules["../all_pkg/jsonpath_wasm.js"].exports["__wbindgen_closure_wrapper113"](p0i32,p1i32,p2i32);
/******/ }
/******/ }
/******/ };
@ -195,7 +195,7 @@
/******/ promises.push(installedWasmModuleData);
/******/ else {
/******/ var importObject = wasmImportObjects[wasmModuleId]();
/******/ var req = fetch(__webpack_require__.p + "" + {"../all_pkg/jsonpath_wasm_bg.wasm":"ddd003b015308ef8b732"}[wasmModuleId] + ".module.wasm");
/******/ var req = fetch(__webpack_require__.p + "" + {"../all_pkg/jsonpath_wasm_bg.wasm":"89ad85706c624c3a6274"}[wasmModuleId] + ".module.wasm");
/******/ var promise;
/******/ if(importObject instanceof Promise && typeof WebAssembly.compileStreaming === 'function') {
/******/ promise = Promise.all([WebAssembly.compileStreaming(req), importObject]).then(function(items) {

1388
nodejs/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -21,7 +21,7 @@
"test": "mocha"
},
"devDependencies": {
"mocha": "^6.0.2"
"mocha": "^6.1.4"
},
"repository": {
"type": "git",

View File

@ -216,7 +216,7 @@ pub fn compile<'a>(path: &'a str) -> impl FnMut(&Value) -> result::Result<Value,
let mut selector = Box::new(selector);
move |json| {
let s: &mut select::Selector = selector.borrow_mut();
let _ = s.value(json.into());
let _ = s.value(&json);
s.select_to_value()
}
}

View File

@ -75,6 +75,7 @@ use super::ref_value::model::*;
/// let result = selector.select_to::<Vec<Person>>().unwrap();
/// assert_eq!(input_person()[0], result[0]);
/// ```
#[derive(Debug)]
pub struct Selector {
pub(crate) node: Option<Node>,
pub(crate) value: Option<RefValueWrapper>,
@ -91,9 +92,8 @@ impl Selector {
Ok(self)
}
#[deprecated(since = "0.1.12", note = "Parameter type will be changed from `RefValue` to `&Value` since `0.1.12`")]
pub fn value(&mut self, ref_value: RefValue) -> result::Result<&mut Self, String> {
self.value = Some(ref_value.into());
pub fn value(&mut self, value: &Value) -> result::Result<&mut Self, String> {
self.value = Some(value.into());
Ok(self)
}
@ -101,43 +101,41 @@ impl Selector {
let ref_value: RefValue = serializable
.serialize(super::ref_value::ser::Serializer)
.map_err(|e| format!("{:?}", e))?;
self.value(ref_value)
self.value = Some(ref_value.into());
Ok(self)
}
pub fn value_from_str(&mut self, json_str: &str) -> result::Result<&mut Self, String> {
let ref_value: RefValue = serde_json::from_str(json_str)
let value = serde_json::from_str(json_str)
.map_err(|e| format!("{:?}", e))?;
self.value(ref_value)
self.value(&value)
}
fn jf(&mut self) -> result::Result<JsonValueFilter, String> {
match &self.value {
Some(v) => Ok(JsonValueFilter::new_from_value(v.clone())),
_ => return Err("Value is empty".to_owned())
_ => return Err("Empty value".to_owned())
}
}
fn select(&mut self) -> result::Result<RefValueWrapper, String> {
let mut jf = self.jf()?;
match &mut self.node {
Some(node) => {
jf.visit(node.clone());
Ok(jf.take_value())
}
_ => Err("Empty path".to_owned())
}
}
pub fn select_to_str(&mut self) -> result::Result<String, String> {
let mut jf = self.jf()?;
match &mut self.node {
Some(node) => {
jf.visit(node.clone());
return serde_json::to_string(jf.take_value().deref()).map_err(|e| format!("{:?}", e));
}
_ => return Err("Path is empty".to_owned())
};
serde_json::to_string(self.select()?.deref()).map_err(|e| format!("{:?}", e))
}
pub fn select_to_value(&mut self) -> result::Result<Value, String> {
let mut jf = self.jf()?;
match &mut self.node {
Some(node) => {
jf.visit(node.clone());
Ok((&jf.take_value()).into())
}
_ => Err("Path is empty".to_owned())
}
Ok((&self.select()?).into())
}
pub fn select_to<T: serde::de::DeserializeOwned>(&mut self) -> result::Result<T, String> {

View File

@ -25,6 +25,16 @@ fn input_str() -> &'static str {
"name": "이름2",
"age": 42,
"phone": "++44 12341234"
},
{
"name": "이름3",
"age": 50,
"phone": "++55 111111"
},
{
"name": "이름4",
"age": 51,
"phone": "++55 12341234"
}
]"#
}
@ -36,6 +46,7 @@ fn input_json() -> Value {
fn input_person() -> Vec<Person> {
serde_json::from_str(input_str()).unwrap()
}
#[test]
fn selector_value_from() {
let result = Selector::new()
@ -74,7 +85,8 @@ fn selector_select_to() {
assert_eq!(input_json()[1], result[0]);
let result = selector.select_to_str().unwrap();
assert_eq!(serde_json::to_string(&vec![&input_json()[1].clone()]).unwrap(), result);
let value: Value = serde_json::from_str(&result).unwrap();
assert_eq!(input_json()[1], value[0]);
let result = selector.select_to::<Vec<Person>>().unwrap();
assert_eq!(input_person()[1], result[0]);

View File

@ -20,6 +20,7 @@ console_error_panic_hook = { version = "0.1.1", optional = true }
wee_alloc = { version = "0.4.2", optional = true }
jsonpath_lib = { path = "../" }
serde = "1.0"
serde_json = { version = "1.0", features = ["preserve_order"] }
lazy_static = "1.3.0"
web-sys = { version = "0.3", features = ['console'] }

View File

@ -2,6 +2,7 @@ extern crate cfg_if;
extern crate jsonpath_lib as jsonpath;
#[macro_use]
extern crate lazy_static;
extern crate serde;
extern crate serde_json;
extern crate wasm_bindgen;
extern crate web_sys;
@ -48,7 +49,9 @@ fn filter_ref_value(json: RefValueWrapper, node: Node) -> JsValue {
}
}
fn into_serde_json(js_value: &JsValue) -> Result<RefValue, String> {
fn into_serde_json<D>(js_value: &JsValue) -> Result<D, String>
where D: for<'a> serde::de::Deserialize<'a>
{
if js_value.is_string() {
match serde_json::from_str(js_value.as_string().unwrap().as_str()) {
Ok(json) => Ok(json),
@ -63,7 +66,8 @@ fn into_serde_json(js_value: &JsValue) -> Result<RefValue, String> {
}
fn into_ref_value(js_value: &JsValue, node: Node) -> JsValue {
match into_serde_json(js_value) {
let result: result::Result<RefValue, String> = into_serde_json::<RefValue>(js_value);
match result {
Ok(json) => filter_ref_value(json.into(), node),
Err(e) => JsValue::from_str(&format!("Json serialize error: {}", e))
}
@ -88,7 +92,8 @@ lazy_static! {
#[wasm_bindgen(js_name = allocJson)]
pub extern fn alloc_json(js_value: JsValue) -> usize {
match into_serde_json(&js_value) {
let result: result::Result<RefValue, String> = into_serde_json(&js_value);
match result {
Ok(json) => {
let mut map = CACHE_JSON.lock().unwrap();
if map.len() >= std::u8::MAX as usize {
@ -139,7 +144,7 @@ pub fn selector(js_value: JsValue) -> JsValue {
}
}
_ => {
match into_serde_json(&js_value) {
match into_serde_json::<RefValue>(&js_value) {
Ok(json) => json.into(),
Err(e) => return JsValue::from_str(e.as_str())
}
@ -191,7 +196,7 @@ impl Selector {
#[wasm_bindgen(catch)]
pub fn value(&mut self, value: JsValue) -> result::Result<(), JsValue> {
let ref_value = into_serde_json(&value)?;
let ref ref_value = into_serde_json(&value)?;
let _ = self.selector.value(ref_value)?;
Ok(())
}