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

feat: allow split/select to be used in conditions #150

Merged
merged 7 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 16 additions & 0 deletions src/ir/conditions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ pub enum ConditionIr {

// Cloudformation meta-functions
Map(String, Box<ConditionIr>, Box<ConditionIr>),
Split(String, Box<ConditionIr>),
Select(String, Box<ConditionIr>),

// End of recursion, the base primitives to work with
Str(String),
Expand Down Expand Up @@ -94,6 +96,14 @@ impl ConditionValue {

ConditionIr::Map(name, Box::new(x), Box::new(y))
}
Self::Split(delimiter, x) => {
let x = x.into_ir();
ConditionIr::Split(delimiter, Box::new(x))
}
Self::Select(index, x) => {
let x = x.into_ir();
ConditionIr::Select(index, Box::new(x))
}
Self::String(x) => ConditionIr::Str(x),
Self::Ref(name) => {
// The only 2 references allowed in conditions is parameters or pseudo parameters.
Expand Down Expand Up @@ -180,6 +190,12 @@ impl ConditionValue {
key1.find_dependencies(logical_id, topo_sort);
key2.find_dependencies(logical_id, topo_sort);
}
Self::Split(_, key1) => {
key1.find_dependencies(logical_id, topo_sort);
}
Self::Select(_, key1) => {
key1.find_dependencies(logical_id, topo_sort);
}
Self::Function(func) => func.find_dependencies(logical_id, topo_sort),
Self::Ref(_) | Self::String(_) => {}
}
Expand Down
19 changes: 18 additions & 1 deletion src/parser/condition/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ pub enum ConditionValue {

// Cloudformation meta-functions
FindInMap(String, Box<ConditionValue>, Box<ConditionValue>),

Split(String, Box<ConditionValue>),
Select(String, Box<ConditionValue>),
// End of recursion, the base primitives to work with
String(String),
Ref(String),
Expand Down Expand Up @@ -160,6 +161,14 @@ impl<'de> serde::Deserialize<'de> for ConditionValue {
second_level_key,
))
}
"Split" => {
let (delimiter, source_string) = data.newtype_variant()?;
Ok(Self::Value::Split(delimiter, source_string))
}
"Select" => {
let (index, source_array) = data.newtype_variant()?;
Ok(Self::Value::Select(index, source_array))
}
"Ref" => Ok(Self::Value::Ref(data.newtype_variant()?)),
other => Ok(ConditionFunction::from_variant_access(other, data)?.into()),
}
Expand Down Expand Up @@ -195,6 +204,14 @@ impl<'de> serde::Deserialize<'de> for ConditionValue {
second_level_key,
))
}
"!Split" | "Fn::Split" => {
let (delimiter, split_str) = data.next_value()?;
Ok(Self::Value::Split(delimiter, split_str))
}
"!Select" | "Fn::Select" => {
let (index, array) = data.next_value()?;
Ok(Self::Value::Select(index, array))
}
"!Ref" | "Ref" => Ok(Self::Value::Ref(data.next_value()?)),
other => Ok(ConditionFunction::from_map_access(other, &mut data)?.into()),
}
Expand Down
16 changes: 16 additions & 0 deletions src/parser/condition/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,22 @@ fn condition_find_in_map() {
);
}

#[test]
fn condition_split() {
let expected = ConditionValue::Split(
",".into(),
Box::new(ConditionValue::String("hello,world".into())),
);
assert_eq!(
expected,
serde_yaml::from_str("!Split [\",\", \"hello,world\"]").unwrap()
);
assert_eq!(
expected,
serde_yaml::from_str("Fn::Split: [\",\", \"hello,world\"]").unwrap()
);
}

#[test]
fn condition_str_bool() {
let expected = ConditionValue::String("true".into());
Expand Down
12 changes: 12 additions & 0 deletions src/synthesizer/golang/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ impl Inspectable for ConditionIr {
list.iter().any(|cond| cond.uses_map_table(name))
}
ConditionIr::Map(map_name, _, _) => map_name == name,
ConditionIr::Split(_, cond) => cond.uses_map_table(name),
ConditionIr::Select(_, cond) => cond.uses_map_table(name),
ConditionIr::Str(_) | ConditionIr::Ref(_) => false,
}
}
Expand Down Expand Up @@ -494,6 +496,16 @@ impl GolangEmitter for ConditionIr {
slk.emit_golang(context, output, None);
output.text("]");
}
ConditionIr::Split(sep, str) => {
output.text(format!("cdk.Fn_Split(jsii.String({sep:?}), "));
str.emit_golang(context, output, None);
output.text(")");
}
ConditionIr::Select(index, str) => {
output.text(format!("cdk.Fn_Select(jsii.Number({index:?}), "));
str.emit_golang(context, output, None);
output.text(")");
}
}
if let Some(trailer) = trailer {
output.text(trailer.to_owned())
Expand Down
12 changes: 12 additions & 0 deletions src/synthesizer/typescript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,18 @@ fn synthesize_condition_recursive(val: &ConditionIr) -> String {
synthesize_condition_recursive(l2.as_ref())
)
}
ConditionIr::Split(sep, l1) => {
let str = synthesize_condition_recursive(l1.as_ref());
format!(
"{str}.split('{sep}')",
str = str.escape_debug(),
sep = sep.escape_debug()
)
}
ConditionIr::Select(index, l1) => {
let str = synthesize_condition_recursive(l1.as_ref());
format!("cdk.Fn.select({index}, {str})")
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions tests/end-to-end/simple/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ func NewNoctStack(scope constructs.Construct, id string, props NoctStackProps) *

stack := cdk.NewStack(scope, &id, &props.StackProps)

isUs := cdk.Fn_Select(jsii.Number("0"), cdk.Fn_Split(jsii.String("-"), stack.Region())) == jsii.String("us")

isUsEast1 := stack.Region() == jsii.String("us-east-1")

queue := sqs.NewCfnQueue(
Expand Down
1 change: 1 addition & 0 deletions tests/end-to-end/simple/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export class NoctStack extends cdk.Stack {
};

// Conditions
const isUs = cdk.Fn.select(0, this.region.split('-')) === 'us';
const isUsEast1 = this.region === 'us-east-1';

// Resources
Expand Down
9 changes: 8 additions & 1 deletion tests/end-to-end/simple/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ Conditions:
Fn::Equals:
- !Ref AWS::Region
- us-east-1

IsUs:
Fn::Equals:
- Fn::Select:
- 0
- Fn::Split:
- "-"
- "!Ref": AWS::Region
- us
Parameters:
BucketNamePrefix:
Type: String
Expand Down