Skip to content

Commit a765f6e

Browse files
allow parenthesesed identifier (reference) as an object key (#212)
1 parent 1e8b233 commit a765f6e

File tree

5 files changed

+19
-6
lines changed

5 files changed

+19
-6
lines changed

hcl2/hcl2.lark

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ LPAR : "("
3939
RPAR : ")"
4040
COMMA : ","
4141
DOT : "."
42+
COLON : ":"
4243

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

7576
tuple : "[" (new_line_or_comment* expression new_line_or_comment* ",")* (new_line_or_comment* expression)? new_line_or_comment* "]"
7677
object : "{" new_line_or_comment? (new_line_or_comment* (object_elem | (object_elem COMMA)) new_line_or_comment*)* "}"
77-
object_elem : object_elem_key ( EQ | ":") expression
78+
object_elem : LPAR? object_elem_key RPAR? ( EQ | COLON ) expression
7879
object_elem_key : float_lit | int_lit | identifier | STRING_LIT | object_elem_key_dot_accessor
7980
object_elem_key_dot_accessor : identifier (DOT identifier)+
8081

hcl2/reconstructor.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,12 @@ def _is_string_wrapped_tf(interp_s: str) -> bool:
395395

396396
return True
397397

398+
@classmethod
399+
def _unwrap_interpolation(cls, value: str) -> str:
400+
if cls._is_string_wrapped_tf(value):
401+
return value[2:-1]
402+
return value
403+
398404
def _newline(self, level: int, count: int = 1) -> Tree:
399405
return Tree(
400406
Token("RULE", "new_line_or_comment"),
@@ -560,6 +566,7 @@ def _transform_value_to_expr_term(self, value, level) -> Union[Token, Tree]:
560566
continue
561567

562568
value_expr_term = self._transform_value_to_expr_term(dict_v, level + 1)
569+
k = self._unwrap_interpolation(k)
563570
elements.append(
564571
Tree(
565572
Token("RULE", "object_elem"),

hcl2/transformer.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,14 @@ def tuple(self, args: List) -> List:
9898
def object_elem(self, args: List) -> Dict:
9999
# This returns a dict with a single key/value pair to make it easier to merge these
100100
# into a bigger dict that is returned by the "object" function
101-
key = self.strip_quotes(str(args[0].children[0]))
102-
if len(args) == 3:
103-
value = args[2]
101+
if args[0] == Token("LPAR", "("):
102+
key = self.strip_quotes(str(args[1].children[0]))
103+
key = f"({key})"
104+
key = self.to_string_dollar(key)
105+
value = args[4]
104106
else:
105-
value = args[1]
107+
key = self.strip_quotes(str(args[0].children[0]))
108+
value = args[2]
106109

107110
value = self.to_string_dollar(value)
108111
return {key: value}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
{
4747
"foo": "${var.account}_bar",
4848
"bar": {
49-
"baz": 1
49+
"baz": 1,
50+
"${(var.account)}": 2
5051
}
5152
},
5253
{

test/helpers/terraform-config/variables.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ locals {
88
foo = "${var.account}_bar"
99
bar = {
1010
baz : 1
11+
(var.account) : 2
1112
}
1213
}
1314

0 commit comments

Comments
 (0)