diff --git a/Cargo.lock b/Cargo.lock index 8d63fcc1..0576eaca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4401,9 +4401,9 @@ dependencies = [ [[package]] name = "test-r" -version = "0.0.6" +version = "0.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce4f9076be412de335c06832a0a72b3261a2a91a2925f9d47363643e7b7a015a" +checksum = "7f503de241983649990311e28573ce1e98f62ed65f2b6fb7bdf2496d2ba1c978" dependencies = [ "ctor", "test-r-core", @@ -4413,9 +4413,9 @@ dependencies = [ [[package]] name = "test-r-core" -version = "0.0.6" +version = "0.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2ad71c85134f87fc11617d92fec4320d3154ecd117871a866116813d6a1ece8" +checksum = "bf42476bec5da760737467562f6169dc126d3716b74ca072f08246fbcb332000" dependencies = [ "anstream", "anstyle", @@ -4434,13 +4434,14 @@ dependencies = [ [[package]] name = "test-r-macro" -version = "0.0.6" +version = "0.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a638ef4ccf6d0d5b92d06e5c827ef4e941eb59bf4192101a3b8c29b7540a878" +checksum = "198b6d8536c41236c3bc5dec066dd4d63ff01fab76596a9738d8bf60b050afdb" dependencies = [ "heck 0.5.0", "proc-macro2", "quote", + "rand", "syn 2.0.79", "test-r-core", ] diff --git a/Cargo.toml b/Cargo.toml index 1eaba42b..f5fc08dc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,5 +20,5 @@ opt-level = 's' fs_extra = "1.3.0" golem-wasm-ast = "1.0.1" tempfile = "3.12.0" -test-r = { version = "0.0.6", default-features = false } +test-r = { version = "0.0.11", default-features = false } tokio = "1.38.0" \ No newline at end of file diff --git a/wasm-rpc/src/json/impl.rs b/wasm-rpc/src/json/impl.rs index 188a22c8..33847d8f 100644 --- a/wasm-rpc/src/json/impl.rs +++ b/wasm-rpc/src/json/impl.rs @@ -449,16 +449,14 @@ fn get_result( Some(value) => { let value = validate(ok_type, value)?; - let result_value = value.map(|value| { - ResultValue::OkValue(Box::new(protobuf::TypeAnnotatedValue { - type_annotated_value: Some(value.deref().clone()), - })) - }); + let result_value = ResultValue::OkValue(Box::new(protobuf::TypeAnnotatedValue { + type_annotated_value: value.map(|value| value.deref().clone()), + })); let typed_result = protobuf::TypedResult { ok: ok_type.clone().map(|x| x.deref().into()), error: err_type.clone().map(|x| x.deref().into()), - result_value, + result_value: Some(result_value), }; Ok(TypeAnnotatedValue::Result(Box::new(typed_result))) @@ -467,16 +465,15 @@ fn get_result( Some(value) => { let value = validate(err_type, value)?; - let result_value = value.map(|value| { + let result_value = ResultValue::ErrorValue(Box::new(protobuf::TypeAnnotatedValue { - type_annotated_value: Some(value.deref().clone()), - })) - }); + type_annotated_value: value.map(|value| value.deref().clone()), + })); let typed_result = protobuf::TypedResult { ok: ok_type.clone().map(|x| x.deref().into()), error: err_type.clone().map(|x| x.deref().into()), - result_value, + result_value: Some(result_value), }; Ok(TypeAnnotatedValue::Result(Box::new(typed_result))) diff --git a/wasm-rpc/src/json/mod.rs b/wasm-rpc/src/json/mod.rs index 60952e6e..a373b472 100644 --- a/wasm-rpc/src/json/mod.rs +++ b/wasm-rpc/src/json/mod.rs @@ -100,7 +100,7 @@ mod tests { use crate::protobuf::type_annotated_value::TypeAnnotatedValue; use crate::{TypeAnnotatedValueConstructors, Value}; - use golem_wasm_ast::analysis::analysed_type::{str, tuple, u32}; + use golem_wasm_ast::analysis::analysed_type::{result_err, result_ok, str, tuple, u32}; use serde_json::json; @@ -129,4 +129,66 @@ mod tests { let tav2: TypeAnnotatedValue = serde_json::from_value(json).unwrap(); assert_eq!(tav, tav2); } + + #[test] + fn example2() { + let tav = TypeAnnotatedValue::create( + &Value::Tuple(vec![Value::Result(Ok(None))]), + &tuple(vec![result_err(str())]), + ) + .unwrap(); + let json = serde_json::to_value(&tav).unwrap(); + assert_eq!( + json, + json!({ + "typ": { + "type": "Tuple", + "items": [ + { + "type": "Result", + "err": { + "type": "Str" + }, + "ok": null + }, + ] + }, + "value": [{ "ok": null }] + }) + ); + + let tav2: TypeAnnotatedValue = serde_json::from_value(json).unwrap(); + assert_eq!(tav, tav2); + } + + #[test] + fn example3() { + let tav = TypeAnnotatedValue::create( + &Value::Tuple(vec![Value::Result(Err(None))]), + &tuple(vec![result_ok(str())]), + ) + .unwrap(); + let json = serde_json::to_value(&tav).unwrap(); + assert_eq!( + json, + json!({ + "typ": { + "type": "Tuple", + "items": [ + { + "type": "Result", + "ok": { + "type": "Str" + }, + "err": null + }, + ] + }, + "value": [{ "err": null }] + }) + ); + + let tav2: TypeAnnotatedValue = serde_json::from_value(json).unwrap(); + assert_eq!(tav, tav2); + } }