Skip to content

Commit

Permalink
Better optional values handling (#430)
Browse files Browse the repository at this point in the history
Closes #414
  • Loading branch information
andrzejressel authored Oct 5, 2024
1 parent 974a800 commit 5907c6b
Show file tree
Hide file tree
Showing 19 changed files with 394 additions and 33 deletions.
12 changes: 12 additions & 0 deletions examples/simple/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ fn test_main() -> Result<(), Error> {
// Tests number mapping
let number = random_string.min_upper.map(|i| i * 2);

// Optional values are deserialized as None
let keepers = random_string.keepers.map(|map| format!("Keepers: {map:?}"));

let val1 = Output::new(&1);
let val2 = Output::new(&"abc".to_string());

Expand All @@ -29,10 +32,19 @@ fn test_main() -> Result<(), Error> {
let combined_string = combined.map(|values| format!("Values: {values:?}"));
let combined_2_string = combined_2.map(|values| format!("Values: {values:?}"));

let random_string_2 = random_string::create(
"test_2",
RandomStringArgs::builder()
.length(keepers.map(|s| s.len() as i32))
.build_struct(),
);

add_export("result", &random_string.result);
add_export("transformed_result", &t);
add_export("number", &number);
add_export("combined_string", &combined_string);
add_export("combined_2_string", &combined_2_string);
add_export("keepers", &keepers);
add_export("result_2", &random_string_2.result);
Ok(())
}
14 changes: 14 additions & 0 deletions examples/simple/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,25 @@ fn test_integration() -> Result<(), anyhow::Error> {
.as_str()
.ok_or(anyhow!("[combined_2_string] is not a string"))?;

let keepers = stack
.pointer("/keepers")
.ok_or(anyhow!("Cannot find [keepers] in stack export"))?
.as_str()
.ok_or(anyhow!("[keepers] is not a string"))?;

let result_2 = stack
.pointer("/result_2")
.ok_or(anyhow!("Cannot find [result_2] in stack export"))?
.as_str()
.ok_or(anyhow!("[result_2] is not a string"))?;

assert_eq!(result.len(), 36);
assert_eq!(transformed_result, format!("Result: {}", result));
assert_eq!(number, 0);
assert_eq!(combined_string, "Values: (1, \"abc\")");
assert_eq!(combined_2_string, "Values: (1, \"abc\")");
assert_eq!(keepers, "Keepers: None");
assert_eq!(result_2.len(), 13);

Ok(())
}
1 change: 1 addition & 0 deletions providers/pulumi_wasm_provider_cloudflare/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod resource;
mod function;

#[allow(unused_braces)]
#[allow(unused_imports)]
mod bindings;
bindings::export!(Component with_types_in bindings);

Expand Down
1 change: 1 addition & 0 deletions providers/pulumi_wasm_provider_docker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod resource;
mod function;

#[allow(unused_braces)]
#[allow(unused_imports)]
mod bindings;
bindings::export!(Component with_types_in bindings);

Expand Down
1 change: 1 addition & 0 deletions providers/pulumi_wasm_provider_random/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod resource;

#[allow(unused_braces)]
#[allow(unused_imports)]
mod bindings;
bindings::export!(Component with_types_in bindings);

Expand Down
50 changes: 43 additions & 7 deletions pulumi_wasm/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,37 @@ pub mod exports {
);
}
}
#[allow(dead_code)]
pub mod pulumi_wasm_external {
#[allow(dead_code, clippy::all)]
pub mod pulumi_settings {
#[used]
#[doc(hidden)]
static __FORCE_SECTION_REF: fn() = super::super::super::super::__link_custom_section_describing_imports;
use super::super::super::super::_rt;
#[doc(hidden)]
#[allow(non_snake_case)]
pub unsafe fn _export_set_in_preview_cabi<T: Guest>(arg0: i32) {
#[cfg(target_arch = "wasm32")] _rt::run_ctors_once();
T::set_in_preview(_rt::bool_lift(arg0 as u8));
}
pub trait Guest {
fn set_in_preview(in_preview: bool);
}
#[doc(hidden)]
macro_rules! __export_component_pulumi_wasm_external_pulumi_settings_0_0_0_stable_dev_cabi {
($ty:ident with_types_in $($path_to_types:tt)*) => {
const _ : () = { #[export_name =
"component:pulumi-wasm-external/[email protected]#set-in-preview"]
unsafe extern "C" fn export_set_in_preview(arg0 : i32,) {
$($path_to_types)*:: _export_set_in_preview_cabi::<$ty > (arg0) }
};
};
}
#[doc(hidden)]
pub(crate) use __export_component_pulumi_wasm_external_pulumi_settings_0_0_0_stable_dev_cabi;
}
}
}
}
mod _rt {
Expand Down Expand Up @@ -1474,17 +1505,20 @@ macro_rules! __export_pulumi_wasm_impl {
exports::component::pulumi_wasm::register_interface); $($path_to_types_root)*::
exports::component::pulumi_wasm::stack_interface::__export_component_pulumi_wasm_stack_interface_0_0_0_dev_cabi!($ty
with_types_in $($path_to_types_root)*::
exports::component::pulumi_wasm::stack_interface);
exports::component::pulumi_wasm::stack_interface); $($path_to_types_root)*::
exports::component::pulumi_wasm_external::pulumi_settings::__export_component_pulumi_wasm_external_pulumi_settings_0_0_0_stable_dev_cabi!($ty
with_types_in $($path_to_types_root)*::
exports::component::pulumi_wasm_external::pulumi_settings);
};
}
#[doc(inline)]
pub(crate) use __export_pulumi_wasm_impl as export;
#[cfg(target_arch = "wasm32")]
#[link_section = "component-type:wit-bindgen:0.30.0:pulumi-wasm:encoded world"]
#[doc(hidden)]
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 1775] = *b"\
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xed\x0c\x01A\x02\x01\
A\x0b\x01B\x0a\x01m\x05\x05TRACE\x05DEBUG\x04INFO\x04WARN\x05ERROR\x04\0\x05leve\
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 1882] = *b"\
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xd8\x0d\x01A\x02\x01\
A\x0d\x01B\x0a\x01m\x05\x05TRACE\x05DEBUG\x04INFO\x04WARN\x05ERROR\x04\0\x05leve\
l\x03\0\0\x01ks\x01ky\x01o\x02ss\x01p\x04\x01r\x07\x05level\x01\x06targets\x04ar\
gss\x0bmodule-path\x02\x04file\x02\x04line\x03\x0akey-values\x05\x04\0\x07conten\
t\x03\0\x06\x01@\x01\x07content\x07\x01\0\x04\0\x03log\x01\x08\x03\x013component\
Expand Down Expand Up @@ -1517,9 +1551,11 @@ d\x02\x0bfunction-ids\x05values\x04\0\x1bfunction-invocation-request\x03\0\x03\x
h\x01\x01r\x02\x02id\x05\x05values\x04\0\x1afunction-invocation-result\x03\0\x06\
\x01@\x02\x04names\x05value\x05\x01\0\x04\0\x0aadd-export\x01\x08\x01p\x07\x01p\x04\
\x01@\x01\x09functions\x09\0\x0a\x04\0\x06finish\x01\x0b\x04\x01/component:pulum\
i-wasm/[email protected]\x05\x05\x04\x01+component:pulumi-wasm/pulumi-wa\
[email protected]\x04\0\x0b\x11\x01\0\x0bpulumi-wasm\x03\0\0\0G\x09producers\x01\x0cp\
rocessed-by\x02\x0dwit-component\x070.215.0\x10wit-bindgen-rust\x060.30.0";
i-wasm/[email protected]\x05\x05\x01B\x02\x01@\x01\x0ain-preview\x7f\x01\
\0\x04\0\x0eset-in-preview\x01\0\x04\x01?component:pulumi-wasm-external/pulumi-s\
[email protected]\x05\x06\x04\x01+component:pulumi-wasm/[email protected]\
-DEV\x04\0\x0b\x11\x01\0\x0bpulumi-wasm\x03\0\0\0G\x09producers\x01\x0cprocessed\
-by\x02\x0dwit-component\x070.215.0\x10wit-bindgen-rust\x060.30.0";
#[inline(never)]
#[doc(hidden)]
pub fn __link_custom_section_describing_imports() {
Expand Down
22 changes: 15 additions & 7 deletions pulumi_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use crate::bindings::exports::component::pulumi_wasm::stack_interface::{
use crate::bindings::exports::component::pulumi_wasm::{
output_interface, register_interface, stack_interface,
};
use crate::bindings::exports::component::pulumi_wasm_external::pulumi_settings;
use std::sync::atomic::{AtomicBool, Ordering};

bindings::export!(Component with_types_in bindings);

Expand Down Expand Up @@ -74,11 +76,14 @@ impl stack_interface::Guest for Component {
}
})
.collect()
}
}

// vec![]
static GLOBAL_BOOL: AtomicBool = AtomicBool::new(false);

// true
// finalizer::finish()
impl pulumi_settings::Guest for Component {
fn set_in_preview(in_preview: bool) {
GLOBAL_BOOL.store(in_preview, Ordering::SeqCst);
}
}

Expand Down Expand Up @@ -122,6 +127,7 @@ impl register_interface::Guest for Component {
request.name.to_string(),
object,
outputs,
GLOBAL_BOOL.load(Ordering::SeqCst),
);

RegisterResourceResult {
Expand Down Expand Up @@ -153,10 +159,12 @@ impl register_interface::Guest for Component {
})
.collect::<HashMap<_, _>>();

let (_, field_outputs) =
refcell
.borrow_mut()
.create_resource_invoke_node(request.token, object, outputs);
let (_, field_outputs) = refcell.borrow_mut().create_resource_invoke_node(
request.token,
object,
outputs,
GLOBAL_BOOL.load(Ordering::SeqCst),
);

ResourceInvokeResult {
fields: field_outputs
Expand Down
Loading

0 comments on commit 5907c6b

Please sign in to comment.