From c86b4a63c776de4a6de1e4baaa21ece12206f671 Mon Sep 17 00:00:00 2001 From: Kamil Kozik Date: Thu, 27 Mar 2025 22:04:56 +0100 Subject: [PATCH] allow indefinite dot accessor as an object key --- CHANGELOG.md | 6 ++++++ hcl2/hcl2.lark | 5 +++-- hcl2/transformer.py | 3 +++ test/helpers/terraform-config-json/s3.json | 6 +++++- test/helpers/terraform-config/s3.tf | 5 +++++ 5 files changed, 22 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b701d9b..1ce76f38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## \[Unreleased\] + +### Fixed + +- Issue parsing dot-accessed attribute as an object key ([#209](https://github.com/amplify-education/python-hcl2/pull/209)) + ## \[7.0.0\] - 2025-03-27 ### Added diff --git a/hcl2/hcl2.lark b/hcl2/hcl2.lark index 7a9ff6cb..d33f6f45 100644 --- a/hcl2/hcl2.lark +++ b/hcl2/hcl2.lark @@ -38,6 +38,7 @@ PLUS : "+" LPAR : "(" RPAR : ")" COMMA : "," +DOT : "." expr_term : LPAR new_line_or_comment? expression new_line_or_comment? RPAR | float_lit @@ -74,8 +75,8 @@ 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_key : float_lit | int_lit | identifier | STRING_LIT - +object_elem_key : float_lit | int_lit | identifier | STRING_LIT | object_elem_key_dot_accessor +object_elem_key_dot_accessor : identifier (DOT identifier)+ heredoc_template : /<<(?P[a-zA-Z][a-zA-Z0-9._-]+)\n?(?:.|\n)*?\n\s*(?P=heredoc)\n/ heredoc_template_trim : /<<-(?P[a-zA-Z][a-zA-Z0-9._-]+)\n?(?:.|\n)*?\n\s*(?P=heredoc_trim)\n/ diff --git a/hcl2/transformer.py b/hcl2/transformer.py index a1b84c35..2ac45dcd 100644 --- a/hcl2/transformer.py +++ b/hcl2/transformer.py @@ -107,6 +107,9 @@ def object_elem(self, args: List) -> Dict: value = self.to_string_dollar(value) return {key: value} + def object_elem_key_dot_accessor(self, args: List) -> str: + return "".join(args) + def object(self, args: List) -> Dict: args = self.strip_new_line_tokens(args) result: Dict[str, Any] = {} diff --git a/test/helpers/terraform-config-json/s3.json b/test/helpers/terraform-config-json/s3.json index 41a1a9f6..d3318a21 100644 --- a/test/helpers/terraform-config-json/s3.json +++ b/test/helpers/terraform-config-json/s3.json @@ -36,7 +36,11 @@ "source": "s3_bucket_name", "name": "audit", "account": "${var.account}", - "region": "${var.region}" + "region": "${var.region}", + "providers": { + "aws.ue1": "${aws}", + "aws.uw2.attribute": "${aws.backup}" + } } } ] diff --git a/test/helpers/terraform-config/s3.tf b/test/helpers/terraform-config/s3.tf index 006118ab..b4a1df33 100644 --- a/test/helpers/terraform-config/s3.tf +++ b/test/helpers/terraform-config/s3.tf @@ -28,4 +28,9 @@ module "bucket_name" { name = "audit" account = var.account region = var.region + + providers = { + aws.ue1 = aws + aws.uw2.attribute = aws.backup + } }