Skip to content

Commit

Permalink
NodeJs 0.1.7 - support Selector(0.1.9)
Browse files Browse the repository at this point in the history
  • Loading branch information
freestrings committed Apr 9, 2019
1 parent ceadbce commit d263e30
Show file tree
Hide file tree
Showing 6 changed files with 699 additions and 77 deletions.
40 changes: 39 additions & 1 deletion nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,52 @@ Build from source instead of using pre-built binary, and if Rust is not installe

> Not yet tested in Windows
## 목차
## APIs

* [jsonpath.Selector](#jsonpathselector)
* [jsonpath.select(json: string|object, jsonpath: string)](#json-stringobject-jsonpath-string)
* [jsonpath.compile(jsonpath: string)](#compilejsonpath-string)
* [jsonpath.selector(json: string|object)](#selectorjson-stringobject)
* [Simple time check](https://github.com/freestrings/jsonpath/wiki/Simple-timecheck-jsonpath-native)
* [Other Examples](https://github.com/freestrings/jsonpath/wiki/Javascript-examples)

### jsonpath.Selector

```javascript
let jsonObj = {
"school": {
"friends": [
{"name": "친구1", "age": 20},
{"name": "친구2", "age": 20}
]
},
"friends": [
{"name": "친구3", "age": 30},
{"name": "친구4"}
]
};

let selector = new jsonpath.Selector().value(jsonObj);

{
let jsonObj = selector.path('$..[?(@.age >= 30)]').selectTo();
let resultObj = [{"name": "친구3", "age": 30}];
console.log(JSON.stringify(jsonObj) === JSON.stringify(resultObj));
}

{
let jsonObj = selector.path('$..[?(@.age == 20)]').selectTo();
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 resultObj = [{"name": "친구5", "age": 20}];
console.log(JSON.stringify(jsonObj) === JSON.stringify(resultObj));
}
```

### jsonpath.select(json: string|object, jsonpath: string)

```javascript
Expand Down
40 changes: 35 additions & 5 deletions nodejs/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { Compile, Selector, selectStr } = require('../native');
const { CompileFn, SelectorFn, selectStr, Selector: _Selector } = require('../native');

function compile(path) {
let compile = new Compile(path);
let compile = new CompileFn(path);
return (json) => {
if(typeof json != 'string') {
json = JSON.stringify(json)
Expand All @@ -14,9 +14,9 @@ function selector(json) {
if(typeof json != 'string') {
json = JSON.stringify(json)
}
let selector = new Selector(json);
let selector = new SelectorFn(json);
return (path) => {
return JSON.parse(selector.selector(path));
return JSON.parse(selector.select(path));
}
}

Expand All @@ -27,8 +27,38 @@ function select(json, path) {
return JSON.parse(selectStr(json, path));
}

class Selector {
constructor() {
this._selector = new _Selector();
return this;
}

path(path) {
this._selector.path(path);
return this;
}

value(json) {
if(typeof json != 'string') {
json = JSON.stringify(json)
}
this._selector.value_from_str(json);
return this;
}

selectToStr() {
return this._selector.select_to_str();
}

selectTo() {
return JSON.parse(this.selectToStr());
}

}

module.exports = {
compile,
selector,
select
select,
Selector
};
2 changes: 1 addition & 1 deletion nodejs/native/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ exclude = ["artifacts.json", "index.node"]
neon-build = "0.2.0"

[dependencies]
jsonpath_lib = "0.1.8"
jsonpath_lib = "0.1.9"
neon = "0.2.0"
neon-serde = "0.1.1"
serde_json = { version = "1.0", features = ["preserve_order"] }
Expand Down
70 changes: 60 additions & 10 deletions nodejs/native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ extern crate neon;
extern crate neon_serde;
extern crate serde_json;

use std::ops::Deref;

use jsonpath::filter::value_filter::JsonValueFilter;
use jsonpath::parser::parser::{Node, NodeVisitor, Parser};
use jsonpath::ref_value::model::{RefValue, RefValueWrapper};
use jsonpath::Selector;
use neon::prelude::*;
use serde_json::Value;
use std::ops::Deref;

///
/// `neon_serde::from_value` has very poor performance.
Expand All @@ -35,16 +35,20 @@ fn select_str(mut ctx: FunctionContext) -> JsResult<JsValue> {
}
}

pub struct Compile {
pub struct CompileFn {
node: Node
}

pub struct Selector {
pub struct SelectorFn {
json: RefValueWrapper
}

pub struct SelectorCls {
selector: Selector
}

declare_types! {
pub class JsCompile for Compile {
pub class JsCompileFn for CompileFn {
init(mut ctx) {
let path = ctx.argument::<JsString>(0)?.value();
let mut parser = Parser::new(path.as_str());
Expand All @@ -54,7 +58,7 @@ declare_types! {
Err(e) => panic!("{:?}", e)
};

Ok(Compile { node })
Ok(CompileFn { node })
}

method template(mut ctx) {
Expand All @@ -81,18 +85,18 @@ declare_types! {
}
}

pub class JsSelector for Selector {
pub class JsSelectorFn for SelectorFn {
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)
};

Ok(Selector { json: ref_value.into() })
Ok(SelectorFn { json: ref_value.into() })
}

method selector(mut ctx) {
method select(mut ctx) {
let this = ctx.this();

let json = {
Expand All @@ -117,9 +121,55 @@ declare_types! {
}
}
}

pub class JsSelector for SelectorCls {
init(mut _ctx) {
Ok(SelectorCls { selector: Selector::new() })
}

method path(mut ctx) {
let mut this = ctx.this();

let path = ctx.argument::<JsString>(0)?.value();
{
let guard = ctx.lock();
let mut this = this.borrow_mut(&guard);
let _ = this.selector.path(&path);
}
Ok(JsUndefined::new().upcast())
}

method value_from_str(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);
}
Ok(JsUndefined::new().upcast())
}

method select_to_str(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()
};

match result {
Ok(json_str) => Ok(JsString::new(&mut ctx, &json_str).upcast()),
Err(e) => panic!("{:?}", e)
}
}
}
}
register_module!(mut m, {
m.export_class::<JsCompile>("Compile").expect("Compile class error");
m.export_class::<JsCompileFn>("CompileFn").expect("CompileFn class error");
m.export_class::<JsSelectorFn>("SelectorFn").expect("SelectorFn class error");
m.export_class::<JsSelector>("Selector").expect("Selector class error");
m.export_function("select", select)?;
m.export_function("selectStr", select_str)?;
Expand Down
3 changes: 2 additions & 1 deletion nodejs/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "jsonpath-rs",
"version": "0.1.6",
"version": "0.1.7",
"description": "It is JsonPath implementation. The core implementation is written in Rust",
"author": "Changseok Han <[email protected]>",
"license": "MIT",
"keywords": [
"jsonpath",
"rust-addon",
"rust-binding",
"rust",
"rustlang",
Expand Down
Loading

0 comments on commit d263e30

Please sign in to comment.