Skip to content

Commit

Permalink
Enum
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejressel committed Dec 15, 2024
1 parent 90c1d19 commit 1c01047
Show file tree
Hide file tree
Showing 23 changed files with 357 additions and 102 deletions.
37 changes: 26 additions & 11 deletions examples/typesystem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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 pulumi_wasm_typesystem::{MyEnum, UnionCase1, UnionCase2};
use std::panic::catch_unwind;

#[test]
Expand All @@ -12,11 +12,11 @@ mod tests {
}

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

Expand All @@ -25,13 +25,28 @@ mod tests {
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();
let deserialized_case1: UnionCase1 = serde_json::from_str(&case1_json).unwrap();
let deserialized_case2: UnionCase2 = serde_json::from_str(&case2_json).unwrap();
assert_eq!(deserialized_case1, case1);

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

#[test]
fn test_enum_deserialization() {
let enum1 = MyEnum::Value1;
let enum2 = MyEnum::Value2;

let enum1_json = serde_json::to_string(&enum1).unwrap();
let enum2_json = serde_json::to_string(&enum2).unwrap();
assert_eq!(enum1_json, r#""VALUE1""#);
assert_eq!(enum2_json, r#""Value2""#);

let deserialized_enum1: MyEnum = serde_json::from_str(&enum1_json).unwrap();
let deserialized_enum2: MyEnum = serde_json::from_str(&enum2_json).unwrap();
assert_eq!(deserialized_enum1, enum1);
assert_eq!(deserialized_enum2, enum2);
}

fn compilation_test() {
// String
let output = Output::new(&"Hello, World!".to_string());
Expand Down Expand Up @@ -67,10 +82,10 @@ mod tests {
// let _ = TypesystemServerArgs::builder().optional_string_array([string_output]);

// Union
let case1 = EnumCase1::builder()
let case1 = UnionCase1::builder()
.field_1("value1".to_string())
.build_struct();
let case2 = EnumCase2::builder()
let case2 = UnionCase2::builder()
.field_2("value2".to_string())
.build_struct();
let enum_case1_output = Output::new(&case1);
Expand All @@ -81,10 +96,10 @@ mod tests {
let _ =
TypesystemServerArgs::builder().required_union(enum_case2_output.map(OneOf2::right));

let case1 = EnumCase1::builder()
let case1 = UnionCase1::builder()
.field_1("value1".to_string())
.build_struct();
let case2 = EnumCase2::builder()
let case2 = UnionCase2::builder()
.field_2("value2".to_string())
.build_struct();
let _ = TypesystemServerArgs::builder().optional_union(OneOf2::left(case1));
Expand Down

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.

3 changes: 2 additions & 1 deletion providers/pulumi_wasm_provider_docker_rust/src/types/mod.rs

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.

10 changes: 6 additions & 4 deletions providers/pulumi_wasm_provider_typesystem_rust/src/types/mod.rs

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.

28 changes: 20 additions & 8 deletions providers/typesystem.json

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

3 changes: 2 additions & 1 deletion pulumi_wasm_generator_lib/src/code_generation/yaml/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ fn map_type(
let tpe = &context.package.types[element_id];

let gtp = match tpe {
GlobalType::Object(gtp) => gtp,
GlobalType::Object(_, gtp) => gtp,
GlobalType::StringEnum(_, _) => panic!("StringEnum type is not supported"),
GlobalType::String => panic!("String type is not supported"),
GlobalType::Boolean => panic!("Boolean type is not supported"),
GlobalType::Number => panic!("Number type is not supported"),
Expand Down
10 changes: 9 additions & 1 deletion pulumi_wasm_generator_lib/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,21 @@ pub(crate) struct GlobalTypeProperty {

#[derive(Debug, PartialEq, Hash, Ord, PartialOrd, Eq)]
pub(crate) enum GlobalType {
Object(Vec<GlobalTypeProperty>),
Object(Option<String>, Vec<GlobalTypeProperty>),
StringEnum(Option<String>, Vec<StringEnumElement>),
String,
Boolean,
Number,
Integer,
}

#[derive(Debug, PartialEq, Hash, Ord, PartialOrd, Eq)]
pub(crate) struct StringEnumElement {
pub(crate) name: String,
pub(crate) value: Option<String>,
pub(crate) description: Option<String>,
}

#[derive(Debug, PartialEq, Hash, Ord, PartialOrd, Eq)]
pub(crate) struct Resource {
pub(crate) element_id: ElementId,
Expand Down
Loading

0 comments on commit 1c01047

Please sign in to comment.