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

String const #571

Merged
merged 5 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

17 changes: 16 additions & 1 deletion 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::{MyEnum, UnionCase1, UnionCase2};
use pulumi_wasm_typesystem::{MyEnum, UnionCase1, UnionCase2, UnionCaseWithConst1, UnionCaseWithConst2};
use std::panic::catch_unwind;

#[test]
Expand Down Expand Up @@ -47,6 +47,21 @@ mod tests {
assert_eq!(deserialized_enum2, enum2);
}

#[test]
fn test_case_deserialization_with_oneof2() {
let oneof: OneOf2<UnionCaseWithConst1, UnionCaseWithConst2> = OneOf2::Left(
UnionCaseWithConst1::builder()
.field_1("value1".to_string())
.build_struct(),
);

let json = serde_json::to_string(&oneof).unwrap();
assert_eq!(json, r#"{"field1":"value1"}"#);

let deserialized: OneOf2<UnionCaseWithConst1, UnionCaseWithConst2> = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized, oneof);
}

fn compilation_test() {
// String
let output = Output::new(&"Hello, World!".to_string());
Expand Down
2 changes: 2 additions & 0 deletions providers/pulumi_wasm_provider_typesystem_rust/src/lib.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.

30 changes: 30 additions & 0 deletions providers/typesystem.json

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

1 change: 1 addition & 0 deletions pulumi_wasm_generator_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ prettyplease.workspace = true
syn.workspace = true
quote.workspace = true
proc-macro2.workspace = true
itertools.workspace = true

[dev-dependencies]
assert_cmd.workspace = true
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@
Type::Integer => TypeWithoutOption::Integer,
Type::Number => TypeWithoutOption::Number,
Type::String => TypeWithoutOption::String,
Type::ConstString(s) => TypeWithoutOption::String,
Fixed Show fixed Hide fixed
Type::Array(a) => TypeWithoutOption::Array(Box::new(remove_option(a))),
Type::Object(o) => TypeWithoutOption::Object(Box::new(remove_option(o))),
Type::Ref(r) => TypeWithoutOption::Ref(r.clone()),
Expand Down
20 changes: 19 additions & 1 deletion pulumi_wasm_generator_lib/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub(crate) enum Type {
Ref(Ref),
Option(Box<Type>),
DiscriminatedUnion(Vec<Type>),
ConstString(String)
}

impl Type {
Expand Down Expand Up @@ -48,6 +49,7 @@ impl Type {
.collect::<Vec<_>>()
.join(", ")
),
Type::ConstString(s) => format!("crate::__ConstString_{}", s).to_string(),
}
}

Expand All @@ -57,9 +59,10 @@ impl Type {
Type::Integer => None,
Type::Number => None,
Type::String => None,
Type::ConstString(_) => None,
Type::Ref(_) => None,
Type::Array(t) => t.get_internal_discriminated_union(),
Type::Object(o) => o.get_internal_discriminated_union(),
Type::Ref(_) => None,
Type::Option(o) => o.get_internal_discriminated_union(),
Type::DiscriminatedUnion(types) => Some(
types
Expand All @@ -69,6 +72,21 @@ impl Type {
),
}
}

pub(crate) fn get_consts(&self) -> Vec<String> {
match self {
Type::Boolean => vec![],
Type::Integer => vec![],
Type::Number => vec![],
Type::String => vec![],
Type::ConstString(s) => vec![s.clone()],
Type::Ref(_) => vec![],
Type::Array(t) => t.get_consts(),
Type::Object(o) => o.get_consts(),
Type::Option(o) => o.get_consts(),
Type::DiscriminatedUnion(_) => vec![],
}
}
}

