From f022a2dec4229f1dd739794e98a0e5d080ea13d5 Mon Sep 17 00:00:00 2001 From: vms Date: Tue, 22 Dec 2020 22:45:20 +0300 Subject: [PATCH] Implicitly flatten array with one JValue according to a function signature (#54) --- Cargo.lock | 60 ++++++++++++------------ aquamarine-vm/Cargo.toml | 4 +- fluence-app-service/Cargo.toml | 4 +- fluence-faas/Cargo.toml | 2 +- fluence-faas/src/misc/json_to_ivalues.rs | 35 +++++++++----- fluence-faas/tests/arguments_passing.rs | 14 ++++++ tools/repl/Cargo.toml | 4 +- 7 files changed, 73 insertions(+), 50 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 73cbdb70..90da3459 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -41,7 +41,7 @@ checksum = "68803225a7b13e47191bab76f2687382b60d259e8cf37f6e1893658b84bb9479" [[package]] name = "aquamarine-vm" -version = "0.1.5" +version = "0.1.6" dependencies = [ "fluence-faas", "maplit", @@ -722,6 +722,15 @@ dependencies = [ "serde_json", ] +[[package]] +name = "fluence" +version = "0.2.13" +source = "git+https://github.com/fluencelabs/rust-sdk#71591f412cb65879d74e8c38838e827ab74d8802" +dependencies = [ + "fluence-sdk-macro 0.2.13 (git+https://github.com/fluencelabs/rust-sdk)", + "fluence-sdk-main 0.2.13 (git+https://github.com/fluencelabs/rust-sdk)", +] + [[package]] name = "fluence" version = "0.2.13" @@ -732,18 +741,9 @@ dependencies = [ "fluence-sdk-main 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "fluence" -version = "0.2.13" -source = "git+https://github.com/fluencelabs/rust-sdk#71591f412cb65879d74e8c38838e827ab74d8802" -dependencies = [ - "fluence-sdk-macro 0.2.13 (git+https://github.com/fluencelabs/rust-sdk)", - "fluence-sdk-main 0.2.13 (git+https://github.com/fluencelabs/rust-sdk)", -] - [[package]] name = "fluence-app-service" -version = "0.1.17" +version = "0.1.18" dependencies = [ "fluence-faas", "log", @@ -757,7 +757,7 @@ dependencies = [ [[package]] name = "fluence-faas" -version = "0.1.17" +version = "0.1.18" dependencies = [ "cmd_lib", "env_logger 0.7.1", @@ -779,6 +779,14 @@ dependencies = [ "wasmer-wasi-fl", ] +[[package]] +name = "fluence-sdk-macro" +version = "0.2.13" +source = "git+https://github.com/fluencelabs/rust-sdk#71591f412cb65879d74e8c38838e827ab74d8802" +dependencies = [ + "fluence-sdk-wit 0.2.13 (git+https://github.com/fluencelabs/rust-sdk)", +] + [[package]] name = "fluence-sdk-macro" version = "0.2.13" @@ -789,11 +797,13 @@ dependencies = [ ] [[package]] -name = "fluence-sdk-macro" +name = "fluence-sdk-main" version = "0.2.13" source = "git+https://github.com/fluencelabs/rust-sdk#71591f412cb65879d74e8c38838e827ab74d8802" dependencies = [ - "fluence-sdk-wit 0.2.13 (git+https://github.com/fluencelabs/rust-sdk)", + "fluence-sdk-macro 0.2.13 (git+https://github.com/fluencelabs/rust-sdk)", + "log", + "serde", ] [[package]] @@ -807,21 +817,10 @@ dependencies = [ "serde", ] -[[package]] -name = "fluence-sdk-main" -version = "0.2.13" -source = "git+https://github.com/fluencelabs/rust-sdk#71591f412cb65879d74e8c38838e827ab74d8802" -dependencies = [ - "fluence-sdk-macro 0.2.13 (git+https://github.com/fluencelabs/rust-sdk)", - "log", - "serde", -] - [[package]] name = "fluence-sdk-wit" version = "0.2.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8da5f51cddeae52ff5b91d1a5d8be90e54629f4887f89f8d7501b829b374fe6a" +source = "git+https://github.com/fluencelabs/rust-sdk#71591f412cb65879d74e8c38838e827ab74d8802" dependencies = [ "proc-macro2", "quote", @@ -834,7 +833,8 @@ dependencies = [ [[package]] name = "fluence-sdk-wit" version = "0.2.13" -source = "git+https://github.com/fluencelabs/rust-sdk#71591f412cb65879d74e8c38838e827ab74d8802" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8da5f51cddeae52ff5b91d1a5d8be90e54629f4887f89f8d7501b829b374fe6a" dependencies = [ "proc-macro2", "quote", @@ -877,7 +877,7 @@ dependencies = [ [[package]] name = "frepl" -version = "0.1.22" +version = "0.1.23" dependencies = [ "anyhow", "clap", @@ -2205,9 +2205,9 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" [[package]] name = "stepper-interface" version = "0.1.0" -source = "git+https://github.com/fluencelabs/aquamarine#b967a63abca9742d6764e9b27926025d5e2b2efb" +source = "git+https://github.com/fluencelabs/aquamarine#724117547205d8ccc742d325b542af8f3df801b8" dependencies = [ - "fluence 0.2.13 (git+https://github.com/fluencelabs/rust-sdk)", + "fluence 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", "serde", "wasmer-interface-types-fl", ] diff --git a/aquamarine-vm/Cargo.toml b/aquamarine-vm/Cargo.toml index 897b13b9..58e635c1 100644 --- a/aquamarine-vm/Cargo.toml +++ b/aquamarine-vm/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "aquamarine-vm" description = "Fluence Aquamarine VM" -version = "0.1.5" +version = "0.1.6" authors = ["Fluence Labs"] edition = "2018" license = "Apache-2.0" @@ -11,7 +11,7 @@ name = "aquamarine_vm" path = "src/lib.rs" [dependencies] -fluence-faas = { path = "../fluence-faas", version = "0.1.17" } +fluence-faas = { path = "../fluence-faas", version = "0.1.18" } stepper-interface = { git = "https://github.com/fluencelabs/aquamarine", branch = "master" } maplit = "1.0.2" diff --git a/fluence-app-service/Cargo.toml b/fluence-app-service/Cargo.toml index a21693af..308a79af 100644 --- a/fluence-app-service/Cargo.toml +++ b/fluence-app-service/Cargo.toml @@ -1,13 +1,13 @@ [package] name = "fluence-app-service" description = "Fluence Application Service" -version = "0.1.17" +version = "0.1.18" authors = ["Fluence Labs"] license = "Apache-2.0" edition = "2018" [dependencies] -fluence-faas = { path = "../fluence-faas", version = "0.1.17" } +fluence-faas = { path = "../fluence-faas", version = "0.1.18" } maplit = "1.0.2" log = "0.4.8" diff --git a/fluence-faas/Cargo.toml b/fluence-faas/Cargo.toml index 11695e66..1e4f1730 100644 --- a/fluence-faas/Cargo.toml +++ b/fluence-faas/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "fluence-faas" description = "Fluence FaaS" -version = "0.1.17" +version = "0.1.18" authors = ["Fluence Labs"] license = "Apache-2.0" edition = "2018" diff --git a/fluence-faas/src/misc/json_to_ivalues.rs b/fluence-faas/src/misc/json_to_ivalues.rs index 05b456b3..4b2a9282 100644 --- a/fluence-faas/src/misc/json_to_ivalues.rs +++ b/fluence-faas/src/misc/json_to_ivalues.rs @@ -37,8 +37,8 @@ pub(crate) fn json_to_ivalues<'a, 'b>( JValue::Array(json_array) => { json_array_to_ivalues(json_array, arg_types.map(|arg| arg.1), &record_types)? } - JValue::Null => json_null_to_ivalue(arg_types)?, - json_value => json_value_to_ivalue(json_value, arg_types)?, + JValue::Null => json_null_to_ivalues(arg_types)?, + json_value => json_value_to_ivalues(json_value, arg_types)?, }; Ok(ivalues) @@ -95,23 +95,27 @@ fn json_array_to_ivalues<'a, 'b>( } /// Convert json value (Number, String or Bool) to an array of ivalues according to the supplied argument types. -fn json_value_to_ivalue<'a>( +fn json_value_to_ivalues<'a>( json_value: JValue, mut arg_types: impl Iterator + ExactSizeIterator, ) -> Result> { if arg_types.len() != 1 { return Err(ArgDeError(format!( - "the called function has the following signature: {:?}, but only one string argument is provided", - arg_types.collect::>() + "called function has the following signature: '{:?}', and it isn't suitable for an argument '{:?}' provided", + arg_types.collect::>(), + json_value, ))); } - let ivalue = jvalue_to_ivalue(json_value, arg_types.next().unwrap().1, &HashMap::new())?; + // unwrap is safe here because iterator size's been checked + let arg_type = arg_types.next().unwrap().1; + let ivalue = jvalue_to_ivalue(json_value, arg_type, &HashMap::new())?; + Ok(vec![ivalue]) } /// Convert json Null to an empty array of ivalues. -fn json_null_to_ivalue<'a>( +fn json_null_to_ivalues<'a>( arg_types: impl Iterator + ExactSizeIterator, ) -> Result> { if arg_types.len() != 0 { @@ -129,12 +133,17 @@ fn jvalue_to_ivalue(jvalue: JValue, ty: &IType, record_types: &RecordTypes) -> R macro_rules! to_ivalue( ($json_value:expr, $ty:ident) => { { - let value = serde_json::from_value($json_value).map_err(|e| { - ArgDeError(format!( - "error {:?} occurred while deserialize output result to a json value", - e - )) - })?; + let value = match $json_value { + // if there is an array with only one element try to implicitly flatten it, + // this is needed mostly because jsonpath lib returns Vec<&JValue> and + // could be changed in future + JValue::Array(mut json_array) if json_array.len() == 1 => { + serde_json::from_value(json_array.remove(0)) + }, + jvalue => serde_json::from_value(jvalue), + }.map_err(|e| + ArgDeError(format!("error {:?} occurred while deserialize output result to a json value",e)) + )?; Ok(IValue::$ty(value)) } diff --git a/fluence-faas/tests/arguments_passing.rs b/fluence-faas/tests/arguments_passing.rs index d3e46754..284d90ef 100644 --- a/fluence-faas/tests/arguments_passing.rs +++ b/fluence-faas/tests/arguments_passing.rs @@ -359,6 +359,9 @@ pub fn i32_type() { let result4 = call_faas!(faas, "arguments_passing_pure", "i32_type", json!(1)); assert_eq!(result4, right_result); + + let result5 = call_faas!(faas, "arguments_passing_pure", "i32_type", json!([[1]])); + assert_eq!(result5, right_result); } #[test] @@ -393,6 +396,9 @@ pub fn i64_type() { let result4 = call_faas!(faas, "arguments_passing_pure", "i64_type", json!(1)); assert_eq!(result4, right_result); + + let result5 = call_faas!(faas, "arguments_passing_pure", "i64_type", json!([1])); + assert_eq!(result5, right_result); } #[test] @@ -607,6 +613,14 @@ pub fn bytearray_type() { json!([[0x13, 0x37]]) ); assert_eq!(result4, right_result); + + let result5 = call_faas!( + faas, + "arguments_passing_pure", + "bytearray_type", + json!([[0x13]]) + ); + assert_eq!(result5, json!([0x13, 1, 1])); } #[test] diff --git a/tools/repl/Cargo.toml b/tools/repl/Cargo.toml index db7e0ff3..f0964dec 100644 --- a/tools/repl/Cargo.toml +++ b/tools/repl/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "frepl" description = "Fluence FCE REPL intended for testing purposes" -version = "0.1.22" +version = "0.1.23" authors = ["Fluence Labs"] repository = "https://github.com/fluencelabs/fce/tools/repl" license = "Apache-2.0" @@ -12,7 +12,7 @@ name = "fce-repl" path = "src/main.rs" [dependencies] -fluence-app-service = { path = "../../fluence-app-service", version = "0.1.17", features = ["raw-module-api"] } +fluence-app-service = { path = "../../fluence-app-service", version = "0.1.18", features = ["raw-module-api"] } fluence-sdk-main = { version = "=0.2.13", features = ["logger"] } anyhow = "1.0.31"