Skip to content

Commit

Permalink
Add support for YAML shorthand (#97)
Browse files Browse the repository at this point in the history
Addresses #96 for not supporting the YAML shorthand forms.
  • Loading branch information
iph authored Apr 5, 2023
1 parent 6bf5a21 commit ebd6919
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
14 changes: 7 additions & 7 deletions src/parser/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ fn build_condition_recursively(name: &str, obj: &Value) -> Result<ConditionValue
#[allow(clippy::never_loop)]
for (condition_name, condition_object) in val {
let cond: ConditionValue = match condition_name.as_str() {
"Fn::And" => {
"!And" | "Fn::And" => {
let mut v: Vec<ConditionValue> = Vec::new();
let arr = match condition_object.as_array() {
None => {
Expand All @@ -107,7 +107,7 @@ fn build_condition_recursively(name: &str, obj: &Value) -> Result<ConditionValue

ConditionValue::And(v)
}
"Fn::Equals" => {
"!Equals" | "Fn::Equals" => {
let arr = match condition_object.as_array() {
None => {
return Err(TransmuteError {
Expand Down Expand Up @@ -135,7 +135,7 @@ fn build_condition_recursively(name: &str, obj: &Value) -> Result<ConditionValue
}?;
ConditionValue::Equals(Box::new(obj1), Box::new(obj2))
}
"Fn::Not" => {
"!Not" | "Fn::Not" => {
let arr = match condition_object.as_array() {
None => {
return Err(TransmuteError {
Expand All @@ -155,7 +155,7 @@ fn build_condition_recursively(name: &str, obj: &Value) -> Result<ConditionValue
}?;
ConditionValue::Not(Box::new(obj1))
}
"Fn::Or" => {
"!Or" | "Fn::Or" => {
let arr = match condition_object.as_array() {
None => {
return Err(TransmuteError {
Expand All @@ -173,7 +173,7 @@ fn build_condition_recursively(name: &str, obj: &Value) -> Result<ConditionValue

ConditionValue::Or(v)
}
"Condition" => {
"!Condition" | "Condition" => {
let condition_name = match condition_object.as_str() {
None => {
return Err(TransmuteError {
Expand All @@ -184,7 +184,7 @@ fn build_condition_recursively(name: &str, obj: &Value) -> Result<ConditionValue
};
ConditionValue::Condition(condition_name.to_string())
}
"Ref" => {
"!Ref" | "Ref" => {
let ref_name = match condition_object.as_str() {
None => {
return Err(TransmuteError {
Expand All @@ -195,7 +195,7 @@ fn build_condition_recursively(name: &str, obj: &Value) -> Result<ConditionValue
};
ConditionValue::Ref(ref_name.to_string())
}
"Fn::FindInMap" => {
"!FindInMap" | "Fn::FindInMap" => {
let arr = match condition_object.as_array() {
None => {
return Err(TransmuteError {
Expand Down
22 changes: 11 additions & 11 deletions src/parser/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ pub fn build_resources_recursively(
#[allow(clippy::never_loop)]
for (resource_name, resource_object) in val {
let cond: ResourceValue = match resource_name.as_str() {
"Fn::Sub" => {
"!Sub" | "Fn::Sub" => {
let mut v = Vec::new();
match resource_object {
Value::String(str) => {
Expand All @@ -205,7 +205,7 @@ pub fn build_resources_recursively(
}
ResourceValue::Sub(v)
}
"Fn::FindInMap" => {
"!FindInMap" | "Fn::FindInMap" => {
let v = match resource_object.as_array() {
None => {
return Err(TransmuteError {
Expand Down Expand Up @@ -253,7 +253,7 @@ pub fn build_resources_recursively(
Box::new(third_obj),
)
}
"Fn::GetAtt" => {
"!GetAtt" | "Fn::GetAtt" => {
match resource_object {
// Short form: "Fn::GetAttr": "blah.blah"
Value::String(x) => {
Expand Down Expand Up @@ -299,7 +299,7 @@ pub fn build_resources_recursively(
}
}
}
"Fn::GetAZs" => {
"!GetAZs" | "Fn::GetAZs" => {
let v = match resource_object {
Value::String(_) => {
build_resources_recursively(name, resource_object)
Expand All @@ -320,15 +320,15 @@ pub fn build_resources_recursively(
ResourceValue::GetAZs(Box::new(v))
}

"Fn::Base64" => {
"!Base64" | "Fn::Base64" => {
let resolved_obj = build_resources_recursively(name, resource_object)?;
ResourceValue::Base64(Box::new(resolved_obj))
}
"Fn::ImportValue" => {
"!ImportValue" | "Fn::ImportValue" => {
let resolved_obj = build_resources_recursively(name, resource_object)?;
ResourceValue::ImportValue(Box::new(resolved_obj))
}
"Fn::Select" => {
"!Select" | "Fn::Select" => {
let arr = resource_object.as_array().unwrap();

let index = match arr.get(0) {
Expand All @@ -354,7 +354,7 @@ pub fn build_resources_recursively(

ResourceValue::Select(Box::new(index), Box::new(obj))
}
"Fn::If" => {
"!If" | "Fn::If" => {
let v = match resource_object.as_array() {
None => {
return Err(TransmuteError {
Expand Down Expand Up @@ -400,7 +400,7 @@ pub fn build_resources_recursively(
Box::new(third_obj),
)
}
"Fn::Join" => {
"!Join" | "Fn::Join" => {
let arr = match resource_object.as_array() {
None => {
return Err(TransmuteError {
Expand All @@ -421,7 +421,7 @@ pub fn build_resources_recursively(

ResourceValue::Join(v)
}
"Fn::Cidr" => {
"!Cidr" | "Fn::Cidr" => {
let v = match resource_object.as_array() {
None => {
return Err(TransmuteError {
Expand Down Expand Up @@ -470,7 +470,7 @@ pub fn build_resources_recursively(
Box::new(third_obj),
)
}
"Ref" => {
"!Ref" | "Ref" => {
let ref_name = match resource_object.as_str() {
None => {
return Err(TransmuteError {
Expand Down
27 changes: 27 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,33 @@ fn test_parse_tree_sub_str() {
assert_resource_equal(a, resource);
}

#[test]
fn test_parse_tree_yaml_codes() {
let a = serde_json::json!({
"LogicalResource": {
"Type": "AWS::IAM::Role",
"Properties": {
"RoleName": {
"!Sub": "bobs-role-${AWS::Region}"
}
}
}
});

let resource = ResourceParseTree {
name: "LogicalResource".into(),
condition: Option::None,
metadata: Option::None,
update_policy: Option::None,
deletion_policy: Option::None,
dependencies: vec![],
resource_type: "AWS::IAM::Role".into(),
properties: map! {
"RoleName" => ResourceValue::Sub(vec![ResourceValue::String("bobs-role-${AWS::Region}".into())])
},
};
assert_resource_equal(a, resource);
}
#[test]
fn test_parse_get_attr_shorthand() {
let a = serde_json::json!({
Expand Down

0 comments on commit ebd6919

Please sign in to comment.