mirror of
https://github.com/fluencelabs/jsonpath
synced 2025-06-22 12:21:37 +00:00
jsonpath-rs add "map", "get" function
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "jsonpath4nodejs"
|
||||
version = "0.1.2"
|
||||
version = "0.1.3"
|
||||
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.12"
|
||||
jsonpath_lib = "0.1.13"
|
||||
neon = "0.2.0"
|
||||
neon-serde = "0.1.1"
|
||||
serde_json = { version = "1.0", features = ["preserve_order"] }
|
||||
|
@ -139,7 +139,7 @@ declare_types! {
|
||||
Ok(JsUndefined::new().upcast())
|
||||
}
|
||||
|
||||
method value_from_str(mut ctx) {
|
||||
method valueFromStr(mut ctx) {
|
||||
let mut this = ctx.this();
|
||||
|
||||
let json_str = ctx.argument::<JsString>(0)?.value();
|
||||
@ -151,13 +151,13 @@ declare_types! {
|
||||
Ok(JsUndefined::new().upcast())
|
||||
}
|
||||
|
||||
method select_to_str(mut ctx) {
|
||||
method selectAsStr(mut ctx) {
|
||||
let mut this = ctx.this();
|
||||
|
||||
let result = {
|
||||
let guard = ctx.lock();
|
||||
let mut this = this.borrow_mut(&guard);
|
||||
this.selector.select_to_str()
|
||||
this.selector.select_as_str()
|
||||
};
|
||||
|
||||
match result {
|
||||
@ -165,6 +165,50 @@ declare_types! {
|
||||
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 mut 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 mut 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())
|
||||
}
|
||||
}
|
||||
}
|
||||
register_module!(mut m, {
|
||||
|
Reference in New Issue
Block a user