diff --git a/CHANGELOG.md b/CHANGELOG.md index 09a08d61..99aa56d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/hcl2/hcl2.lark b/hcl2/hcl2.lark index 9bd41e18..56dd104a 100644 --- a/hcl2/hcl2.lark +++ b/hcl2/hcl2.lark @@ -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)+ diff --git a/hcl2/transformer.py b/hcl2/transformer.py index 670eb617..6f13f9c4 100644 --- a/hcl2/transformer.py +++ b/hcl2/transformer.py @@ -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: diff --git a/test/helpers/terraform-config-json/variables.json b/test/helpers/terraform-config-json/variables.json index 156734b6..d344902c 100644 --- a/test/helpers/terraform-config-json/variables.json +++ b/test/helpers/terraform-config-json/variables.json @@ -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": [] diff --git a/test/helpers/terraform-config/variables.tf b/test/helpers/terraform-config/variables.tf index f4ef2a82..9f0d6c6b 100644 --- a/test/helpers/terraform-config/variables.tf +++ b/test/helpers/terraform-config/variables.tf @@ -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 = []