Skip to content

Commit 9693d42

Browse files
DictTransformer.expr_term: do not discard parentheses (#199)
1 parent 2c34df6 commit 9693d42

File tree

10 files changed

+31
-18
lines changed

10 files changed

+31
-18
lines changed

hcl2/hcl2.lark

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ PERCENT : "%"
3535
DOUBLE_AMP : "&&"
3636
DOUBLE_PIPE : "||"
3737
PLUS : "+"
38+
LPAR : "("
39+
RPAR : ")"
3840

39-
expr_term : "(" new_line_or_comment? expression new_line_or_comment? ")"
41+
expr_term : LPAR new_line_or_comment? expression new_line_or_comment? RPAR
4042
| float_lit
4143
| int_lit
4244
| STRING_LIT

hcl2/reconstructor.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -631,12 +631,7 @@ def _transform_value_to_expr_term(self, value, level) -> Union[Token, Tree]:
631631
raise RuntimeError("Token must be `EQ (=)` rule")
632632

633633
parsed_value = attribute.children[2]
634-
635-
if parsed_value.data == Token("RULE", "expr_term"):
636-
return parsed_value
637-
638-
# wrap other types of syntax as an expression (in parentheses)
639-
return Tree(Token("RULE", "expr_term"), [parsed_value])
634+
return parsed_value
640635

641636
# otherwise it's just a string.
642637
return Tree(

hcl2/transformer.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,9 @@ def expr_term(self, args: List) -> Any:
5959
if args[0] == "null":
6060
return None
6161

62-
# if the expression starts with a paren then unwrap it
63-
if args[0] == "(":
64-
return args[1]
65-
# otherwise return the value itself
62+
if args[0] == "(" and args[-1] == ")":
63+
return "".join(str(arg) for arg in args)
64+
6665
return args[0]
6766

6867
def index_expr_term(self, args: List) -> str:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
},
88
{
99
"aws": {
10-
"region": "${var.backup_region}",
10+
"region": "${(var.backup_region)}",
1111
"alias": "backup"
1212
}
1313
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"locals": [
33
{
44
"terraform": {
5-
"channels": "${local.running_in_ci ? local.ci_channels : local.local_channels}",
5+
"channels": "${(local.running_in_ci ? local.ci_channels : local.local_channels)}",
66
"authentication": [],
77
"foo": null
88
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{
44
"aws_route": {
55
"tgw": {
6-
"count": "${var.tgw_name == \"\" ? 0 : var.number_of_az}",
6+
"count": "${(var.tgw_name == \"\" ? 0 : var.number_of_az)}",
77
"route_table_id": "${aws_route_table.rt[count.index].id}",
88
"destination_cidr_block": "10.0.0.0/8",
99
"transit_gateway_id": "${data.aws_ec2_transit_gateway.tgw[0].id}"
@@ -13,7 +13,7 @@
1313
{
1414
"aws_route": {
1515
"tgw-dot-index": {
16-
"count": "${var.tgw_name == \"\" ? 0 : var.number_of_az}",
16+
"count": "${(var.tgw_name == \"\" ? 0 : var.number_of_az)}",
1717
"route_table_id": "${aws_route_table.rt[count.index].id}",
1818
"destination_cidr_block": "10.0.0.0/8",
1919
"transit_gateway_id": "${data.aws_ec2_transit_gateway.tgw[0].id}"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
},
5252
{
5353
"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\"]]}\"}}}",
54-
"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}",
54+
"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)}",
5555
"for_whitespace": "${{for i in [1, 2, 3] : i => i}}"
5656
},
5757
{

test/unit/test_builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_locals_embdedded_condition_tf(self):
4242
builder.block(
4343
"locals",
4444
terraform={
45-
"channels": "${local.running_in_ci ? local.ci_channels : local.local_channels}",
45+
"channels": "${(local.running_in_ci ? local.ci_channels : local.local_channels)}",
4646
"authentication": [],
4747
"foo": None,
4848
},

test/unit/test_hcl2_syntax.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,20 @@ def test_null(self):
174174

175175
result = self.load_to_dict(identifier)
176176
self.assertDictEqual(result, expected)
177+
178+
def test_expr_term_parentheses(self):
179+
literals = {
180+
"a = 1 * 2 + 3": {"a": "${1 * 2 + 3}"},
181+
"b = 1 * (2 + 3)": {"b": "${1 * (2 + 3)}"},
182+
"c = (1 * (2 + 3))": {"c": "${(1 * (2 + 3))}"},
183+
"conditional = value == null ? 1 : 0": {
184+
"conditional": "${value == None ? 1 : 0}"
185+
},
186+
"conditional = (value == null ? 1 : 0)": {
187+
"conditional": "${(value == None ? 1 : 0)}"
188+
},
189+
}
190+
191+
for actual, expected in literals.items():
192+
result = self.load_to_dict(actual)
193+
self.assertDictEqual(result, expected)

test/unit/test_load.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,5 @@ def check_terraform(self, hcl_path_str: str):
5353

5454
json_dict = json.load(json_file)
5555
self.assertDictEqual(
56-
hcl2_dict, json_dict, f"failed comparing {hcl_path_str}"
56+
hcl2_dict, json_dict, f"\n\nfailed comparing {hcl_path_str}"
5757
)

0 commit comments

Comments
 (0)