#[derive(Debug, PartialEq, Hash, Ord, PartialOrd, Eq)]
Expand Down
5 changes: 4 additions & 1 deletion pulumi_wasm_generator_lib/src/output/rust/lib.rs.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ mod bindings {
fn into_domain<F: serde::Serialize>(output: WitOutput) -> Output<F> {
unsafe { Output::<F>::new_from_handle(output) }
}
{{/if}}
{{/if}}
{{#each package.const_strings}}
pulumi_wasm_provider_common::generate_string_const!(__ConstString_{{&this}}, "{{&this}}");
{{/each}}
33 changes: 33 additions & 0 deletions pulumi_wasm_generator_lib/src/output/rust/source_code_librs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::collections::BTreeSet;
use crate::output::get_main_version;
use handlebars::Handlebars;
use serde::Serialize;
use serde_json::json;
use crate::model::GlobalType;
use itertools::Itertools;

static TEMPLATE: &str = include_str!("lib.rs.handlebars");

Expand All @@ -16,6 +19,7 @@
pulumi_wasm_version: String,
contains_resources: bool,
contains_functions: bool,
const_strings: Vec<String>,
}

fn convert_model(package: &crate::model::Package) -> Package {
Expand All @@ -26,9 +30,38 @@
contains_elements: !package.resources.is_empty() || !package.functions.is_empty(),
contains_resources: !package.resources.is_empty(),
contains_functions: !package.functions.is_empty(),
const_strings: find_consts(package),
}
}

fn find_consts(package: &crate::model::Package) -> Vec<String> {
let mut consts = BTreeSet::new();
for (_, resource) in &package.resources {
Fixed Show fixed Hide fixed
for (input) in &resource.input_properties {
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
consts.extend(input.r#type.get_consts().clone());
}
for (output) in &resource.output_properties {
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
consts.extend(output.r#type.get_consts().clone());
}
}
for (_, function) in &package.functions {
Fixed Show fixed Hide fixed
for (input) in &function.input_properties {
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
consts.extend(input.r#type.get_consts().clone());
}
for (output) in &function.output_properties {
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
consts.extend(output.r#type.get_consts().clone());
}
}
for (_, type_) in &package.types {
Fixed Show fixed Hide fixed
if let GlobalType::Object(_, obj) = type_ {
for gtp in obj {
consts.extend(gtp.r#type.get_consts().clone());
}
}
}
consts.into_iter().sorted().collect()
}

pub(crate) fn generate_source_code(package: &crate::model::Package) -> String {
let handlebars = Handlebars::new();
handlebars
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use convert_case::{Case, Casing};
use handlebars::Handlebars;
use serde::Serialize;
use serde_json::json;
use std::collections::HashMap;
use std::collections::{BTreeSet, HashMap};
use std::path::PathBuf;

static TEMPLATE: &str = include_str!("types_code.rs.handlebars");
Expand All @@ -25,6 +25,7 @@ struct RefType {
description_lines: Vec<String>,
struct_name: String,
file_name: String,
const_strings: BTreeSet<String>,
}

#[derive(Serialize)]
Expand Down Expand Up @@ -86,11 +87,15 @@ fn convert_model(package: &crate::model::Package) -> Package {
),
})
.collect(),
const_strings: properties
.iter()
.flat_map(|global_type_property| global_type_property.r#type.get_consts())
.collect()
};
real_types.push(ref_type);
}
GlobalType::StringEnum(description, enum_values) => {
let enum_type = Enum {
let enum_type = Enum {
struct_name: element_id.get_rust_struct_name(),
file_name: element_id.get_rust_struct_name().to_case(Case::Snake),
description_lines: crate::utils::to_lines(description.clone(), package, None),
Expand Down
8 changes: 8 additions & 0 deletions pulumi_wasm_generator_lib/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ struct Type {
additional_properties: Option<Box<Type>>,
#[serde(rename = "oneOf")]
one_of: Option<Vec<OneOfType>>,
#[serde(rename = "const")]
const_: Option<String>
}

#[derive(Deserialize, Debug)]
Expand Down Expand Up @@ -136,6 +138,7 @@ pub(crate) struct Package {

//TODO: Fix formatting
fn new_type_mapper(type_: &Type) -> Result<crate::model::Type> {
println!("{:?}", type_);
(match type_ {
Type {
ref_: Some(ref r), ..
Expand All @@ -154,6 +157,11 @@ fn new_type_mapper(type_: &Type) -> Result<crate::model::Type> {
type_: Some(TypeEnum::Number),
..
} => Ok(crate::model::Type::Number),
Type {
type_: Some(TypeEnum::String),
const_: Some(const_),
..
} => Ok(crate::model::Type::ConstString(const_.clone())),
Type {
type_: Some(TypeEnum::String),
..
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#[builder(finish_fn = build_struct)]
pub struct FuncWithConstInputArgs {
#[builder(into, default = ::pulumi_wasm_rust::Output::empty())]
pub plain_input: pulumi_wasm_rust::Output<Option<String>>,
pub plain_input: pulumi_wasm_rust::Output<Option<crate::__ConstString_fixed>>,
}

pub struct FuncWithConstInputResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ mod bindings {
fn into_domain<F: serde::Serialize>(output: WitOutput) -> Output<F> {
unsafe { Output::<F>::new_from_handle(output) }
}
pulumi_wasm_provider_common::generate_string_const!(__ConstString_Environment, "Environment");
pulumi_wasm_provider_common::generate_string_const!(__ConstString_Folder, "Folder");
pulumi_wasm_provider_common::generate_string_const!(__ConstString_Package, "Package");
pulumi_wasm_provider_common::generate_string_const!(__ConstString_Project, "Project");
pulumi_wasm_provider_common::generate_string_const!(__ConstString_fixed, "fixed");
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct SsisEnvironmentResponse {
/// Expected value is 'Environment'.
#[builder(into)]
#[serde(rename = "type")]
pub r#type: Box<String>,
pub r#type: Box<crate::__ConstString_Environment>,
/// Variable in environment
#[builder(into, default = Box::new(None))]
#[serde(rename = "variables")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ pub struct SsisFolderResponse {
/// Expected value is 'Folder'.
#[builder(into)]
#[serde(rename = "type")]
pub r#type: Box<String>,
pub r#type: Box<crate::__ConstString_Folder>,
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ pub struct SsisPackageResponse {
/// Expected value is 'Package'.
#[builder(into)]
#[serde(rename = "type")]
pub r#type: Box<String>,
pub r#type: Box<crate::__ConstString_Package>,
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct SsisProjectResponse {
/// Expected value is 'Project'.
#[builder(into)]
#[serde(rename = "type")]
pub r#type: Box<String>,
pub r#type: Box<crate::__ConstString_Project>,
/// Project version.
#[builder(into, default = Box::new(None))]
#[serde(rename = "version")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ mod bindings {
fn into_domain<F: serde::Serialize>(output: WitOutput) -> Output<F> {
unsafe { Output::<F>::new_from_handle(output) }
}
pulumi_wasm_provider_common::generate_string_const!(__ConstString_PointInTimeRestore, "PointInTimeRestore");
pulumi_wasm_provider_common::generate_string_const!(__ConstString_Replica, "Replica");
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
pub struct ServerPropertiesForReplica {
#[builder(into)]
#[serde(rename = "createMode")]
pub r#create_mode: Box<String>,
pub r#create_mode: Box<crate::__ConstString_Replica>,
#[builder(into, default = Box::new(None))]
#[serde(rename = "version")]
pub r#version: Box<Option<String>>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
pub struct ServerPropertiesForRestore {
#[builder(into)]
#[serde(rename = "createMode")]
pub r#create_mode: Box<String>,
pub r#create_mode: Box<crate::__ConstString_PointInTimeRestore>,
#[builder(into)]
#[serde(rename = "restorePointInTime")]
pub r#restore_point_in_time: Box<String>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ mod bindings {
fn into_domain<F: serde::Serialize>(output: WitOutput) -> Output<F> {
unsafe { Output::<F>::new_from_handle(output) }
}
pulumi_wasm_provider_common::generate_string_const!(__ConstString_PointInTimeRestore, "PointInTimeRestore");
pulumi_wasm_provider_common::generate_string_const!(__ConstString_Replica, "Replica");
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
pub struct ServerPropertiesForReplica {
#[builder(into)]
#[serde(rename = "createMode")]
pub r#create_mode: Box<String>,
pub r#create_mode: Box<crate::__ConstString_Replica>,
#[builder(into, default = Box::new(None))]
#[serde(rename = "version")]
pub r#version: Box<Option<String>>,
Expand Down
Loading
Loading