Skip to content

DictTransformer.expr_term: do not discard parenthesis #199

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

Merged
merged 1 commit into from
Mar 21, 2025
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
4 changes: 3 additions & 1 deletion hcl2/hcl2.lark
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ PERCENT : "%"
DOUBLE_AMP : "&&"
DOUBLE_PIPE : "||"
PLUS : "+"
LPAR : "("
RPAR : ")"

expr_term : "(" new_line_or_comment? expression new_line_or_comment? ")"
expr_term : LPAR new_line_or_comment? expression new_line_or_comment? RPAR
| float_lit
| int_lit
| STRING_LIT
Expand Down
7 changes: 1 addition & 6 deletions hcl2/reconstructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,12 +631,7 @@ def _transform_value_to_expr_term(self, value, level) -> Union[Token, Tree]:
raise RuntimeError("Token must be `EQ (=)` rule")

parsed_value = attribute.children[2]

if parsed_value.data == Token("RULE", "expr_term"):
return parsed_value

# wrap other types of syntax as an expression (in parentheses)
return Tree(Token("RULE", "expr_term"), [parsed_value])
return parsed_value

# otherwise it's just a string.
return Tree(
Expand Down
7 changes: 3 additions & 4 deletions hcl2/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,9 @@ def expr_term(self, args: List) -> Any:
if args[0] == "null":
return None

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

return args[0]

def index_expr_term(self, args: List) -> str:
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/terraform-config-json/backend.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
{
"aws": {
"region": "${var.backup_region}",
"region": "${(var.backup_region)}",
"alias": "backup"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"locals": [
{
"terraform": {
"channels": "${local.running_in_ci ? local.ci_channels : local.local_channels}",
"channels": "${(local.running_in_ci ? local.ci_channels : local.local_channels)}",
"authentication": [],
"foo": null
}
Expand Down
4 changes: 2 additions & 2 deletions test/helpers/terraform-config-json/route_table.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"aws_route": {
"tgw": {
"count": "${var.tgw_name == \"\" ? 0 : var.number_of_az}",
"count": "${(var.tgw_name == \"\" ? 0 : var.number_of_az)}",
"route_table_id": "${aws_route_table.rt[count.index].id}",
"destination_cidr_block": "10.0.0.0/8",
"transit_gateway_id": "${data.aws_ec2_transit_gateway.tgw[0].id}"
Expand All @@ -13,7 +13,7 @@
{
"aws_route": {
"tgw-dot-index": {
"count": "${var.tgw_name == \"\" ? 0 : var.number_of_az}",
"count": "${(var.tgw_name == \"\" ? 0 : var.number_of_az)}",
"route_table_id": "${aws_route_table.rt[count.index].id}",
"destination_cidr_block": "10.0.0.0/8",
"transit_gateway_id": "${data.aws_ec2_transit_gateway.tgw[0].id}"
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/terraform-config-json/variables.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
},
{
"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}",
"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)}",
"for_whitespace": "${{for i in [1, 2, 3] : i => i}}"
},
{
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_locals_embdedded_condition_tf(self):
builder.block(
"locals",
terraform={
"channels": "${local.running_in_ci ? local.ci_channels : local.local_channels}",
"channels": "${(local.running_in_ci ? local.ci_channels : local.local_channels)}",
"authentication": [],
"foo": None,
},
Expand Down
17 changes: 17 additions & 0 deletions test/unit/test_hcl2_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,20 @@ def test_null(self):

result = self.load_to_dict(identifier)
self.assertDictEqual(result, expected)

def test_expr_term_parentheses(self):
literals = {
"a = 1 * 2 + 3": {"a": "${1 * 2 + 3}"},
"b = 1 * (2 + 3)": {"b": "${1 * (2 + 3)}"},
"c = (1 * (2 + 3))": {"c": "${(1 * (2 + 3))}"},
"conditional = value == null ? 1 : 0": {
"conditional": "${value == None ? 1 : 0}"
},
"conditional = (value == null ? 1 : 0)": {
"conditional": "${(value == None ? 1 : 0)}"
},
}

for actual, expected in literals.items():
result = self.load_to_dict(actual)
self.assertDictEqual(result, expected)
2 changes: 1 addition & 1 deletion test/unit/test_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ def check_terraform(self, hcl_path_str: str):

json_dict = json.load(json_file)
self.assertDictEqual(
hcl2_dict, json_dict, f"failed comparing {hcl_path_str}"
hcl2_dict, json_dict, f"\n\nfailed comparing {hcl_path_str}"
)