Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Fixed

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

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

Expand Down
2 changes: 1 addition & 1 deletion hcl2/hcl2.lark
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,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 | COLON ) expression
object_elem_key : float_lit | int_lit | identifier | STRING_LIT | object_elem_key_dot_accessor | object_elem_key_expression
object_elem_key : float_lit | int_lit | identifier | STRING_LIT | object_elem_key_dot_accessor | object_elem_key_expression | string_with_interpolation
object_elem_key_expression : LPAR expression RPAR
object_elem_key_dot_accessor : identifier (DOT identifier)+

Expand Down
8 changes: 5 additions & 3 deletions hcl2/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,12 @@ 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]))
value = args[2]
key = str(args[0].children[0])
if not re.match(r".*?(\${).*}.*", key):
# do not strip quotes of a interpolation string
key = self.strip_quotes(key)

value = self.to_string_dollar(value)
value = self.to_string_dollar(args[2])
return {key: value}

def object_elem_key_dot_accessor(self, args: List) -> str:
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 @@ -48,7 +48,8 @@
"bar": {
"baz": 1,
"${(var.account)}": 2,
"${(format(\"key_prefix_%s\", local.foo))}": 3
"${(format(\"key_prefix_%s\", local.foo))}": 3,
"\"prefix_${var.account}:${var.user}_suffix\"": "interpolation"
},
"tuple": ["${local.foo}"],
"empty_tuple": []
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 @@ -10,6 +10,7 @@ locals {
baz : 1
(var.account) : 2
(format("key_prefix_%s", local.foo)) : 3
"prefix_${var.account}:${var.user}_suffix":"interpolation",
}
tuple = [local.foo]
empty_tuple = []
Expand Down