Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TypeAnnotatedValue<->JSON conversion result handling when ok or err is Unit #94

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading