Skip to content

Commit 5907c6b

Browse files
Better optional values handling (#430)
Closes #414
1 parent 974a800 commit 5907c6b

File tree

19 files changed

+394
-33
lines changed

19 files changed

+394
-33
lines changed

examples/simple/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ fn test_main() -> Result<(), Error> {
1919
// Tests number mapping
2020
let number = random_string.min_upper.map(|i| i * 2);
2121

22+
// Optional values are deserialized as None
23+
let keepers = random_string.keepers.map(|map| format!("Keepers: {map:?}"));
24+
2225
let val1 = Output::new(&1);
2326
let val2 = Output::new(&"abc".to_string());
2427

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

35+
let random_string_2 = random_string::create(
36+
"test_2",
37+
RandomStringArgs::builder()
38+
.length(keepers.map(|s| s.len() as i32))
39+
.build_struct(),
40+
);
41+
3242
add_export("result", &random_string.result);
3343
add_export("transformed_result", &t);
3444
add_export("number", &number);
3545
add_export("combined_string", &combined_string);
3646
add_export("combined_2_string", &combined_2_string);
47+
add_export("keepers", &keepers);
48+
add_export("result_2", &random_string_2.result);
3749
Ok(())
3850
}

examples/simple/tests/test.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,25 @@ fn test_integration() -> Result<(), anyhow::Error> {
7373
.as_str()
7474
.ok_or(anyhow!("[combined_2_string] is not a string"))?;
7575

76+
let keepers = stack
77+
.pointer("/keepers")
78+
.ok_or(anyhow!("Cannot find [keepers] in stack export"))?
79+
.as_str()
80+
.ok_or(anyhow!("[keepers] is not a string"))?;
81+
82+
let result_2 = stack
83+
.pointer("/result_2")
84+
.ok_or(anyhow!("Cannot find [result_2] in stack export"))?
85+
.as_str()
86+
.ok_or(anyhow!("[result_2] is not a string"))?;
87+
7688
assert_eq!(result.len(), 36);
7789
assert_eq!(transformed_result, format!("Result: {}", result));
7890
assert_eq!(number, 0);
7991
assert_eq!(combined_string, "Values: (1, \"abc\")");
8092
assert_eq!(combined_2_string, "Values: (1, \"abc\")");
93+
assert_eq!(keepers, "Keepers: None");
94+
assert_eq!(result_2.len(), 13);
8195

8296
Ok(())
8397
}

providers/pulumi_wasm_provider_cloudflare/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod resource;
22
mod function;
33

44
#[allow(unused_braces)]
5+
#[allow(unused_imports)]
56
mod bindings;
67
bindings::export!(Component with_types_in bindings);
78

providers/pulumi_wasm_provider_docker/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod resource;
22
mod function;
33

44
#[allow(unused_braces)]
5+
#[allow(unused_imports)]
56
mod bindings;
67
bindings::export!(Component with_types_in bindings);
78

providers/pulumi_wasm_provider_random/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod resource;
22

33
#[allow(unused_braces)]
4+
#[allow(unused_imports)]
45
mod bindings;
56
bindings::export!(Component with_types_in bindings);
67

pulumi_wasm/src/bindings.rs

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,6 +1271,37 @@ pub mod exports {
12711271
);
12721272
}
12731273
}
1274+
#[allow(dead_code)]
1275+
pub mod pulumi_wasm_external {
1276+
#[allow(dead_code, clippy::all)]
1277+
pub mod pulumi_settings {
1278+
#[used]
1279+
#[doc(hidden)]
1280+
static __FORCE_SECTION_REF: fn() = super::super::super::super::__link_custom_section_describing_imports;
1281+
use super::super::super::super::_rt;
1282+
#[doc(hidden)]
1283+
#[allow(non_snake_case)]
1284+
pub unsafe fn _export_set_in_preview_cabi<T: Guest>(arg0: i32) {
1285+
#[cfg(target_arch = "wasm32")] _rt::run_ctors_once();
1286+
T::set_in_preview(_rt::bool_lift(arg0 as u8));
1287+
}
1288+
pub trait Guest {
1289+
fn set_in_preview(in_preview: bool);
1290+
}
1291+
#[doc(hidden)]
1292+
macro_rules! __export_component_pulumi_wasm_external_pulumi_settings_0_0_0_stable_dev_cabi {
1293+
($ty:ident with_types_in $($path_to_types:tt)*) => {
1294+
const _ : () = { #[export_name =
1295+
"component:pulumi-wasm-external/[email protected]#set-in-preview"]
1296+
unsafe extern "C" fn export_set_in_preview(arg0 : i32,) {
1297+
$($path_to_types)*:: _export_set_in_preview_cabi::<$ty > (arg0) }
1298+
};
1299+
};
1300+
}
1301+
#[doc(hidden)]
1302+
pub(crate) use __export_component_pulumi_wasm_external_pulumi_settings_0_0_0_stable_dev_cabi;
1303+
}
1304+
}
12741305
}
12751306
}
12761307
mod _rt {
@@ -1474,17 +1505,20 @@ macro_rules! __export_pulumi_wasm_impl {
14741505
exports::component::pulumi_wasm::register_interface); $($path_to_types_root)*::
14751506
exports::component::pulumi_wasm::stack_interface::__export_component_pulumi_wasm_stack_interface_0_0_0_dev_cabi!($ty
14761507
with_types_in $($path_to_types_root)*::
1477-
exports::component::pulumi_wasm::stack_interface);
1508+
exports::component::pulumi_wasm::stack_interface); $($path_to_types_root)*::
1509+
exports::component::pulumi_wasm_external::pulumi_settings::__export_component_pulumi_wasm_external_pulumi_settings_0_0_0_stable_dev_cabi!($ty
1510+
with_types_in $($path_to_types_root)*::
1511+
exports::component::pulumi_wasm_external::pulumi_settings);
14781512
};
14791513
}
14801514
#[doc(inline)]
14811515
pub(crate) use __export_pulumi_wasm_impl as export;
14821516
#[cfg(target_arch = "wasm32")]
14831517
#[link_section = "component-type:wit-bindgen:0.30.0:pulumi-wasm:encoded world"]
14841518
#[doc(hidden)]
1485-
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 1775] = *b"\
1486-
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xed\x0c\x01A\x02\x01\
1487-
A\x0b\x01B\x0a\x01m\x05\x05TRACE\x05DEBUG\x04INFO\x04WARN\x05ERROR\x04\0\x05leve\
1519+
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 1882] = *b"\
1520+
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xd8\x0d\x01A\x02\x01\
1521+
A\x0d\x01B\x0a\x01m\x05\x05TRACE\x05DEBUG\x04INFO\x04WARN\x05ERROR\x04\0\x05leve\
14881522
l\x03\0\0\x01ks\x01ky\x01o\x02ss\x01p\x04\x01r\x07\x05level\x01\x06targets\x04ar\
14891523
gss\x0bmodule-path\x02\x04file\x02\x04line\x03\x0akey-values\x05\x04\0\x07conten\
14901524
t\x03\0\x06\x01@\x01\x07content\x07\x01\0\x04\0\x03log\x01\x08\x03\x013component\
@@ -1517,9 +1551,11 @@ d\x02\x0bfunction-ids\x05values\x04\0\x1bfunction-invocation-request\x03\0\x03\x
15171551
h\x01\x01r\x02\x02id\x05\x05values\x04\0\x1afunction-invocation-result\x03\0\x06\
15181552
\x01@\x02\x04names\x05value\x05\x01\0\x04\0\x0aadd-export\x01\x08\x01p\x07\x01p\x04\
15191553
\x01@\x01\x09functions\x09\0\x0a\x04\0\x06finish\x01\x0b\x04\x01/component:pulum\
1520-
i-wasm/[email protected]\x05\x05\x04\x01+component:pulumi-wasm/pulumi-wa\
1521-
[email protected]\x04\0\x0b\x11\x01\0\x0bpulumi-wasm\x03\0\0\0G\x09producers\x01\x0cp\
1522-
rocessed-by\x02\x0dwit-component\x070.215.0\x10wit-bindgen-rust\x060.30.0";
1554+
i-wasm/[email protected]\x05\x05\x01B\x02\x01@\x01\x0ain-preview\x7f\x01\
1555+
\0\x04\0\x0eset-in-preview\x01\0\x04\x01?component:pulumi-wasm-external/pulumi-s\
1556+
[email protected]\x05\x06\x04\x01+component:pulumi-wasm/[email protected]\
1557+
-DEV\x04\0\x0b\x11\x01\0\x0bpulumi-wasm\x03\0\0\0G\x09producers\x01\x0cprocessed\
1558+
-by\x02\x0dwit-component\x070.215.0\x10wit-bindgen-rust\x060.30.0";
15231559
#[inline(never)]
15241560
#[doc(hidden)]
15251561
pub fn __link_custom_section_describing_imports() {

pulumi_wasm/src/lib.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use crate::bindings::exports::component::pulumi_wasm::stack_interface::{
1616
use crate::bindings::exports::component::pulumi_wasm::{
1717
output_interface, register_interface, stack_interface,
1818
};
19+
use crate::bindings::exports::component::pulumi_wasm_external::pulumi_settings;
20+
use std::sync::atomic::{AtomicBool, Ordering};
1921

2022
bindings::export!(Component with_types_in bindings);
2123

@@ -74,11 +76,14 @@ impl stack_interface::Guest for Component {
7476
}
7577
})
7678
.collect()
79+
}
80+
}
7781

