From 7ae07d955169b46118208b6868e2176a675d11ef Mon Sep 17 00:00:00 2001 From: Kamil Kozik Date: Fri, 4 Apr 2025 11:17:50 +0200 Subject: [PATCH] allowed parenthesesed identifier (reference) as an object key --- hcl2/hcl2.lark | 3 ++- hcl2/reconstructor.py | 7 +++++++ hcl2/transformer.py | 11 +++++++---- test/helpers/terraform-config-json/variables.json | 3 ++- test/helpers/terraform-config/variables.tf | 1 + 5 files changed, 19 insertions(+), 6 deletions(-) diff --git a/hcl2/hcl2.lark b/hcl2/hcl2.lark index d33f6f45..99df91e8 100644 --- a/hcl2/hcl2.lark +++ b/hcl2/hcl2.lark @@ -39,6 +39,7 @@ LPAR : "(" RPAR : ")" COMMA : "," DOT : "." +COLON : ":" expr_term : LPAR new_line_or_comment? expression new_line_or_comment? RPAR | float_lit @@ -74,7 +75,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 | ":") expression +object_elem : LPAR? object_elem_key RPAR? ( EQ | COLON ) expression object_elem_key : float_lit | int_lit | identifier | STRING_LIT | object_elem_key_dot_accessor object_elem_key_dot_accessor : identifier (DOT identifier)+ diff --git a/hcl2/reconstructor.py b/hcl2/reconstructor.py index d011147f..f2580114 100644 --- a/hcl2/reconstructor.py +++ b/hcl2/reconstructor.py @@ -395,6 +395,12 @@ def _is_string_wrapped_tf(interp_s: str) -> bool: return True + @classmethod + def _unwrap_interpolation(cls, value: str) -> str: + if cls._is_string_wrapped_tf(value): + return value[2:-1] + return value + def _newline(self, level: int, count: int = 1) -> Tree: return Tree( Token("RULE", "new_line_or_comment"), @@ -560,6 +566,7 @@ def _transform_value_to_expr_term(self, value, level) -> Union[Token, Tree]: continue value_expr_term = self._transform_value_to_expr_term(dict_v, level + 1) + k = self._unwrap_interpolation(k) elements.append( Tree( Token("RULE", "object_elem"), diff --git a/hcl2/transformer.py b/hcl2/transformer.py index 2ac45dcd..a26b5f31 100644 --- a/hcl2/transformer.py +++ b/hcl2/transformer.py @@ -98,11 +98,14 @@ def tuple(self, args: List) -> List: 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])) - if len(args) == 3: - value = args[2] + if args[0] == Token("LPAR", "("): + key = self.strip_quotes(str(args[1].children[0])) + key = f"({key})" + key = self.to_string_dollar(key) + value = args[4] else: - value = args[1] + key = self.strip_quotes(str(args[0].children[0])) + value = args[2] value = self.to_string_dollar(value) return {key: value} diff --git a/test/helpers/terraform-config-json/variables.json b/test/helpers/terraform-config-json/variables.json index 3889ff30..088e19cd 100644 --- a/test/helpers/terraform-config-json/variables.json +++ b/test/helpers/terraform-config-json/variables.json @@ -46,7 +46,8 @@ { "foo": "${var.account}_bar", "bar": { - "baz": 1 + "baz": 1, + "${(var.account)}": 2 } }, { diff --git a/test/helpers/terraform-config/variables.tf b/test/helpers/terraform-config/variables.tf index b6d5babf..6047a77f 100644 --- a/test/helpers/terraform-config/variables.tf +++ b/test/helpers/terraform-config/variables.tf @@ -8,6 +8,7 @@ locals { foo = "${var.account}_bar" bar = { baz : 1 + (var.account) : 2 } }