mirror of
https://github.com/fluencelabs/jsonpath
synced 2025-06-20 19:36:37 +00:00
Javascript Selector 유닛테스트 완료
This commit is contained in:
@ -7,17 +7,19 @@ extern crate wasm_bindgen;
|
||||
extern crate web_sys;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::ops::Deref;
|
||||
use std::result::Result;
|
||||
use std::sync::Mutex;
|
||||
use std::ops::Deref;
|
||||
|
||||
use cfg_if::cfg_if;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use web_sys::console;
|
||||
|
||||
use jsonpath::filter::value_filter::JsonValueFilter;
|
||||
use jsonpath::parser::parser::{Node, NodeVisitor, Parser};
|
||||
use jsonpath::ref_value::model::{RefValue, RefValueWrapper};
|
||||
use jsonpath::Selector as _Selector;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use web_sys::console;
|
||||
|
||||
use std::result;
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "wee_alloc")] {
|
||||
@ -85,8 +87,8 @@ lazy_static! {
|
||||
static ref CACHE_JSON_IDX: Mutex<usize> = Mutex::new(0);
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn alloc_json(js_value: JsValue) -> usize {
|
||||
#[wasm_bindgen(js_name = allocJson)]
|
||||
pub extern fn alloc_json(js_value: JsValue) -> usize {
|
||||
match into_serde_json(&js_value) {
|
||||
Ok(json) => {
|
||||
let mut map = CACHE_JSON.lock().unwrap();
|
||||
@ -106,8 +108,8 @@ pub fn alloc_json(js_value: JsValue) -> usize {
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn dealloc_json(ptr: usize) -> bool {
|
||||
#[wasm_bindgen(js_name = deallocJson)]
|
||||
pub extern fn dealloc_json(ptr: usize) -> bool {
|
||||
let mut map = CACHE_JSON.lock().unwrap();
|
||||
map.remove(&ptr).is_some()
|
||||
}
|
||||
@ -167,5 +169,49 @@ pub fn select(js_value: JsValue, path: &str) -> JsValue {
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
/// `wasm_bindgen` 제약으로 builder-pattern을 구사 할 수 없다.
|
||||
///
|
||||
#[wasm_bindgen]
|
||||
pub struct Selector {
|
||||
selector: _Selector
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl Selector {
|
||||
|
||||
#[wasm_bindgen(constructor)]
|
||||
pub fn new() -> Self {
|
||||
Selector { selector: _Selector::new() }
|
||||
}
|
||||
|
||||
#[wasm_bindgen(catch)]
|
||||
pub fn path(&mut self, path: &str) -> result::Result<(), JsValue> {
|
||||
let _ = self.selector.path(path)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[wasm_bindgen(catch)]
|
||||
pub fn value(&mut self, value: JsValue) -> result::Result<(), JsValue> {
|
||||
let ref_value = into_serde_json(&value)?;
|
||||
let _ = self.selector.value(ref_value)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[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()?;
|
||||
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>()
|
||||
.map_err(|e| JsValue::from_str(&e))?;
|
||||
Ok(JsValue::from_serde(&ref_value)
|
||||
.map_err(|e| JsValue::from_str(&format!("{:?}", e)))?)
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn testa() {}
|
Reference in New Issue
Block a user