Skip to content

Commit 64a3d8b

Browse files
Merge pull request #24 from amplify-education/handle_new_lines_better
Handle new lines inside index and binary expressions
2 parents f395489 + 4d57a7d commit 64a3d8b

File tree

5 files changed

+39
-5
lines changed

5 files changed

+39
-5
lines changed

hcl2/hcl2.lark

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ conditional : expression "?" expression ":" expression
1616
!unary_op : ("-" | "!") expr_term
1717
binary_op : expression binary_term
1818
!binary_operator : "==" | "!=" | "<" | ">" | "<=" | ">=" | "-" | "*" | "/" | "%" | "&&" | "||" | "+"
19-
binary_term : binary_operator expression
19+
binary_term : binary_operator new_line_or_comment? expression
2020

21-
expr_term : "(" expression ")"
21+
expr_term : "(" new_line_or_comment? expression new_line_or_comment? ")"
2222
| true_lit
2323
| false_lit
2424
| null_lit
@@ -66,7 +66,7 @@ index_expr_term : expr_term index
6666
get_attr_expr_term : expr_term get_attr
6767
attr_splat_expr_term : expr_term attr_splat
6868
full_splat_expr_term : expr_term full_splat
69-
?index : "[" expression "]"
69+
index : "[" new_line_or_comment? expression new_line_or_comment? "]"
7070
?get_attr : "." identifier
7171
?attr_splat : ".*" get_attr*
7272
?full_splat : "[" "*" "]" (get_attr | index)*

hcl2/transformer.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,20 @@ def null_lit(self, args: List) -> None:
2727
return None
2828

2929
def expr_term(self, args: List) -> Any:
30+
args = self.strip_new_line_tokens(args)
3031
# if the expression starts with a paren then unwrap it
3132
if args[0] == "(":
3233
return args[1]
3334
# otherwise return the value itself
3435
return args[0]
3536

3637
def index_expr_term(self, args: List) -> str:
37-
return "%s[%s]" % (str(args[0]), str(args[1]))
38+
args = self.strip_new_line_tokens(args)
39+
return "%s%s" % (str(args[0]), str(args[1]))
40+
41+
def index(self, args: List) -> str:
42+
args = self.strip_new_line_tokens(args)
43+
return "[%s]" % (str(args[0]))
3844

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

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

122129
def body(self, args: List) -> Dict[str, List]:

hcl2/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
"""Place of record for the package version"""
22

3-
__version__ = "0.2.4"
3+
__version__ = "0.2.5"
44
__git_hash__ = "GIT_HASH"

test/helpers/terraform-config-json/variables.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@
2828
"foo": [
2929
"${var.account}_bar"
3030
]
31+
},
32+
{
33+
"route53_forwarding_rule_shares": [
34+
"${{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\"]]}'}}}"
35+
],
36+
"has_valid_forwarding_rules_template_inputs": [
37+
"${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}"
38+
]
3139
}
3240
]
3341
}

test/helpers/terraform-config/variables.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,22 @@ variable "azs" {
2020
ap-southeast-2 = "ap-southeast-2a,ap-southeast-2b,ap-southeast-2c"
2121
}
2222
}
23+
24+
locals {
25+
route53_forwarding_rule_shares = {
26+
for forwarding_rule_key in keys(var.route53_resolver_forwarding_rule_shares) :
27+
"${forwarding_rule_key}" => {
28+
aws_account_ids = [
29+
for account_name in var.route53_resolver_forwarding_rule_shares[
30+
forwarding_rule_key
31+
].aws_account_names :
32+
module.remote_state_subaccounts.map[account_name].outputs["aws_account_id"]
33+
]
34+
}
35+
}
36+
has_valid_forwarding_rules_template_inputs = (
37+
length(keys(var.forwarding_rules_template.copy_resolver_rules)) > 0 &&
38+
length(var.forwarding_rules_template.replace_with_target_ips) > 0 &&
39+
length(var.forwarding_rules_template.exclude_cidrs) > 0
40+
)
41+
}

0 commit comments

Comments
 (0)