Skip to content

Commit 8281686

Browse files
allow indefinite dot accessor as an object key (#209)
1 parent 4da158c commit 8281686

5 files changed

Lines changed: 22 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8+
## \[Unreleased\]
9+
10+
### Fixed
11+
12+
- Issue parsing dot-accessed attribute as an object key ([#209](https://github.com/amplify-education/python-hcl2/pull/209))
13+
814
## \[7.0.0\] - 2025-03-27
915

1016
### Added

hcl2/hcl2.lark

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ PLUS : "+"
3838
LPAR : "("
3939
RPAR : ")"
4040
COMMA : ","
41+
DOT : "."
4142

4243
expr_term : LPAR new_line_or_comment? expression new_line_or_comment? RPAR
4344
| float_lit
@@ -74,8 +75,8 @@ EQ : /[ \t]*=(?!=|>)/
7475
tuple : "[" (new_line_or_comment* expression new_line_or_comment* ",")* (new_line_or_comment* expression)? new_line_or_comment* "]"
7576
object : "{" new_line_or_comment? (new_line_or_comment* (object_elem | (object_elem COMMA)) new_line_or_comment*)* "}"
7677
object_elem : object_elem_key ( EQ | ":") expression
77-
object_elem_key : float_lit | int_lit | identifier | STRING_LIT
78-
78+
object_elem_key : float_lit | int_lit | identifier | STRING_LIT | object_elem_key_dot_accessor
79+
object_elem_key_dot_accessor : identifier (DOT identifier)+
7980

8081
heredoc_template : /<<(?P<heredoc>[a-zA-Z][a-zA-Z0-9._-]+)\n?(?:.|\n)*?\n\s*(?P=heredoc)\n/
8182
heredoc_template_trim : /<<-(?P<heredoc_trim>[a-zA-Z][a-zA-Z0-9._-]+)\n?(?:.|\n)*?\n\s*(?P=heredoc_trim)\n/

hcl2/transformer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ def object_elem(self, args: List) -> Dict:
107107
value = self.to_string_dollar(value)
108108
return {key: value}
109109

110+
def object_elem_key_dot_accessor(self, args: List) -> str:
111+
return "".join(args)
112+
110113
def object(self, args: List) -> Dict:
111114
args = self.strip_new_line_tokens(args)
112115
result: Dict[str, Any] = {}

test/helpers/terraform-config-json/s3.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@
3636
"source": "s3_bucket_name",
3737
"name": "audit",
3838
"account": "${var.account}",
39-
"region": "${var.region}"
39+
"region": "${var.region}",
40+
"providers": {
41+
"aws.ue1": "${aws}",
42+
"aws.uw2.attribute": "${aws.backup}"
43+
}
4044
}
4145
}
4246
]

test/helpers/terraform-config/s3.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ module "bucket_name" {
2828
name = "audit"
2929
account = var.account
3030
region = var.region
31+
32+
providers = {
33+
aws.ue1 = aws
34+
aws.uw2.attribute = aws.backup
35+
}
3136
}

0 commit comments

Comments
 (0)