Allow using ! to flatten inner arrays (#48)

Co-authored-by: folex <0xdxdy@gmail.com>
This commit is contained in:
vms 2020-12-23 19:26:03 +03:00 committed by GitHub
parent 6e5efe4903
commit 350574d0ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 36 additions and 47 deletions

2
Cargo.lock generated
View File

@ -1685,7 +1685,7 @@ dependencies = [
[[package]]
name = "stepper-interface"
version = "0.1.0"
source = "git+https://github.com/fluencelabs/aquamarine?branch=master#51754016f701aa9dd45661b4dfd78ee01533fabc"
source = "git+https://github.com/fluencelabs/aquamarine?branch=master#6e5efe4903fa6c89781ef2f20edabca281ff9011"
dependencies = [
"fluence",
"serde",

View File

@ -35,13 +35,11 @@ Args: Vec<InstructionValue<'input>> = {
"[" <args:(<Arg>)*> "]" => args
}
FPart: FunctionPart<'input> = {
<f:Function> => FunctionPart::FuncName(f),
"(" <sid:ServiceId> <f:Function> ")" => FunctionPart::ServiceIdWithFuncName(sid, f),
}
PeerPart: PeerPart<'input> = {
<pid:PeerId> => PeerPart::PeerPk(pid),
"(" <pid:PeerId> <sid:ServiceId> ")" => PeerPart::PeerPkWithServiceId(pid, sid),
@ -71,14 +69,12 @@ InstructionValue: InstructionValue<'input> = {
INIT_PEER_ID => InstructionValue::InitPeerId,
}
Alphanumeric = ALPHANUMERIC;
match {
r"[\w_-]+" => ALPHANUMERIC,
r"[\w_-]+\[\]" => ACCUMULATOR,
r#"[\w_-]+\.*\$([\w._-]*(\[[\w"]+\])*)+"# => JSON_PATH,
r#"[\w_-]+\.*\$([\w._-]*(\[[\w"]+\])*!*)+"# => JSON_PATH,
r#"%current_peer_id%"# => CURRENT_PEER_ID,
r#"%init_peer_id%"# => INIT_PEER_ID,
"seq",

File diff suppressed because one or more lines are too long

View File

@ -15,11 +15,10 @@
*/
mod iterable;
mod jvaluable_result;
mod utils;
use iterable::*;
pub(crate) use jvaluable_result::JValuable;
pub(crate) use iterable::Iterable;
pub(crate) use iterable::IterableItemType;
use super::CallEvidenceCtx;
use super::ExecutionCtx;
@ -173,7 +172,6 @@ mod tests {
use serde_json::json;
use std::rc::Rc;
// Check that
#[test]
fn lfold() {
env_logger::try_init().ok();
@ -230,9 +228,7 @@ mod tests {
)"#,
);
println!("set variables\n");
let res = call_vm!(set_variable_vm, "", rfold.clone(), "[]", "[]");
println!("execute rfold\n");
let res = call_vm!(vm, "", rfold, "[]", res.data);
let res: CallEvidencePath = serde_json::from_slice(&res.data).expect("should be valid call evidence path");
@ -292,7 +288,6 @@ mod tests {
}
#[test]
#[ignore]
fn inner_fold_with_same_iterator() {
let mut vm = create_aqua_vm(set_variable_call_service(r#"["1","2","3","4","5"]"#), "set_variable");
@ -319,7 +314,7 @@ mod tests {
let res = call_vm!(vm, "", script, "[]", "[]");
assert_eq!(res.ret_code, 14);
assert_eq!(res.ret_code, 15);
}
#[test]
@ -352,7 +347,6 @@ mod tests {
}
#[test]
#[ignore]
fn json_path() {
use crate::call_evidence::CallResult::*;
use crate::call_evidence::EvidenceState::*;
@ -367,7 +361,7 @@ mod tests {
r#"
(seq
(call "set_variable" ("" "") [] Iterable)
(fold Iterable.$["array"]! i
(fold Iterable.$.array! i
(seq
(call "A" ("" "") [i] acc[])
(next i)

View File

@ -123,14 +123,15 @@ fn handle_instruction_json_path<'ctx>(
return Ok(None);
}
let (jvalues, tetraplet_indices) = select_with_iter(acc.iter().map(|v| v.result.deref()), &path)
let acc_iter = acc.iter().map(|v| v.result.deref());
let (jvalues, tetraplet_indices) = select_with_iter(acc_iter, &path)
.map_err(|e| JValueAccJsonPathError(acc.clone(), path.to_string(), e))?;
let jvalues = jvalues.into_iter().cloned().collect();
let tetraplets = tetraplet_indices
.iter()
.map(|&id| &acc[id].triplet)
.map(|triplet| SecurityTetraplet {
triplet: triplet.clone(),
.into_iter()
.map(|id| SecurityTetraplet {
triplet: acc[id].triplet.clone(),
json_path: path.to_string(),
})
.collect::<Vec<_>>();

View File

@ -20,6 +20,9 @@ use crate::ResolvedCallResult;
use crate::Result;
use crate::SecurityTetraplet;
use jsonpath_lib::select;
use jsonpath_lib::select_with_iter;
use std::borrow::Cow;
use std::ops::Deref;
@ -44,7 +47,6 @@ pub(crate) trait JValuable {
impl<'ctx> JValuable for IterableItemType<'ctx> {
fn apply_json_path(&self, json_path: &str) -> Result<Vec<&JValue>> {
use crate::AquamarineError::JValueJsonPathError as JsonPathError;
use jsonpath_lib::select;
use IterableItemType::*;
let jvalue = match self {
@ -59,9 +61,8 @@ impl<'ctx> JValuable for IterableItemType<'ctx> {
}
fn apply_json_path_with_tetraplets(&self, json_path: &str) -> Result<(Vec<&JValue>, Vec<SecurityTetraplet>)> {
use super::fold::IterableItemType::*;
use crate::AquamarineError::JValueJsonPathError as JsonPathError;
use jsonpath_lib::select;
use IterableItemType::*;
let (jvalue, tetraplet) = match self {
RefRef((jvalue, tetraplet)) => (*jvalue, *tetraplet),
@ -112,7 +113,6 @@ impl<'ctx> JValuable for IterableItemType<'ctx> {
impl JValuable for ResolvedCallResult {
fn apply_json_path(&self, json_path: &str) -> Result<Vec<&JValue>> {
use crate::AquamarineError::JValueJsonPathError as JsonPathError;
use jsonpath_lib::select;
let selected_jvalues = select(&self.result, json_path)
.map_err(|e| JsonPathError(self.result.deref().clone(), String::from(json_path), e))?;
@ -121,7 +121,6 @@ impl JValuable for ResolvedCallResult {
fn apply_json_path_with_tetraplets(&self, json_path: &str) -> Result<(Vec<&JValue>, Vec<SecurityTetraplet>)> {
use crate::AquamarineError::JValueJsonPathError as JsonPathError;
use jsonpath_lib::select;
let selected_jvalues = select(&self.result, json_path)
.map_err(|e| JsonPathError(self.result.deref().clone(), String::from(json_path), e))?;
@ -154,23 +153,18 @@ impl JValuable for ResolvedCallResult {
impl JValuable for std::cell::Ref<'_, Vec<ResolvedCallResult>> {
fn apply_json_path(&self, json_path: &str) -> Result<Vec<&JValue>> {
use jsonpath_lib::select_with_iter;
let (selected_values, _) = select_with_iter(self.iter().map(|r| r.result.deref()), json_path).unwrap();
Ok(selected_values)
}
fn apply_json_path_with_tetraplets(&self, json_path: &str) -> Result<(Vec<&JValue>, Vec<SecurityTetraplet>)> {
use jsonpath_lib::select_with_iter;
let (selected_values, tetraplet_indices) =
select_with_iter(self.iter().map(|r| r.result.deref()), json_path).unwrap();
let tetraplets = tetraplet_indices
.into_iter()
.map(|id| self[id].triplet.clone())
.map(|triplet| SecurityTetraplet {
triplet,
.map(|id| SecurityTetraplet {
triplet: self[id].triplet.clone(),
json_path: json_path.to_string(),
})
.collect::<Vec<_>>();
@ -190,10 +184,8 @@ impl JValuable for std::cell::Ref<'_, Vec<ResolvedCallResult>> {
fn as_tetraplets(&self) -> Vec<SecurityTetraplet> {
self.iter()
// this cloned is needed because of rust-sdk allows passing arguments only by value
.map(|r| r.triplet.clone())
.map(|triplet| SecurityTetraplet {
triplet,
.map(|r| SecurityTetraplet {
triplet: r.triplet.clone(),
json_path: String::new(),
})
.collect::<Vec<_>>()

View File

@ -17,6 +17,7 @@
mod call;
mod execution_context;
mod fold;
mod jvaluable;
mod null;
mod par;
pub(crate) mod resolve;
@ -28,6 +29,7 @@ pub(crate) use fold::FoldState;
pub(self) use crate::call_evidence::CallEvidenceCtx;
pub(self) use crate::call_evidence::EvidenceState;
pub(self) use jvaluable::JValuable;
use crate::Result;

View File

@ -14,8 +14,8 @@
* limitations under the License.
*/
use super::fold::JValuable;
use super::ExecutionCtx;
use super::JValuable;
use crate::AValue;
use crate::AquamarineError;
use crate::JValue;

View File

@ -35,12 +35,14 @@ pub(crate) fn get_current_peer_id() -> std::result::Result<String, VarError> {
#[wasm_bindgen]
extern "C" {
#[allow(unused_attributes)]
#[link_name = "get_current_peer_id"]
fn get_current_peer_id_impl() -> String;
}
#[wasm_bindgen(raw_module = "../src/call_service.ts")]
#[wasm_bindgen]
extern "C" {
#[allow(unused_attributes)]
#[link_name = "call_service"]
fn call_service_impl(service_id: String, fn_name: String, args: String, security_tetraplets: String) -> String;
}

View File

@ -396,9 +396,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6"
[[package]]
name = "jsonpath_lib"
name = "jsonpath_lib-fl"
version = "0.2.5"
source = "git+https://github.com/fluencelabs/jsonpath?branch=fluence#538b433ae2ccbd882ff02fcff22ea16be779fb6f"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e81233a3c2e1f4579f1fdb856eeec115dcb23817374268212ebad691bd53e664"
dependencies = [
"array_tool",
"env_logger",
@ -743,7 +744,7 @@ dependencies = [
"air-parser",
"boolinator",
"fluence",
"jsonpath_lib",
"jsonpath_lib-fl",
"log",
"polyplets",
"serde",

View File

@ -388,9 +388,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6"
[[package]]
name = "jsonpath_lib"
name = "jsonpath_lib-fl"
version = "0.2.5"
source = "git+https://github.com/fluencelabs/jsonpath?branch=fluence#538b433ae2ccbd882ff02fcff22ea16be779fb6f"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e81233a3c2e1f4579f1fdb856eeec115dcb23817374268212ebad691bd53e664"
dependencies = [
"array_tool",
"env_logger",
@ -743,7 +744,7 @@ dependencies = [
"air-parser",
"boolinator",
"fluence",
"jsonpath_lib",
"jsonpath_lib-fl",
"log",
"polyplets",
"serde",