Skip to content

Commit

Permalink
oneOf
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejressel committed Dec 12, 2024
1 parent f2bb9a3 commit 418aa76
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 26 deletions.
22 changes: 15 additions & 7 deletions examples/typesystem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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(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<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(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()
Expand Down
4 changes: 2 additions & 2 deletions pulumi_wasm_generator_lib/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>()
.join(", ")
),
Expand All @@ -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()),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -120,7 +119,7 @@ fn create_discriminated_union(name: String, types: &Vec<Type>) -> String {

code.push_str("}\n");

return code;
code
}

pub(crate) fn generate_source_code(package: &crate::model::Package) -> HashMap<PathBuf, String> {
Expand Down
4 changes: 2 additions & 2 deletions pulumi_wasm_generator_lib/src/schema.rs
Original file line number Diff line number Diff line change
@@ -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<T> = BTreeMap<String, T>;

Expand Down Expand Up @@ -182,7 +182,7 @@ fn create_discriminated_union(one_of: &Vec<OneOfType>) -> Result<crate::model::T
one_of
.iter()
.map(|r| {
Ref::new(&*r.ref_)
Ref::new(&r.ref_)
.context(format!("Cannot convert ref fo type {r:?}"))
.unwrap()
})
Expand Down
4 changes: 2 additions & 2 deletions pulumi_wasm_provider_common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ macro_rules! generate_string_const {
}

impl<'de> serde::Deserialize<'de> for $struct_name {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
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 {
Expand Down
7 changes: 4 additions & 3 deletions pulumi_wasm_provider_common/src/oneof.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::fmt::Debug;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum OneOf2<A: Serialize, B: Serialize> {
pub enum OneOf2<A: Serialize + Debug, B: Serialize + Debug> {
Left(A),
Right(B),
}

impl<A: Serialize, B: Serialize> OneOf2<A, B> {
impl<A: Serialize + Debug, B: Serialize + Debug> OneOf2<A, B> {
pub fn left(a: A) -> Self {
OneOf2::Left(a)
}
Expand Down
13 changes: 5 additions & 8 deletions pulumi_wasm_rust/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -37,11 +38,9 @@ impl<T: Serialize> From<T> for Output<Option<T>> {
}
}

impl<T: Serialize + Clone + Debug + for<'de> serde::Deserialize<'de>> From<Output<T>>
for Output<Option<T>>
{
impl<T: Serialize + DeserializeOwned> From<Output<T>> for Output<Option<T>> {
fn from(output: Output<T>) -> Self {
output.map(|v| Some(v.clone()))
output.map(|v| Some(v))
}
}

Expand Down Expand Up @@ -115,14 +114,12 @@ impl<T> Output<T> {
pub fn map<B, F>(&self, f: F) -> Output<B>
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)
};
Expand Down

0 comments on commit 418aa76

Please sign in to comment.