Skip to content

allow parenthesesed identifier (reference) as an object key #212

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
Apr 4, 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
3 changes: 2 additions & 1 deletion hcl2/hcl2.lark
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ LPAR : "("
RPAR : ")"
COMMA : ","
DOT : "."
COLON : ":"

expr_term : LPAR new_line_or_comment? expression new_line_or_comment? RPAR
| float_lit
Expand Down Expand Up @@ -74,7 +75,7 @@ EQ : /[ \t]*=(?!=|>)/

tuple : "[" (new_line_or_comment* expression new_line_or_comment* ",")* (new_line_or_comment* expression)? new_line_or_comment* "]"
object : "{" new_line_or_comment? (new_line_or_comment* (object_elem | (object_elem COMMA)) new_line_or_comment*)* "}"
object_elem : object_elem_key ( EQ | ":") expression
object_elem : LPAR? object_elem_key RPAR? ( EQ | COLON ) expression
object_elem_key : float_lit | int_lit | identifier | STRING_LIT | object_elem_key_dot_accessor
object_elem_key_dot_accessor : identifier (DOT identifier)+

Expand Down
7 changes: 7 additions & 0 deletions hcl2/reconstructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,12 @@ def _is_string_wrapped_tf(interp_s: str) -> bool:

return True

@classmethod
def _unwrap_interpolation(cls, value: str) -> str:
if cls._is_string_wrapped_tf(value):
return value[2:-1]
return value

def _newline(self, level: int, count: int = 1) -> Tree:
return Tree(
Token("RULE", "new_line_or_comment"),
Expand Down Expand Up @@ -560,6 +566,7 @@ def _transform_value_to_expr_term(self, value, level) -> Union[Token, Tree]:
continue

value_expr_term = self._transform_value_to_expr_term(dict_v, level + 1)
k = self._unwrap_interpolation(k)
elements.append(
Tree(
Token("RULE", "object_elem"),
Expand Down
11 changes: 7 additions & 4 deletions hcl2/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,14 @@ def tuple(self, args: List) -> List:
def object_elem(self, args: List) -> Dict:
# This returns a dict with a single key/value pair to make it easier to merge these
# into a bigger dict that is returned by the "object" function
key = self.strip_quotes(str(args[0].children[0]))
if len(args) == 3:
value = args[2]
if args[0] == Token("LPAR", "("):
key = self.strip_quotes(str(args[1].children[0]))
key = f"({key})"
key = self.to_string_dollar(key)
value = args[4]
else:
value = args[1]
key = self.strip_quotes(str(args[0].children[0]))
value = args[2]

value = self.to_string_dollar(value)
return {key: value}
Expand Down
3 changes: 2 additions & 1 deletion test/helpers/terraform-config-json/variables.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
{
"foo": "${var.account}_bar",
"bar": {
"baz": 1
"baz": 1,
"${(var.account)}": 2
}
},
{
Expand Down
1 change: 1 addition & 0 deletions test/helpers/terraform-config/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ locals {
foo = "${var.account}_bar"
bar = {
baz : 1
(var.account) : 2
}
}

Expand Down