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

Support for unions #557

Merged
merged 15 commits into from
Dec 14, 2024
  •  
  •  
  •  
33 changes: 33 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ members = [
"examples/docker",
"examples/multiple_providers",
"examples/simple",
"examples/typesystem",
# DO NOT EDIT - START
"providers/pulumi_wasm_provider_docker",
"providers/pulumi_wasm_provider_docker_rust",
Expand All @@ -26,6 +27,7 @@ members = [
"pulumi_wasm_generator",
"pulumi_wasm_generator_lib",
"pulumi_wasm_proto",
"pulumi_wasm_provider_common",
"pulumi_wasm_runner",
"pulumi_wasm_runner_component_creator",
"pulumi_wasm_rust",
Expand All @@ -50,6 +52,8 @@ pulumi_wasm_common = { version = "=0.0.0-DEV", path = "pulumi_wasm_common", regi
pulumi_wasm_proto = { version = "=0.0.0-DEV", path = "pulumi_wasm_proto", registry = "cloudsmith" }
pulumi_wasm_wit = { version = "=0.0.0-DEV", path = "pulumi_wasm_wit", registry = "cloudsmith" }
pulumi_wasm_runner_component_creator = { version = "=0.0.0-DEV", path = "pulumi_wasm_runner_component_creator", registry = "cloudsmith" }
pulumi_wasm_provider_common = { version = "=0.0.0-DEV", path = "pulumi_wasm_provider_common", registry = "cloudsmith" }
pulumi_wasm_typesystem = { version = "=1.0.0-0.0.0-DEV", path = "providers/pulumi_wasm_provider_typesystem_rust", registry = "cloudsmith" }

anyhow = "1.0.82"
prost = "0.13.1"
Expand Down
15 changes: 15 additions & 0 deletions examples/typesystem/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "pulumi_wasm_example_typesystem"
version.workspace = true
edition.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
crate-type = ["cdylib"]

[dependencies]
pulumi_wasm_provider_common.workspace = true
pulumi_wasm_rust.workspace = true
pulumi_wasm_typesystem.workspace = true
serde_json.workspace = true
102 changes: 102 additions & 0 deletions examples/typesystem/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#[cfg(test)]
mod tests {
use pulumi_wasm_provider_common::OneOf2;
use pulumi_wasm_rust::Output;
use pulumi_wasm_typesystem::typesystem_server::typesystemServerArgs;
use pulumi_wasm_typesystem::{EnumCase1, EnumCase2};
use std::panic::catch_unwind;

#[test]
fn test_compilation() {
let _ = catch_unwind(compilation_test);
}

#[test]
fn test_deserialization() {
let case1 = EnumCase1::builder()
.field_1("value1".to_string())
.build_struct();
let case2 = EnumCase2::builder()
.field_2("value2".to_string())
.build_struct();

let case1_json = serde_json::to_string(&case1).unwrap();
let case2_json = serde_json::to_string(&case2).unwrap();
assert_eq!(case1_json, r#"{"field1":"value1"}"#);
assert_eq!(case2_json, r#"{"field2":"value2"}"#);

let deserialized_case1: EnumCase1 = serde_json::from_str(&case1_json).unwrap();
assert_eq!(deserialized_case1, case1);

let deserialized_case2: EnumCase2 = serde_json::from_str(&case2_json).unwrap();
assert_eq!(deserialized_case2, case2);
}

fn compilation_test() {
// String
let output = Output::new(&"Hello, World!".to_string());

let _ = typesystemServerArgs::builder().required_string_input("&str");
let _ = typesystemServerArgs::builder().required_string_input("String".to_string());
let _ = typesystemServerArgs::builder().required_string_input(output);

let _ = typesystemServerArgs::builder().optional_string_input("&str");
let _ = typesystemServerArgs::builder().optional_string_input("String".to_string());
let _ = typesystemServerArgs::builder().optional_string_input(output);

// Vec<String>
let _ = typesystemServerArgs::builder().required_string_array(vec!["&str"]);
let _ = typesystemServerArgs::builder().required_string_array(vec!["String".to_string()]);
let _ = typesystemServerArgs::builder().required_string_array(output.map(|s| vec![s]));
// let _ = typesystemServerArgs::builder().required_string_array(vec![string_output]);

let _ = typesystemServerArgs::builder().optional_string_array(vec!["&str"]);
let _ = typesystemServerArgs::builder().optional_string_array(vec!["String".to_string()]);
let _ = typesystemServerArgs::builder().optional_string_array(output.map(|s| vec![s]));
// let _ = typesystemServerArgs::builder().optional_string_array(vec![string_output]);

// Vec<String> with array
let _ = typesystemServerArgs::builder().required_string_array(["&str"]);
let _ = typesystemServerArgs::builder().required_string_array(["String".to_string()]);
let _ = typesystemServerArgs::builder().required_string_array(output.map(|s| vec![s]));
// let _ = typesystemServerArgs::builder().required_string_array([string_output]);

let _ = typesystemServerArgs::builder().optional_string_array(["&str"]);
let _ = typesystemServerArgs::builder().optional_string_array(["String".to_string()]);
let _ = typesystemServerArgs::builder().optional_string_array(output.map(|s| vec![s]));
// let _ = typesystemServerArgs::builder().optional_string_array([string_output]);

// Union
let case1 = EnumCase1::builder()
.field_1("value1".to_string())
.build_struct();
let case2 = EnumCase2::builder()
.field_2("value2".to_string())
.build_struct();
let enum_case1_output = Output::new(&case1);
let enum_case2_output = Output::new(&case2);
let _ = typesystemServerArgs::builder().required_union(OneOf2::left(case1));
let _ = typesystemServerArgs::builder().required_union(OneOf2::right(case2));
let _ = typesystemServerArgs::builder().required_union(enum_case1_output.map(OneOf2::left));
let _ =
typesystemServerArgs::builder().required_union(enum_case2_output.map(OneOf2::right));

let case1 = EnumCase1::builder()
.field_1("value1".to_string())
.build_struct();
let case2 = EnumCase2::builder()
.field_2("value2".to_string())
.build_struct();
let _ = typesystemServerArgs::builder().optional_union(OneOf2::left(case1));
let _ = typesystemServerArgs::builder().optional_union(OneOf2::right(case2));
let _ = typesystemServerArgs::builder().optional_union(enum_case1_output.map(OneOf2::left));
let _ =
typesystemServerArgs::builder().optional_union(enum_case2_output.map(OneOf2::right));

// // Other types
// let _ = typesystemServerArgs::builder()
// .required_string_input(42);
// let _ = typesystemServerArgs::builder()
// .required_string_input(true);
}
}
9 changes: 7 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ CARGO_LLVM_COV_VERSION := "0.6.13"
FORMATTABLE_PROJECTS := "-p pulumi_wasm -p pulumi_wasm_common -p pulumi_wasm_generator -p pulumi_wasm_generator_lib \
-p pulumi_wasm_runner -p pulumi_wasm_runner_component_creator -p pulumi_wasm_rust -p pulumi_wasm_rust_macro \
-p pulumi_wasm_example_dependencies -p pulumi_wasm_example_docker -p pulumi_wasm_example_multiple_providers \
-p pulumi_wasm_example_simple"
-p pulumi_wasm_example_simple -p pulumi_wasm_example_typesystem -p regenerate_providers"

@default: build test

Expand Down Expand Up @@ -92,7 +92,7 @@ regenerate-provider-list:
cargo run -p regenerate_providers

# DO NOT EDIT - REGENERATE-PROVIDERS - START
regenerate-providers:
regenerate-providers-generated:
cargo run -p pulumi_wasm_generator -- gen-provider --remove true --schema providers/docker.json --output providers/pulumi_wasm_provider_docker
cargo run -p pulumi_wasm_generator -- gen-rust --remove true --schema providers/docker.json --output providers/pulumi_wasm_provider_docker_rust
cargo run -p pulumi_wasm_generator -- gen-provider --remove true --schema providers/random.json --output providers/pulumi_wasm_provider_random
Expand All @@ -101,6 +101,10 @@ regenerate-providers:
cargo run -p pulumi_wasm_generator -- gen-rust --remove true --schema providers/cloudflare.json --output providers/pulumi_wasm_provider_cloudflare_rust
# DO NOT EDIT - REGENERATE-PROVIDERS - END

regenerate-providers:
just regenerate-providers-generated
cargo run -p pulumi_wasm_generator -- gen-rust --remove true --schema providers/typesystem.json --output providers/pulumi_wasm_provider_typesystem_rust

publish:
cargo publish -p pulumi_wasm_wit --all-features
cargo publish -p pulumi_wasm_proto --all-features
Expand All @@ -112,6 +116,7 @@ publish:
cargo publish -p pulumi_wasm_core --all-features
cargo publish -p pulumi_wasm_runner_component_creator --all-features
cargo publish -p pulumi_wasm_runner --all-features
cargo publish -p pulumi_wasm_provider_common --all-features
just publish-providers

# DO NOT EDIT - PUBLISH-PROVIDERS - START
Expand Down
3 changes: 2 additions & 1 deletion providers/pulumi_wasm_provider_cloudflare_rust/Cargo.toml

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Loading
Loading