Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #94 from golemcloud/vigoo/tav-result-unit-fix
Browse files Browse the repository at this point in the history
Fix TypeAnnotatedValue<->JSON conversion result handling when ok or err is Unit
  • Loading branch information
vigoo authored Oct 14, 2024
2 parents edc361f + bdfc060 commit af2785e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 19 deletions.
13 changes: 7 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
19 changes: 8 additions & 11 deletions wasm-rpc/src/json/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand All @@ -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)))
Expand Down
64 changes: 63 additions & 1 deletion wasm-rpc/src/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}

0 comments on commit af2785e

Please sign in to comment.