Skip to content

Commit

Permalink
Merge pull request #24 from amplify-education/handle_new_lines_better
Browse files Browse the repository at this point in the history
Handle new lines inside index and binary expressions
  • Loading branch information
aoskotsky-amplify authored Mar 4, 2020
2 parents f395489 + 4d57a7d commit 64a3d8b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
6 changes: 3 additions & 3 deletions hcl2/hcl2.lark
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ conditional : expression "?" expression ":" expression
!unary_op : ("-" | "!") expr_term
binary_op : expression binary_term
!binary_operator : "==" | "!=" | "<" | ">" | "<=" | ">=" | "-" | "*" | "/" | "%" | "&&" | "||" | "+"
binary_term : binary_operator expression
binary_term : binary_operator new_line_or_comment? expression

expr_term : "(" expression ")"
expr_term : "(" new_line_or_comment? expression new_line_or_comment? ")"
| true_lit
| false_lit
| null_lit
Expand Down Expand Up @@ -66,7 +66,7 @@ index_expr_term : expr_term index
get_attr_expr_term : expr_term get_attr
attr_splat_expr_term : expr_term attr_splat
full_splat_expr_term : expr_term full_splat
?index : "[" expression "]"
index : "[" new_line_or_comment? expression new_line_or_comment? "]"
?get_attr : "." identifier
?attr_splat : ".*" get_attr*
?full_splat : "[" "*" "]" (get_attr | index)*
Expand Down
9 changes: 8 additions & 1 deletion hcl2/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,20 @@ def null_lit(self, args: List) -> None:
return None

def expr_term(self, args: List) -> Any:
args = self.strip_new_line_tokens(args)
# if the expression starts with a paren then unwrap it
if args[0] == "(":
return args[1]
# otherwise return the value itself
return args[0]

def index_expr_term(self, args: List) -> str:
return "%s[%s]" % (str(args[0]), str(args[1]))
args = self.strip_new_line_tokens(args)
return "%s%s" % (str(args[0]), str(args[1]))

def index(self, args: List) -> str:
args = self.strip_new_line_tokens(args)
return "[%s]" % (str(args[0]))

def get_attr_expr_term(self, args: List) -> str:
return "%s.%s" % (str(args[0]), str(args[1]))
Expand Down Expand Up @@ -117,6 +123,7 @@ def unary_op(self, args: List) -> str:
return "".join([str(arg) for arg in args])

def binary_term(self, args: List) -> str:
args = self.strip_new_line_tokens(args)
return " ".join([str(arg) for arg in args])

def body(self, args: List) -> Dict[str, List]:
Expand Down
2 changes: 1 addition & 1 deletion hcl2/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Place of record for the package version"""

__version__ = "0.2.4"
__version__ = "0.2.5"
__git_hash__ = "GIT_HASH"
8 changes: 8 additions & 0 deletions test/helpers/terraform-config-json/variables.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
"foo": [
"${var.account}_bar"
]
},
{
"route53_forwarding_rule_shares": [
"${{for forwarding_rule_key in keys(var.route53_resolver_forwarding_rule_shares) : \"${forwarding_rule_key}\" => {'aws_account_ids': '${[for account_name in var.route53_resolver_forwarding_rule_shares[forwarding_rule_key].aws_account_names : module.remote_state_subaccounts.map[account_name].outputs[\"aws_account_id\"]]}'}}}"
],
"has_valid_forwarding_rules_template_inputs": [
"${length(keys(var.forwarding_rules_template.copy_resolver_rules)) > 0 && length(var.forwarding_rules_template.replace_with_target_ips) > 0 && length(var.forwarding_rules_template.exclude_cidrs) > 0}"
]
}
]
}
19 changes: 19 additions & 0 deletions test/helpers/terraform-config/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,22 @@ variable "azs" {
ap-southeast-2 = "ap-southeast-2a,ap-southeast-2b,ap-southeast-2c"
}
}

locals {
route53_forwarding_rule_shares = {
for forwarding_rule_key in keys(var.route53_resolver_forwarding_rule_shares) :
"${forwarding_rule_key}" => {
aws_account_ids = [
for account_name in var.route53_resolver_forwarding_rule_shares[
forwarding_rule_key
].aws_account_names :
module.remote_state_subaccounts.map[account_name].outputs["aws_account_id"]
]
}
}
has_valid_forwarding_rules_template_inputs = (
length(keys(var.forwarding_rules_template.copy_resolver_rules)) > 0 &&
length(var.forwarding_rules_template.replace_with_target_ips) > 0 &&
length(var.forwarding_rules_template.exclude_cidrs) > 0
)
}

0 comments on commit 64a3d8b

Please sign in to comment.