Skip to content

Commit 0710134

Browse files
support string_with_interpolation as an object key (#232)
1 parent 7493dfd commit 0710134

File tree

5 files changed

+10
-5
lines changed

5 files changed

+10
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1010
### Fixed
1111

1212
- More robust escaping for special characters. Thanks, @eranor ([#224](https://github.com/amplify-education/python-hcl2/pull/224))
13+
- Issue parsing interpolation string as an object key ([#232](https://github.com/amplify-education/python-hcl2/pull/232))
1314

1415
## \[7.2.0\] - 2025-04-24
1516

hcl2/hcl2.lark

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ EQ : /[ \t]*=(?!=|>)/
7777
tuple : "[" (new_line_or_comment* expression new_line_or_comment* ",")* (new_line_or_comment* expression)? new_line_or_comment* "]"
7878
object : "{" new_line_or_comment? (new_line_or_comment* (object_elem | (object_elem COMMA)) new_line_or_comment*)* "}"
7979
object_elem : object_elem_key ( EQ | COLON ) expression
80-
object_elem_key : float_lit | int_lit | identifier | STRING_LIT | object_elem_key_dot_accessor | object_elem_key_expression
80+
object_elem_key : float_lit | int_lit | identifier | STRING_LIT | object_elem_key_dot_accessor | object_elem_key_expression | string_with_interpolation
8181
object_elem_key_expression : LPAR expression RPAR
8282
object_elem_key_dot_accessor : identifier (DOT identifier)+
8383

hcl2/transformer.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,12 @@ def object_elem(self, args: List) -> Dict:
102102
# This returns a dict with a single key/value pair to make it easier to merge these
103103
# into a bigger dict that is returned by the "object" function
104104

105-
key = self.strip_quotes(str(args[0].children[0]))
106-
value = args[2]
105+
key = str(args[0].children[0])
106+
if not re.match(r".*?(\${).*}.*", key):
107+
# do not strip quotes of a interpolation string
108+
key = self.strip_quotes(key)
107109

108-
value = self.to_string_dollar(value)
110+
value = self.to_string_dollar(args[2])
109111
return {key: value}
110112

111113
def object_elem_key_dot_accessor(self, args: List) -> str:

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
"bar": {
4949
"baz": 1,
5050
"${(var.account)}": 2,
51-
"${(format(\"key_prefix_%s\", local.foo))}": 3
51+
"${(format(\"key_prefix_%s\", local.foo))}": 3,
52+
"\"prefix_${var.account}:${var.user}_suffix\"": "interpolation"
5253
},
5354
"tuple": ["${local.foo}"],
5455
"empty_tuple": []

test/helpers/terraform-config/variables.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ locals {
1010
baz : 1
1111
(var.account) : 2
1212
(format("key_prefix_%s", local.foo)) : 3
13+
"prefix_${var.account}:${var.user}_suffix":"interpolation",
1314
}
1415
tuple = [local.foo]
1516
empty_tuple = []

0 commit comments

Comments
 (0)