Skip to content

Commit

Permalink
Support optional result fields
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejressel committed Apr 1, 2024
1 parent 5387cfa commit d2a72e7
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 132 deletions.
63 changes: 46 additions & 17 deletions pulumi_wasm/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,34 @@ fn should_return_value_of_handled_mapped_value() -> Result<(), Error> {
}

#[test]
fn should_return_uninitialized_when_getting_nonexisting_field_during_preview() -> Result<(), Error>
{
fn should_panic_when_getting_nonexisting_required_field_not_during_preview() -> Result<(), Error> {
let (mut store, plugin) = create_engine(false)?;

let output1 = plugin
.component_pulumi_wasm_output_interface()
.output()
.call_constructor(
&mut store,
rmp_serde::to_vec(&HashMap::from([("key", "value")]))
.unwrap()
.as_slice(),
)?;

let _output2 = plugin
.component_pulumi_wasm_output_interface()
.output()
.call_get_field(&mut store, output1, "nonexisting", true)?;

let result = run_loop(&mut store, &plugin);

anyhow::ensure!(result.is_err());

Ok(())
}

#[test]
fn should_return_nothing_when_getting_nonexisting_nonrequired_field_during_preview(
) -> Result<(), Error> {
let (mut store, plugin) = create_engine(true)?;

let output1 = plugin
Expand All @@ -178,7 +204,7 @@ fn should_return_uninitialized_when_getting_nonexisting_field_during_preview() -
let output2 = plugin
.component_pulumi_wasm_output_interface()
.output()
.call_get_field(&mut store, output1, "nonexisting")?;
.call_get_field(&mut store, output1, "nonexisting", false)?;

run_loop(&mut store, &plugin)?;

Expand All @@ -187,21 +213,13 @@ fn should_return_uninitialized_when_getting_nonexisting_field_during_preview() -
.output()
.call_get_type(&mut store, output2)?;

let output2_value = plugin
.component_pulumi_wasm_output_interface()
.output()
.call_get(&mut store, output2)?;

anyhow::ensure!(output2_type == "Done");
anyhow::ensure!(output2_value.unwrap() == rmp_serde::to_vec(&None::<String>).unwrap());
anyhow::ensure!(output2_type == "Nothing");

Ok(())
}

//TODO: Implement
#[test]
#[ignore]
fn should_panic_when_getting_nonexisting_field_not_during_preview() -> Result<(), Error> {
fn should_return_null_when_getting_nonexisting_nonrequired_field() -> Result<(), Error> {
let (mut store, plugin) = create_engine(false)?;

let output1 = plugin
Expand All @@ -214,14 +232,25 @@ fn should_panic_when_getting_nonexisting_field_not_during_preview() -> Result<()
.as_slice(),
)?;

let _output2 = plugin
let output2 = plugin
.component_pulumi_wasm_output_interface()
.output()
.call_get_field(&mut store, output1, "nonexisting")?;
.call_get_field(&mut store, output1, "nonexisting", false)?;

let result = run_loop(&mut store, &plugin);
run_loop(&mut store, &plugin)?;

anyhow::ensure!(result.is_err());
let output2_type = plugin
.component_pulumi_wasm_output_interface()
.output()
.call_get_type(&mut store, output2)?;

let output2_value = plugin
.component_pulumi_wasm_output_interface()
.output()
.call_get(&mut store, output2)?;

anyhow::ensure!(output2_type == "Done");
anyhow::ensure!(output2_value.unwrap() == rmp_serde::to_vec(&None::<String>).unwrap());

Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ pub mod component {
}
impl Output {
#[allow(unused_unsafe, clippy::all)]
pub fn get_field(&self, field: &str) -> Output {
pub fn get_field(&self, field: &str, required: bool) -> Output {
unsafe {
let vec0 = field;
let ptr0 = vec0.as_ptr().cast::<u8>();
Expand All @@ -197,14 +197,22 @@ pub mod component {
#[link(wasm_import_module = "component:pulumi-wasm/[email protected]")]
extern "C" {
#[link_name = "[method]output.get-field"]
fn wit_import(_: i32, _: *mut u8, _: usize) -> i32;
fn wit_import(_: i32, _: *mut u8, _: usize, _: i32) -> i32;
}

#[cfg(not(target_arch = "wasm32"))]
fn wit_import(_: i32, _: *mut u8, _: usize) -> i32 {
fn wit_import(_: i32, _: *mut u8, _: usize, _: i32) -> i32 {
unreachable!()
}
let ret = wit_import((self).handle() as i32, ptr0.cast_mut(), len0);
let ret = wit_import(
(self).handle() as i32,
ptr0.cast_mut(),
len0,
match &required {
true => 1,
false => 0,
},
);
Output::from_handle(ret as u32)
}
}
Expand Down Expand Up @@ -1615,66 +1623,66 @@ pub(crate) use __export_main_world_impl as export;
#[cfg(target_arch = "wasm32")]
#[link_section = "component-type:wit-bindgen:0.21.0:main-world:encoded world"]
#[doc(hidden)]
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 2581] = *b"\
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\x94\x13\x01A\x02\x01\
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 2591] = *b"\
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\x9e\x13\x01A\x02\x01\
A\x15\x01B\x15\x04\0\x06output\x03\x01\x01p}\x01i\0\x01@\x01\x05value\x01\0\x02\x04\
\0\x13[constructor]output\x01\x03\x01h\0\x01@\x02\x04self\x04\x0dfunction-names\0\
\x02\x04\0\x12[method]output.map\x01\x05\x01k\x01\x01@\x01\x04self\x04\0\x06\x04\
\0\x12[method]output.get\x01\x07\x01@\x02\x04self\x04\x05fields\0\x02\x04\0\x18[\
method]output.get-field\x01\x08\x01@\x01\x04self\x04\0s\x04\0\x17[method]output.\
get-type\x01\x09\x01@\x01\x04self\x04\0\x02\x04\0\x18[method]output.duplicate\x01\
\x0a\x01@\0\0s\x04\0\x10describe-outputs\x01\x0b\x01@\0\0\x7f\x04\0\x0fnon-done-\
exists\x01\x0c\x03\x01,component:pulumi-wasm/[email protected].0\x05\0\x02\x03\
\0\0\x06output\x01B\x0b\x02\x03\x02\x01\x01\x04\0\x06output\x03\0\0\x01h\x01\x01\
r\x02\x04names\x05value\x02\x04\0\x0cobject-field\x03\0\x03\x01p\x04\x01r\x03\x04\
types\x04names\x06object\x05\x04\0\x19register-resource-request\x03\0\x06\x01i\x01\
\x01@\x01\x07request\x07\0\x08\x04\0\x08register\x01\x09\x03\x01.component:pulum\
i-wasm/[email protected]\x05\x02\x01B\x0a\x02\x03\x02\x01\x01\x04\0\x06ou\
tput\x03\0\0\x01h\x01\x01r\x02\x07keepers\x02\x06length\x02\x04\0\x04args\x03\0\x03\
\x01i\x01\x01r\x04\x06base64\x05\x03hex\x05\x07keepers\x05\x06length\x05\x04\0\x03\
res\x03\0\x06\x01@\x02\x04names\x04args\x04\0\x07\x04\0\x06invoke\x01\x08\x04\x01\
!pulumi:random/[email protected]\x05\x03\x01B\x0a\x02\x03\x02\x01\x01\x04\0\x06\
output\x03\0\0\x01h\x01\x01r\x03\x0bbyte-length\x02\x07keepers\x02\x06prefix\x02\
\x04\0\x04args\x03\0\x03\x01i\x01\x01r\x07\x07b64-std\x05\x07b64-url\x05\x0bbyte\
-length\x05\x03dec\x05\x03hex\x05\x07keepers\x05\x06prefix\x05\x04\0\x03res\x03\0\
\x06\x01@\x02\x04names\x04args\x04\0\x07\x04\0\x06invoke\x01\x08\x04\x01\x1epulu\
mi:random/[email protected]\x05\x04\x01B\x0a\x02\x03\x02\x01\x01\x04\0\x06output\x03\
\0\0\x01h\x01\x01r\x04\x07keepers\x02\x03max\x02\x03min\x02\x04seed\x02\x04\0\x04\
args\x03\0\x03\x01i\x01\x01r\x05\x07keepers\x05\x03max\x05\x03min\x05\x06result\x05\
\x04seed\x05\x04\0\x03res\x03\0\x06\x01@\x02\x04names\x04args\x04\0\x07\x04\0\x06\
invoke\x01\x08\x04\x01#pulumi:random/[email protected]\x05\x05\x01B\x0a\x02\x03\
\x02\x01\x01\x04\0\x06output\x03\0\0\x01h\x01\x01r\x0c\x07keepers\x02\x06length\x02\
\x05lower\x02\x09min-lower\x02\x0bmin-numeric\x02\x0bmin-special\x02\x09min-uppe\
r\x02\x06number\x02\x07numeric\x02\x10override-special\x02\x07special\x02\x05upp\
er\x02\x04\0\x04args\x03\0\x03\x01i\x01\x01r\x0e\x0bbcrypt-hash\x05\x07keepers\x05\
\x06length\x05\x05lower\x05\x09min-lower\x05\x0bmin-numeric\x05\x0bmin-special\x05\
\x09min-upper\x05\x06number\x05\x07numeric\x05\x10override-special\x05\x06result\
\x05\x07special\x05\x05upper\x05\x04\0\x03res\x03\0\x06\x01@\x02\x04names\x04arg\
s\x04\0\x07\x04\0\x06invoke\x01\x08\x04\x01$pulumi:random/[email protected]\
\x05\x06\x01B\x0a\x02\x03\x02\x01\x01\x04\0\x06output\x03\0\0\x01h\x01\x01r\x04\x07\
keepers\x02\x06length\x02\x06prefix\x02\x09separator\x02\x04\0\x04args\x03\0\x03\
\x01i\x01\x01r\x04\x07keepers\x05\x06length\x05\x06prefix\x05\x09separator\x05\x04\
\0\x03res\x03\0\x06\x01@\x02\x04names\x04args\x04\0\x07\x04\0\x06invoke\x01\x08\x04\
\x01\x1fpulumi:random/[email protected]\x05\x07\x01B\x0a\x02\x03\x02\x01\x01\x04\
\0\x06output\x03\0\0\x01h\x01\x01r\x04\x06inputs\x02\x07keepers\x02\x0cresult-co\
unt\x02\x04seed\x02\x04\0\x04args\x03\0\x03\x01i\x01\x01r\x05\x06inputs\x05\x07k\
eepers\x05\x0cresult-count\x05\x07results\x05\x04seed\x05\x04\0\x03res\x03\0\x06\
\x01@\x02\x04names\x04args\x04\0\x07\x04\0\x06invoke\x01\x08\x04\x01#pulumi:rand\
om/[email protected]\x05\x08\x01B\x0a\x02\x03\x02\x01\x01\x04\0\x06output\x03\
\0\0\x01h\x01\x01r\x0c\x07keepers\x02\x06length\x02\x05lower\x02\x09min-lower\x02\
\x0bmin-numeric\x02\x0bmin-special\x02\x09min-upper\x02\x06number\x02\x07numeric\
\x02\x10override-special\x02\x07special\x02\x05upper\x02\x04\0\x04args\x03\0\x03\
\x01i\x01\x01r\x0d\x07keepers\x05\x06length\x05\x05lower\x05\x09min-lower\x05\x0b\
min-numeric\x05\x0bmin-special\x05\x09min-upper\x05\x06number\x05\x07numeric\x05\
\x10override-special\x05\x06result\x05\x07special\x05\x05upper\x05\x04\0\x03res\x03\
\0\x06\x01@\x02\x04names\x04args\x04\0\x07\x04\0\x06invoke\x01\x08\x04\x01\"pulu\
mi:random/[email protected]\x05\x09\x01B\x0a\x02\x03\x02\x01\x01\x04\0\x06out\
put\x03\0\0\x01h\x01\x01r\x01\x07keepers\x02\x04\0\x04args\x03\0\x03\x01i\x01\x01\
r\x02\x07keepers\x05\x06result\x05\x04\0\x03res\x03\0\x06\x01@\x02\x04names\x04a\
rgs\x04\0\x07\x04\0\x06invoke\x01\x08\x04\x01\x20pulumi:random/[email protected].\
0\x05\x0a\x04\x01\x1fpulumi:random/[email protected]\x04\0\x0b\x10\x01\0\x0amain\
-world\x03\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dwit-component\x070.201\
.0\x10wit-bindgen-rust\x060.21.0";
\0\x12[method]output.get\x01\x07\x01@\x03\x04self\x04\x05fields\x08required\x7f\0\
\x02\x04\0\x18[method]output.get-field\x01\x08\x01@\x01\x04self\x04\0s\x04\0\x17\
[method]output.get-type\x01\x09\x01@\x01\x04self\x04\0\x02\x04\0\x18[method]outp\
ut.duplicate\x01\x0a\x01@\0\0s\x04\0\x10describe-outputs\x01\x0b\x01@\0\0\x7f\x04\
\0\x0fnon-done-exists\x01\x0c\x03\x01,component:pulumi-wasm/[email protected]\
.0\x05\0\x02\x03\0\0\x06output\x01B\x0b\x02\x03\x02\x01\x01\x04\0\x06output\x03\0\
\0\x01h\x01\x01r\x02\x04names\x05value\x02\x04\0\x0cobject-field\x03\0\x03\x01p\x04\
\x01r\x03\x04types\x04names\x06object\x05\x04\0\x19register-resource-request\x03\
\0\x06\x01i\x01\x01@\x01\x07request\x07\0\x08\x04\0\x08register\x01\x09\x03\x01.\
component:pulumi-wasm/[email protected]\x05\x02\x01B\x0a\x02\x03\x02\x01\x01\
\x04\0\x06output\x03\0\0\x01h\x01\x01r\x02\x07keepers\x02\x06length\x02\x04\0\x04\
args\x03\0\x03\x01i\x01\x01r\x04\x06base64\x05\x03hex\x05\x07keepers\x05\x06leng\
th\x05\x04\0\x03res\x03\0\x06\x01@\x02\x04names\x04args\x04\0\x07\x04\0\x06invok\
e\x01\x08\x04\x01!pulumi:random/[email protected]\x05\x03\x01B\x0a\x02\x03\x02\
\x01\x01\x04\0\x06output\x03\0\0\x01h\x01\x01r\x03\x0bbyte-length\x02\x07keepers\
\x02\x06prefix\x02\x04\0\x04args\x03\0\x03\x01i\x01\x01r\x07\x07b64-std\x05\x07b\
64-url\x05\x0bbyte-length\x05\x03dec\x05\x03hex\x05\x07keepers\x05\x06prefix\x05\
\x04\0\x03res\x03\0\x06\x01@\x02\x04names\x04args\x04\0\x07\x04\0\x06invoke\x01\x08\
\x04\x01\x1epulumi:random/[email protected]\x05\x04\x01B\x0a\x02\x03\x02\x01\x01\x04\
\0\x06output\x03\0\0\x01h\x01\x01r\x04\x07keepers\x02\x03max\x02\x03min\x02\x04s\
eed\x02\x04\0\x04args\x03\0\x03\x01i\x01\x01r\x05\x07keepers\x05\x03max\x05\x03m\
in\x05\x06result\x05\x04seed\x05\x04\0\x03res\x03\0\x06\x01@\x02\x04names\x04arg\
s\x04\0\x07\x04\0\x06invoke\x01\x08\x04\x01#pulumi:random/[email protected]\x05\
\x05\x01B\x0a\x02\x03\x02\x01\x01\x04\0\x06output\x03\0\0\x01h\x01\x01r\x0c\x07k\
eepers\x02\x06length\x02\x05lower\x02\x09min-lower\x02\x0bmin-numeric\x02\x0bmin\
-special\x02\x09min-upper\x02\x06number\x02\x07numeric\x02\x10override-special\x02\
\x07special\x02\x05upper\x02\x04\0\x04args\x03\0\x03\x01i\x01\x01r\x0e\x0bbcrypt\
-hash\x05\x07keepers\x05\x06length\x05\x05lower\x05\x09min-lower\x05\x0bmin-nume\
ric\x05\x0bmin-special\x05\x09min-upper\x05\x06number\x05\x07numeric\x05\x10over\
ride-special\x05\x06result\x05\x07special\x05\x05upper\x05\x04\0\x03res\x03\0\x06\
\x01@\x02\x04names\x04args\x04\0\x07\x04\0\x06invoke\x01\x08\x04\x01$pulumi:rand\
om/[email protected]\x05\x06\x01B\x0a\x02\x03\x02\x01\x01\x04\0\x06output\x03\
\0\0\x01h\x01\x01r\x04\x07keepers\x02\x06length\x02\x06prefix\x02\x09separator\x02\
\x04\0\x04args\x03\0\x03\x01i\x01\x01r\x04\x07keepers\x05\x06length\x05\x06prefi\
x\x05\x09separator\x05\x04\0\x03res\x03\0\x06\x01@\x02\x04names\x04args\x04\0\x07\
\x04\0\x06invoke\x01\x08\x04\x01\x1fpulumi:random/[email protected]\x05\x07\x01B\
\x0a\x02\x03\x02\x01\x01\x04\0\x06output\x03\0\0\x01h\x01\x01r\x04\x06inputs\x02\
\x07keepers\x02\x0cresult-count\x02\x04seed\x02\x04\0\x04args\x03\0\x03\x01i\x01\
\x01r\x05\x06inputs\x05\x07keepers\x05\x0cresult-count\x05\x07results\x05\x04see\
d\x05\x04\0\x03res\x03\0\x06\x01@\x02\x04names\x04args\x04\0\x07\x04\0\x06invoke\
\x01\x08\x04\x01#pulumi:random/[email protected]\x05\x08\x01B\x0a\x02\x03\x02\
\x01\x01\x04\0\x06output\x03\0\0\x01h\x01\x01r\x0c\x07keepers\x02\x06length\x02\x05\
lower\x02\x09min-lower\x02\x0bmin-numeric\x02\x0bmin-special\x02\x09min-upper\x02\
\x06number\x02\x07numeric\x02\x10override-special\x02\x07special\x02\x05upper\x02\
\x04\0\x04args\x03\0\x03\x01i\x01\x01r\x0d\x07keepers\x05\x06length\x05\x05lower\
\x05\x09min-lower\x05\x0bmin-numeric\x05\x0bmin-special\x05\x09min-upper\x05\x06\
number\x05\x07numeric\x05\x10override-special\x05\x06result\x05\x07special\x05\x05\
upper\x05\x04\0\x03res\x03\0\x06\x01@\x02\x04names\x04args\x04\0\x07\x04\0\x06in\
voke\x01\x08\x04\x01\"pulumi:random/[email protected]\x05\x09\x01B\x0a\x02\x03\
\x02\x01\x01\x04\0\x06output\x03\0\0\x01h\x01\x01r\x01\x07keepers\x02\x04\0\x04a\
rgs\x03\0\x03\x01i\x01\x01r\x02\x07keepers\x05\x06result\x05\x04\0\x03res\x03\0\x06\
\x01@\x02\x04names\x04args\x04\0\x07\x04\0\x06invoke\x01\x08\x04\x01\x20pulumi:r\
andom/[email protected].0\x05\x0a\x04\x01\x1fpulumi:random/[email protected]\x04\0\
\x0b\x10\x01\0\x0amain-world\x03\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0d\
wit-component\x070.201.0\x10wit-bindgen-rust\x060.21.0";

#[inline(never)]
#[doc(hidden)]
Expand Down
Loading

0 comments on commit d2a72e7

Please sign in to comment.