From 98b86cebd74020b846505f7a3d1196709e6a6e00 Mon Sep 17 00:00:00 2001 From: Kamil Kozik Date: Thu, 15 May 2025 13:26:03 +0200 Subject: [PATCH 1/3] support `string_with_interpolation` as an object key --- hcl2/hcl2.lark | 2 +- hcl2/transformer.py | 8 +++++--- test/helpers/terraform-config-json/variables.json | 3 ++- test/helpers/terraform-config/variables.tf | 1 + 4 files changed, 9 insertions(+), 5 deletions(-) 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..122eb05e 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}_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..d9b23e4d 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}_suffix":"interpolation", } tuple = [local.foo] empty_tuple = [] From aeb3df096fb4f0dfa949ef56b35f7d9b8f80feeb Mon Sep 17 00:00:00 2001 From: Kamil Kozik Date: Fri, 16 May 2025 14:01:38 +0200 Subject: [PATCH 2/3] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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 From 0b948aae3a562b0ccbe460979c8d4dfe501410f1 Mon Sep 17 00:00:00 2001 From: Kamil Kozik Date: Fri, 16 May 2025 14:15:01 +0200 Subject: [PATCH 3/3] update test --- test/helpers/terraform-config-json/variables.json | 2 +- test/helpers/terraform-config/variables.tf | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/helpers/terraform-config-json/variables.json b/test/helpers/terraform-config-json/variables.json index 122eb05e..d344902c 100644 --- a/test/helpers/terraform-config-json/variables.json +++ b/test/helpers/terraform-config-json/variables.json @@ -49,7 +49,7 @@ "baz": 1, "${(var.account)}": 2, "${(format(\"key_prefix_%s\", local.foo))}": 3, - "\"prefix_${var.account}_suffix\"": "interpolation" + "\"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 d9b23e4d..9f0d6c6b 100644 --- a/test/helpers/terraform-config/variables.tf +++ b/test/helpers/terraform-config/variables.tf @@ -10,7 +10,7 @@ locals { baz : 1 (var.account) : 2 (format("key_prefix_%s", local.foo)) : 3 - "prefix_${var.account}_suffix":"interpolation", + "prefix_${var.account}:${var.user}_suffix":"interpolation", } tuple = [local.foo] empty_tuple = []