This is an automated email from the ASF dual-hosted git repository.

hsun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-teaclave.git

commit d8e38dce9ef66b9e9dfa9e573aa6d76f0e33bf7e
Author: GeminiCarrie <springgem...@163.com>
AuthorDate: Thu Jun 15 09:27:10 2023 +0000

    Fix deserializing function parameters of the rust SDK
---
 config/Cargo.lock                          | 156 +++++++++++++++++++++++++++++
 executor/src/wamr.rs                       |   4 +-
 sdk/rust/src/lib.rs                        |   2 +-
 services/management/enclave/src/lib.rs     |   1 +
 services/management/enclave/src/service.rs |  69 +++++++++++++
 types/src/function.rs                      |   3 +
 types/src/staged_function.rs               |  15 ++-
 7 files changed, 238 insertions(+), 12 deletions(-)

diff --git a/config/Cargo.lock b/config/Cargo.lock
new file mode 100644
index 00000000..c2f0fd61
--- /dev/null
+++ b/config/Cargo.lock
@@ -0,0 +1,156 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "anyhow"
+version = "1.0.71"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8"
+
+[[package]]
+name = "form_urlencoded"
+version = "1.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+checksum = "a62bc1cf6f830c2ec14a513a9fb124d0a213a629668a4186f329db21fe045652"
+dependencies = [
+ "percent-encoding",
+]
+
+[[package]]
+name = "idna"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+checksum = "7d20d6b07bfbc108882d88ed8e37d39636dcc260e15e30c45e6ba089610b917c"
+dependencies = [
+ "unicode-bidi",
+ "unicode-normalization",
+]
+
+[[package]]
+name = "log"
+version = "0.4.19"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4"
+
+[[package]]
+name = "percent-encoding"
+version = "2.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94"
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.63"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+checksum = "7b368fba921b0dce7e60f5e04ec15e565b3303972b42bcfde1d0713b881959eb"
+dependencies = [
+ "unicode-ident",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.28"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+checksum = "1b9ab9c7eadfd8df19006f1cf1a4aed13540ed5cbc047010ece5826e10825488"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "serde"
+version = "1.0.164"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+checksum = "9e8c8cf938e98f769bc164923b06dce91cea1751522f46f8466461af04c9027d"
+dependencies = [
+ "serde_derive",
+]
+
+[[package]]
+name = "serde_derive"
+version = "1.0.164"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+checksum = "d9735b638ccc51c28bf6914d90a2e9725b377144fc612c49a611fddd1b631d68"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "syn"
+version = "2.0.22"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+checksum = "2efbeae7acf4eabd6bcdcbd11c92f45231ddda7539edc7806bd1a04a03b24616"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
+
+[[package]]
+name = "teaclave_config"
+version = "0.6.0"
+dependencies = [
+ "anyhow",
+ "log",
+ "serde",
+ "toml",
+ "url",
+]
+
+[[package]]
+name = "tinyvec"
+version = "1.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50"
+dependencies = [
+ "tinyvec_macros",
+]
+
+[[package]]
+name = "tinyvec_macros"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
+
+[[package]]
+name = "toml"
+version = "0.5.11"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234"
+dependencies = [
+ "serde",
+]
+
+[[package]]
+name = "unicode-bidi"
+version = "0.3.13"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+checksum = "92888ba5573ff080736b3648696b70cafad7d250551175acbaa4e0385b3e1460"
+
+[[package]]
+name = "unicode-ident"
+version = "1.0.9"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+checksum = "b15811caf2415fb889178633e7724bad2509101cde276048e013b9def5e51fa0"
+
+[[package]]
+name = "unicode-normalization"
+version = "0.1.22"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921"
+dependencies = [
+ "tinyvec",
+]
+
+[[package]]
+name = "url"
+version = "2.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+checksum = "50bff7831e19200a85b17131d085c25d7811bc4e186efdaf54bbd132994a88cb"
+dependencies = [
+ "form_urlencoded",
+ "idna",
+ "percent-encoding",
+]
diff --git a/executor/src/wamr.rs b/executor/src/wamr.rs
index 71b32ebd..0738ad03 100644
--- a/executor/src/wamr.rs
+++ b/executor/src/wamr.rs
@@ -275,7 +275,7 @@ pub mod tests {
         let mut args = HashMap::new();
 
         args.insert("input_img".to_string(), "input_img".to_string());
-        let args = FunctionArguments::from(args);
+        let args = FunctionArguments::from_map(args);
 
         let mut file =
             
fs::File::open("../../examples/python/wasm_tvm_mnist_payload/mnist.wasm").unwrap();
@@ -310,7 +310,7 @@ pub mod tests {
         args.insert("input_file_id1".to_string(), "pf_in_a".to_string());
         args.insert("input_file_id2".to_string(), "pf_in_b".to_string());
         args.insert("output_file_id".to_string(), "pf_out".to_string());
-        let args = FunctionArguments::from(args);
+        let args = FunctionArguments::from_map(args);
 
         let mut file = fs::File::open(
             
"../../examples/python/wasm_c_millionaire_problem_payload/millionaire_problem.wasm",
diff --git a/sdk/rust/src/lib.rs b/sdk/rust/src/lib.rs
index 752e50f4..e3293a92 100644
--- a/sdk/rust/src/lib.rs
+++ b/sdk/rust/src/lib.rs
@@ -404,7 +404,7 @@ impl FrontendClient {
     pub fn create_task(
         &mut self,
         function_id: &str,
-        function_arguments: Option<HashMap<String, String>>,
+        function_arguments: Option<HashMap<String, serde_json::value::Value>>,
         executor: &str,
         inputs_ownership: Option<HashMap<String, Vec<String>>>,
         outputs_ownership: Option<HashMap<String, Vec<String>>>,
diff --git a/services/management/enclave/src/lib.rs 
b/services/management/enclave/src/lib.rs
index 4f5e75ad..f33ead01 100644
--- a/services/management/enclave/src/lib.rs
+++ b/services/management/enclave/src/lib.rs
@@ -146,6 +146,7 @@ pub mod tests {
             service::tests::handle_output_file,
             service::tests::handle_function,
             service::tests::check_function_quota,
+            service::tests::deserialize_function_arguments,
             service::tests::handle_task,
             service::tests::handle_staged_task,
             audit::tests::test_entry_doc_conversion,
diff --git a/services/management/enclave/src/service.rs 
b/services/management/enclave/src/service.rs
index 7d182a32..944555ac 100644
--- a/services/management/enclave/src/service.rs
+++ b/services/management/enclave/src/service.rs
@@ -1324,4 +1324,73 @@ pub mod tests {
         let deserialized_data = StagedTask::from_slice(&value).unwrap();
         debug!("staged task: {:?}", deserialized_data);
     }
+
+    #[derive(serde::Deserialize, Debug)]
+    struct TestFunctionArguments {
+        arg_bool: bool,
+        arg_usize: usize,
+    }
+
+    pub fn deserialize_function_arguments() {
+        let arguments = vec![
+            FunctionArgument::new("arg_bool", "", true),
+            FunctionArgument::new("arg_usize", "", true),
+        ];
+        let function = FunctionBuilder::new()
+            .id(Uuid::new_v4())
+            .name("mock_function")
+            .description("mock function")
+            .arguments(arguments)
+            .public(true)
+            .owner("mock_user")
+            .build();
+        let request = CreateTaskRequest::new()
+            .function_arguments(hashmap!("arg_bool" => true,"arg_usize" => 10))
+            .executor(Executor::Builtin);
+        let task = Task::<Create>::new(
+            "mock_user".into(),
+            request.executor.try_into().unwrap(),
+            request.function_arguments.try_into().unwrap(),
+            from_proto_ownership(request.inputs_ownership),
+            from_proto_ownership(request.outputs_ownership),
+            function,
+        )
+        .unwrap();
+        let ts: TaskState = task.try_into().unwrap();
+        let deserialized_argument: TestFunctionArguments =
+            
serde_json::from_str(&ts.function_arguments.into_string()).unwrap();
+        assert!(deserialized_argument.arg_bool);
+        assert_eq!(deserialized_argument.arg_usize, 10);
+
+        let arguments = vec![
+            FunctionArgument::new("arg_bool", "true", true),
+            FunctionArgument::new("arg_usize", "10", false),
+        ];
+        let function = FunctionBuilder::new()
+            .id(Uuid::new_v4())
+            .name("mock_function2")
+            .description("mock function")
+            .arguments(arguments)
+            .public(true)
+            .owner("mock_user")
+            .build();
+        let request = CreateTaskRequest::new()
+            .function_arguments(hashmap!("arg_bool" => false))
+            .executor(Executor::Builtin);
+        let task = Task::<Create>::new(
+            "mock_user".into(),
+            request.executor.try_into().unwrap(),
+            request.function_arguments.try_into().unwrap(),
+            from_proto_ownership(request.inputs_ownership),
+            from_proto_ownership(request.outputs_ownership),
+            function,
+        )
+        .unwrap();
+        let ts: TaskState = task.try_into().unwrap();
+        let result =
+            
serde_json::from_str::<TestFunctionArguments>(&ts.function_arguments.into_string());
+        assert!(result.is_err());
+        let err_msg = format!("{:?}", result.unwrap_err());
+        assert!(err_msg.contains("invalid type: string \\\"10\\\", expected 
usize"));
+    }
 }
diff --git a/types/src/function.rs b/types/src/function.rs
index 13e0f331..0cef59fc 100644
--- a/types/src/function.rs
+++ b/types/src/function.rs
@@ -182,6 +182,9 @@ impl Storable for Function {
     }
 }
 
+// FIXME: If the argument type is not a string, 'allow_overwrite' should be
+// set to true when registering a function and the argument value should be
+// provided when creating a task based on the funcion.
 #[derive(Debug, Deserialize, Serialize, Clone)]
 pub struct FunctionArgument {
     pub key: String,
diff --git a/types/src/staged_function.rs b/types/src/staged_function.rs
index d0ee4923..1dafe26b 100644
--- a/types/src/staged_function.rs
+++ b/types/src/staged_function.rs
@@ -30,13 +30,9 @@ pub struct FunctionArguments {
     inner: serde_json::Map<String, ArgumentValue>,
 }
 
-impl From<HashMap<String, String>> for FunctionArguments {
-    fn from(map: HashMap<String, String>) -> Self {
-        let inner = map.iter().fold(serde_json::Map::new(), |mut acc, (k, v)| {
-            acc.insert(k.to_owned(), v.to_owned().into());
-            acc
-        });
-
+impl From<HashMap<String, ArgumentValue>> for FunctionArguments {
+    fn from(map: HashMap<String, ArgumentValue>) -> Self {
+        let inner = map.into_iter().collect();
         Self { inner }
     }
 }
@@ -65,8 +61,9 @@ impl FunctionArguments {
         Ok(Self { inner })
     }
 
-    pub fn from_map(map: HashMap<String, String>) -> Self {
-        map.into()
+    pub fn from_map<T: Into<ArgumentValue>>(map: HashMap<String, T>) -> Self {
+        let inner = map.into_iter().map(|(k, v)| (k, v.into())).collect();
+        Self { inner }
     }
 
     pub fn inner(&self) -> &serde_json::Map<String, ArgumentValue> {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@teaclave.apache.org
For additional commands, e-mail: commits-h...@teaclave.apache.org

Reply via email to