78-
// vec![]
82+
static GLOBAL_BOOL: AtomicBool = AtomicBool::new(false);
7983

80-
// true
81-
// finalizer::finish()
84+
impl pulumi_settings::Guest for Component {
85+
fn set_in_preview(in_preview: bool) {
86+
GLOBAL_BOOL.store(in_preview, Ordering::SeqCst);
8287
}
8388
}
8489

@@ -122,6 +127,7 @@ impl register_interface::Guest for Component {
122127
request.name.to_string(),
123128
object,
124129
outputs,
130+
GLOBAL_BOOL.load(Ordering::SeqCst),
125131
);
126132

127133
RegisterResourceResult {
@@ -153,10 +159,12 @@ impl register_interface::Guest for Component {
153159
})
154160
.collect::<HashMap<_, _>>();
155161

156-
let (_, field_outputs) =
157-
refcell
158-
.borrow_mut()
159-
.create_resource_invoke_node(request.token, object, outputs);
162+
let (_, field_outputs) = refcell.borrow_mut().create_resource_invoke_node(
163+
request.token,
164+
object,
165+
outputs,
166+
GLOBAL_BOOL.load(Ordering::SeqCst),
167+
);
160168

161169
ResourceInvokeResult {
162170
fields: field_outputs

0 commit comments

Comments
 (0)