From 418aa768728e5d01e69e8bfbbc8571191025e67f Mon Sep 17 00:00:00 2001 From: Andrzej Ressel Date: Thu, 12 Dec 2024 22:56:10 +0100 Subject: [PATCH] oneOf --- examples/typesystem/src/lib.rs | 22 +++++++++++++------ pulumi_wasm_generator_lib/src/model.rs | 4 ++-- .../output/rust/source_code_resource_code.rs | 3 +-- pulumi_wasm_generator_lib/src/schema.rs | 4 ++-- pulumi_wasm_provider_common/src/lib.rs | 4 ++-- pulumi_wasm_provider_common/src/oneof.rs | 7 +++--- pulumi_wasm_rust/src/output.rs | 13 +++++------ 7 files changed, 31 insertions(+), 26 deletions(-) diff --git a/examples/typesystem/src/lib.rs b/examples/typesystem/src/lib.rs index 5a4d1d92..d3960473 100644 --- a/examples/typesystem/src/lib.rs +++ b/examples/typesystem/src/lib.rs @@ -12,44 +12,52 @@ mod tests { } fn compilation_test() { - let string_output = Output::new(&"Hello, World!".to_string()); // 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(string_output); + 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(string_output); + let _ = typesystemServerArgs::builder().optional_string_input(output); // Vec let _ = typesystemServerArgs::builder().required_string_array(vec!["&str"]); let _ = typesystemServerArgs::builder().required_string_array(vec!["String".to_string()]); - let _ = typesystemServerArgs::builder().required_string_array(string_output.map(|s| vec![s])); + 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(string_output.map(|s| vec![s])); + let _ = typesystemServerArgs::builder().optional_string_array(output.map(|s| vec![s])); // let _ = typesystemServerArgs::builder().optional_string_array(vec![string_output]); // Vec with array let _ = typesystemServerArgs::builder().required_string_array(["&str"]); let _ = typesystemServerArgs::builder().required_string_array(["String".to_string()]); - let _ = typesystemServerArgs::builder().required_string_array(string_output.map(|s| vec![s])); + 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(string_output.map(|s| vec![s])); + let _ = typesystemServerArgs::builder().optional_string_array(output.map(|s| vec![s])); // let _ = typesystemServerArgs::builder().optional_string_array([string_output]); // Union + let enum_case1_output = Output::new(&EnumCase1 {}); + let enum_case2_output = Output::new(&EnumCase2 {}); let _ = typesystemServerArgs::builder().required_union(OneOf2::left(EnumCase1 {})); let _ = typesystemServerArgs::builder().required_union(OneOf2::right(EnumCase2 {})); + let _ = typesystemServerArgs::builder().required_union(enum_case1_output.map(|c| OneOf2::left(c))); + let _ = typesystemServerArgs::builder().required_union(enum_case2_output.map(|c| OneOf2::right(c))); + let _ = typesystemServerArgs::builder().optional_union(OneOf2::left(EnumCase1 {})); let _ = typesystemServerArgs::builder().optional_union(OneOf2::right(EnumCase2 {})); + let _ = typesystemServerArgs::builder().optional_union(enum_case1_output.map(|c| OneOf2::left(c))); + let _ = typesystemServerArgs::builder().optional_union(enum_case2_output.map(|c| OneOf2::right(c))); // // Other types // let _ = typesystemServerArgs::builder() diff --git a/pulumi_wasm_generator_lib/src/model.rs b/pulumi_wasm_generator_lib/src/model.rs index c8902803..dac81da9 100644 --- a/pulumi_wasm_generator_lib/src/model.rs +++ b/pulumi_wasm_generator_lib/src/model.rs @@ -44,7 +44,7 @@ impl Type { "pulumi_wasm_provider_common::OneOf{}<{}>", refs.len(), refs.iter() - .map(|(r)| Type::Ref(r.clone()).get_rust_type()) + .map(|r| Type::Ref(r.clone()).get_rust_type()) .collect::>() .join(", ") ), @@ -61,7 +61,7 @@ impl Type { Type::Object(o) => o.get_internal_discriminated_union(), Type::Ref(_) => None, Type::Option(o) => o.get_internal_discriminated_union(), - Type::DiscriminatedUnion(m) => Some(m.iter().map(|(r)| Type::Ref(r.clone())).collect()), + Type::DiscriminatedUnion(m) => Some(m.iter().map(|r| Type::Ref(r.clone())).collect()), } } } diff --git a/pulumi_wasm_generator_lib/src/output/rust/source_code_resource_code.rs b/pulumi_wasm_generator_lib/src/output/rust/source_code_resource_code.rs index 0c5c7034..ecaeab49 100644 --- a/pulumi_wasm_generator_lib/src/output/rust/source_code_resource_code.rs +++ b/pulumi_wasm_generator_lib/src/output/rust/source_code_resource_code.rs @@ -1,5 +1,4 @@ use crate::model::{Resource, Type}; -use crate::utils::to_lines; use convert_case::{Case, Casing}; use handlebars::Handlebars; use serde::Serialize; @@ -120,7 +119,7 @@ fn create_discriminated_union(name: String, types: &Vec) -> String { code.push_str("}\n"); - return code; + code } pub(crate) fn generate_source_code(package: &crate::model::Package) -> HashMap { diff --git a/pulumi_wasm_generator_lib/src/schema.rs b/pulumi_wasm_generator_lib/src/schema.rs index 66ca369b..fde1de48 100644 --- a/pulumi_wasm_generator_lib/src/schema.rs +++ b/pulumi_wasm_generator_lib/src/schema.rs @@ -1,7 +1,7 @@ use crate::model::{ElementId, GlobalType, GlobalTypeProperty, InputProperty, OutputProperty, Ref}; use anyhow::{anyhow, Context, Result}; use serde::Deserialize; -use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; +use std::collections::{BTreeMap, BTreeSet, HashSet}; type PulumiMap = BTreeMap; @@ -182,7 +182,7 @@ fn create_discriminated_union(one_of: &Vec) -> Result serde::Deserialize<'de> for $struct_name { - fn deserialize>(deserializer: D) -> Result { + fn deserialize>(deserializer: D) -> Result { struct ConstantVisitor; - impl<'de> Visitor<'de> for ConstantVisitor { + impl<'de> serde::de::Visitor<'de> for ConstantVisitor { type Value = $struct_name; fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { diff --git a/pulumi_wasm_provider_common/src/oneof.rs b/pulumi_wasm_provider_common/src/oneof.rs index 1c2388d4..103a40ca 100644 --- a/pulumi_wasm_provider_common/src/oneof.rs +++ b/pulumi_wasm_provider_common/src/oneof.rs @@ -1,13 +1,14 @@ +use std::fmt::Debug; use serde::{Deserialize, Serialize}; -#[derive(Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize)] #[serde(untagged)] -pub enum OneOf2 { +pub enum OneOf2 { Left(A), Right(B), } -impl OneOf2 { +impl OneOf2 { pub fn left(a: A) -> Self { OneOf2::Left(a) } diff --git a/pulumi_wasm_rust/src/output.rs b/pulumi_wasm_rust/src/output.rs index 4bf0ab81..ee2f1014 100644 --- a/pulumi_wasm_rust/src/output.rs +++ b/pulumi_wasm_rust/src/output.rs @@ -3,6 +3,7 @@ use log::info; use once_cell::sync::Lazy; use pulumi_wasm_wit::client_bindings::component::pulumi_wasm::output_interface; use pulumi_wasm_wit::client_bindings::component::pulumi_wasm::stack_interface::add_export; +use serde::de::DeserializeOwned; use serde::Serialize; use std::collections::HashMap; use std::fmt::Debug; @@ -37,11 +38,9 @@ impl From for Output> { } } -impl serde::Deserialize<'de>> From> - for Output> -{ +impl From> for Output> { fn from(output: Output) -> Self { - output.map(|v| Some(v.clone())) + output.map(|v| Some(v)) } } @@ -115,14 +114,12 @@ impl Output { pub fn map(&self, f: F) -> Output where F: Fn(T) -> B + Send + 'static, - T: serde::de::DeserializeOwned + Debug, - B: Serialize + Debug, + T: DeserializeOwned, + B: Serialize, { let f = move |arg: &String| { let argument = serde_json::from_str(arg)?; - info!("Argument: {:?}", argument); let result = f(argument); - info!("Result: {:?}", result); let result = serde_json::to_string(&result)?; Ok(result) };