mirror of
https://github.com/fluencelabs/jsonpath
synced 2025-06-14 00:21:25 +00:00
fix nodejs
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "jsonpath4nodejs"
|
||||
version = "0.1.3"
|
||||
version = "0.2.0"
|
||||
authors = ["Changseok Han <freestrings@gmail.com>"]
|
||||
description = "jsonpath_lib bindings for nodejs"
|
||||
keywords = ["library", "jsonpath", "json", "nodejs"]
|
||||
@ -14,7 +14,7 @@ exclude = ["artifacts.json", "index.node"]
|
||||
neon-build = "0.2.0"
|
||||
|
||||
[dependencies]
|
||||
jsonpath_lib = "0.1.13"
|
||||
jsonpath_lib = "0.2.0"
|
||||
neon = "0.2.0"
|
||||
neon-serde = "0.1.1"
|
||||
serde_json = { version = "1.0", features = ["preserve_order"] }
|
||||
|
@ -4,13 +4,9 @@ extern crate neon;
|
||||
extern crate neon_serde;
|
||||
extern crate serde_json;
|
||||
|
||||
use jsonpath::filter::value_filter::JsonValueFilter;
|
||||
use jsonpath::parser::parser::{Node, NodeVisitor, Parser};
|
||||
use jsonpath::ref_value::model::{RefValue, RefValueWrapper};
|
||||
use jsonpath::Selector;
|
||||
use jsonpath::{JsonPathError, Node, Parser, Selector};
|
||||
use neon::prelude::*;
|
||||
use serde_json::Value;
|
||||
use std::ops::Deref;
|
||||
|
||||
///
|
||||
/// `neon_serde::from_value` has very poor performance.
|
||||
@ -35,96 +31,124 @@ fn select_str(mut ctx: FunctionContext) -> JsResult<JsValue> {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CompileFn {
|
||||
node: Node
|
||||
}
|
||||
|
||||
pub struct SelectorFn {
|
||||
json: RefValueWrapper
|
||||
}
|
||||
|
||||
pub struct SelectorCls {
|
||||
selector: Selector
|
||||
node: Option<Node>,
|
||||
value: Option<Value>,
|
||||
}
|
||||
|
||||
impl SelectorCls {
|
||||
fn path(&mut self, path: &str) {
|
||||
let mut parser = Parser::new(path);
|
||||
let node = match parser.compile() {
|
||||
Ok(node) => node,
|
||||
Err(e) => panic!("{:?}", e)
|
||||
};
|
||||
|
||||
self.node = Some(node);
|
||||
}
|
||||
|
||||
fn value(&mut self, json_str: &str) {
|
||||
let value: Value = match serde_json::from_str(&json_str) {
|
||||
Ok(value) => value,
|
||||
Err(e) => panic!("{:?}", JsonPathError::Serde(e.to_string()))
|
||||
};
|
||||
|
||||
self.value = Some(value);
|
||||
}
|
||||
|
||||
fn select(&self) -> String {
|
||||
let node = match &self.node {
|
||||
Some(node) => node.clone(),
|
||||
None => panic!("{:?}", JsonPathError::EmptyPath)
|
||||
};
|
||||
|
||||
let value = match &self.value {
|
||||
Some(value) => value,
|
||||
None => panic!("{:?}", JsonPathError::EmptyValue)
|
||||
};
|
||||
|
||||
let mut selector = Selector::new();
|
||||
selector.compiled_path(node.clone());
|
||||
selector.value(&value);
|
||||
match selector.select_as_str() {
|
||||
Ok(ret) => ret,
|
||||
Err(e) => panic!("{:?}", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
declare_types! {
|
||||
pub class JsCompileFn for CompileFn {
|
||||
pub class JsCompileFn for SelectorCls {
|
||||
init(mut ctx) {
|
||||
let path = ctx.argument::<JsString>(0)?.value();
|
||||
let mut parser = Parser::new(path.as_str());
|
||||
|
||||
let node = match parser.compile() {
|
||||
Ok(node) => node,
|
||||
Err(e) => panic!("{:?}", e)
|
||||
};
|
||||
|
||||
Ok(CompileFn { node })
|
||||
Ok(SelectorCls { node: Some(node), value: None })
|
||||
}
|
||||
|
||||
method template(mut ctx) {
|
||||
let this = ctx.this();
|
||||
|
||||
let node = {
|
||||
let guard = ctx.lock();
|
||||
let this = this.borrow(&guard);
|
||||
this.node.clone()
|
||||
};
|
||||
let mut this = ctx.this();
|
||||
|
||||
let json_str = ctx.argument::<JsString>(0)?.value();
|
||||
let ref_value: RefValue = match serde_json::from_str(&json_str) {
|
||||
Ok(ref_value) => ref_value,
|
||||
Err(e) => panic!("{:?}", e)
|
||||
{
|
||||
let guard = ctx.lock();
|
||||
let mut this = this.borrow_mut(&guard);
|
||||
let value: Value = match serde_json::from_str(&json_str) {
|
||||
Ok(value) => value,
|
||||
Err(e) => panic!("{:?}", JsonPathError::Serde(e.to_string()))
|
||||
};
|
||||
this.value = Some(value);
|
||||
};
|
||||
|
||||
let mut jf = JsonValueFilter::new_from_value(ref_value.into());
|
||||
jf.visit(node);
|
||||
match serde_json::to_string(&jf.take_value().deref()) {
|
||||
Ok(json_str) => Ok(JsString::new(&mut ctx, &json_str).upcast()),
|
||||
Err(e) => panic!("{:?}", e)
|
||||
}
|
||||
let result_str = {
|
||||
let guard = ctx.lock();
|
||||
let this = this.borrow(&guard);
|
||||
this.select()
|
||||
};
|
||||
|
||||
Ok(JsString::new(&mut ctx, &result_str).upcast())
|
||||
}
|
||||
}
|
||||
|
||||
pub class JsSelectorFn for SelectorFn {
|
||||
pub class JsSelectorFn for SelectorCls {
|
||||
init(mut ctx) {
|
||||
let json_str = ctx.argument::<JsString>(0)?.value();
|
||||
let ref_value: RefValue = match serde_json::from_str(&json_str) {
|
||||
Ok(ref_value) => ref_value,
|
||||
Err(e) => panic!("{:?}", e)
|
||||
let value: Value = match serde_json::from_str(&json_str) {
|
||||
Ok(value) => value,
|
||||
Err(e) => panic!("{:?}", JsonPathError::Serde(e.to_string()))
|
||||
};
|
||||
|
||||
Ok(SelectorFn { json: ref_value.into() })
|
||||
Ok(SelectorCls { node: None, value: Some(value) })
|
||||
}
|
||||
|
||||
method select(mut ctx) {
|
||||
let this = ctx.this();
|
||||
|
||||
let json = {
|
||||
let guard = ctx.lock();
|
||||
let this = this.borrow(&guard);
|
||||
this.json.clone()
|
||||
};
|
||||
let mut this = ctx.this();
|
||||
|
||||
let path = ctx.argument::<JsString>(0)?.value();
|
||||
let mut parser = Parser::new(path.as_str());
|
||||
{
|
||||
let guard = ctx.lock();
|
||||
let mut this = this.borrow_mut(&guard);
|
||||
this.path(&path);
|
||||
}
|
||||
|
||||
let node = match parser.compile() {
|
||||
Ok(node) => node,
|
||||
Err(e) => panic!("{:?}", e)
|
||||
let result_str = {
|
||||
let guard = ctx.lock();
|
||||
let this = this.borrow(&guard);
|
||||
this.select()
|
||||
};
|
||||
|
||||
let mut jf = JsonValueFilter::new_from_value(json);
|
||||
jf.visit(node);
|
||||
match serde_json::to_string(&jf.take_value().deref()) {
|
||||
Ok(json_str) => Ok(JsString::new(&mut ctx, &json_str).upcast()),
|
||||
Err(e) => panic!("{:?}", e)
|
||||
}
|
||||
Ok(JsString::new(&mut ctx, &result_str).upcast())
|
||||
}
|
||||
}
|
||||
|
||||
pub class JsSelector for SelectorCls {
|
||||
init(mut _ctx) {
|
||||
Ok(SelectorCls { selector: Selector::new() })
|
||||
Ok(SelectorCls { node: None, value: None })
|
||||
}
|
||||
|
||||
method path(mut ctx) {
|
||||
@ -134,80 +158,35 @@ declare_types! {
|
||||
{
|
||||
let guard = ctx.lock();
|
||||
let mut this = this.borrow_mut(&guard);
|
||||
let _ = this.selector.path(&path);
|
||||
let _ = this.path(&path);
|
||||
}
|
||||
|
||||
Ok(JsUndefined::new().upcast())
|
||||
}
|
||||
|
||||
method valueFromStr(mut ctx) {
|
||||
method value(mut ctx) {
|
||||
let mut this = ctx.this();
|
||||
|
||||
let json_str = ctx.argument::<JsString>(0)?.value();
|
||||
{
|
||||
let guard = ctx.lock();
|
||||
let mut this = this.borrow_mut(&guard);
|
||||
let _ = this.selector.value_from_str(&json_str);
|
||||
let _ = this.value(&json_str);
|
||||
}
|
||||
|
||||
Ok(JsUndefined::new().upcast())
|
||||
}
|
||||
|
||||
method selectAsStr(mut ctx) {
|
||||
let mut this = ctx.this();
|
||||
method select(mut ctx) {
|
||||
let this = ctx.this();
|
||||
|
||||
let result = {
|
||||
let result_str = {
|
||||
let guard = ctx.lock();
|
||||
let this = this.borrow_mut(&guard);
|
||||
this.selector.select_as_str()
|
||||
let this = this.borrow(&guard);
|
||||
this.select()
|
||||
};
|
||||
|
||||
match result {
|
||||
Ok(json_str) => Ok(JsString::new(&mut ctx, &json_str).upcast()),
|
||||
Err(e) => panic!("{:?}", e)
|
||||
}
|
||||
}
|
||||
|
||||
method map(mut ctx) {
|
||||
let null = ctx.null();
|
||||
let mut this = ctx.this();
|
||||
|
||||
let func = ctx.argument::<JsFunction>(0)?;
|
||||
|
||||
let value = {
|
||||
let guard = ctx.lock();
|
||||
let this = this.borrow_mut(&guard);
|
||||
match this.selector.select_as_str() {
|
||||
Ok(v) => v,
|
||||
Err(e) => panic!("{:?}", e)
|
||||
}
|
||||
};
|
||||
|
||||
let js_value = JsString::new(&mut ctx, &value);
|
||||
let json_str = func.call(&mut ctx, null, vec![js_value])?
|
||||
.downcast::<JsString>()
|
||||
.or_throw(&mut ctx)?
|
||||
.value();
|
||||
{
|
||||
let guard = ctx.lock();
|
||||
let mut this = this.borrow_mut(&guard);
|
||||
let _ = this.selector.value_from_str(&json_str);
|
||||
}
|
||||
|
||||
Ok(JsUndefined::new().upcast())
|
||||
}
|
||||
|
||||
method get(mut ctx) {
|
||||
let mut this = ctx.this();
|
||||
|
||||
let result = {
|
||||
let guard = ctx.lock();
|
||||
let this = this.borrow_mut(&guard);
|
||||
match this.selector.get() {
|
||||
Ok(v) => v,
|
||||
Err(e) => panic!("{:?}", e)
|
||||
}
|
||||
};
|
||||
|
||||
Ok(JsString::new(&mut ctx, &result.to_string()).upcast())
|
||||
Ok(JsString::new(&mut ctx, &result_str).upcast())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user