Skip to content

Commit

Permalink
Add default parameters for constructors, fix doubles (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
adwhalen authored Feb 23, 2023
1 parent 0da80f1 commit cd415fc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/ir/constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ impl Constructor {
pub fn translate(parse_tree: &CloudformationParseTree) -> Constructor {
let mut inputs = Vec::new();
for (name, param) in parse_tree.parameters.params.iter() {
let default: Option<&String> = param.default.as_ref();
inputs.push(ConstructorParameter {
name: camel_case(name),
constructor_type: param.parameter_type.to_string(),
default_value: default.map(|v| v.to_string()),
})
}
Constructor { inputs }
Expand All @@ -30,4 +32,5 @@ impl Default for Constructor {
pub struct ConstructorParameter {
pub name: String,
pub constructor_type: String,
pub default_value: Option<String>,
}
1 change: 1 addition & 0 deletions src/ir/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ pub fn translate_resource(
return match simple_type {
CfnType::Boolean => Ok(ResourceIr::Bool(s.parse().unwrap())),
CfnType::Integer => Ok(ResourceIr::Number(s.parse().unwrap())),
CfnType::Double => Ok(ResourceIr::Number(s.parse().unwrap())),
&_ => Ok(ResourceIr::String(s.to_string())),
};
}
Expand Down
25 changes: 23 additions & 2 deletions src/synthesizer/typescript_synthesizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,39 @@ impl TypescriptSynthesizer {
"export interface NoctStackProps extends cdk.StackProps {",
);

for param in ir.constructor.inputs {
for param in &ir.constructor.inputs {
append_with_newline(
output,
&format!(
"\treadonly {}: {};",
pretty_name(&param.name),
pretty_name(&param.constructor_type)
pretty_name(&param.constructor_type),
),
);
}

append_with_newline(output, "}");

append_with_newline(output, "\n// Default parameters");
append_with_newline(output, "// {");
for param in &ir.constructor.inputs {
let default_value: Option<&String> = param.default_value.as_ref();
if let Some(x) = default_value {
append_with_newline(
output,
&format!(
"//\t {}: {},",
pretty_name(&param.name),
match pretty_name(&param.constructor_type).as_str() {
"string" => x.to_string(),
_ => pretty_name(x),
}
),
);
}
}
append_with_newline(output, "// }");

append_with_newline(output, "\n// Stack");
append_with_newline(output, "export class NoctStack extends cdk.Stack {");
append_with_newline(
Expand Down

0 comments on commit cd415fc

Please sign in to comment.