Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle nested interpolations #61

Merged
merged 8 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ 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).

## \[3.0.4] - 2022-02-20
- Handle nested interpolations. Thanks @arielkru and @matt-land ([#61](https://github.com/amplify-education/python-hcl2/pull/61))

## \[3.0.3] - 2022-02-20
- Fix nested splat statements. Thanks josh-barker [(#80](https://github.com/amplify-education/python-hcl2/pull/80)]
- Fix nested splat statements. Thanks josh-barker ([#80](https://github.com/amplify-education/python-hcl2/pull/80))

## \[3.0.2] - 2022-02-20
- Fix issue of whitespace around for expressions. Thanks @ryanking and @matchaxnb ([#87](https://github.com/amplify-education/python-hcl2/pull/87))
Expand Down
3 changes: 2 additions & 1 deletion hcl2/hcl2.lark
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ expr_term : "(" new_line_or_comment? expression new_line_or_comment? ")"

STRING_LIT : "\"" (STRING_CHARS | INTERPOLATION)* "\""
STRING_CHARS : /(?:(?!\${)([^"\\]|\\.))+/+ // any character except '"" unless inside a interpolation string
INTERPOLATION : "${" /[^}]+/ "}"
NESTED_INTERPOLATION : "${" /[^}]+/ "}"
INTERPOLATION : "${" (/(?:(?!\${)([^}]))+/ | NESTED_INTERPOLATION)+ "}"

int_lit : DECIMAL+
!float_lit: DECIMAL+ "." DECIMAL+ (EXP_MARK DECIMAL+)?
Expand Down
2 changes: 1 addition & 1 deletion hcl2/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Place of record for the package version"""

__version__ = "3.0.3"
__version__ = "3.0.4"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"locals": [
{
"function_test": "${var.basename}-${var.forwarder_function_name}_${md5(\"${var.vpc_id}${data.aws_region.current.name}\")}"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"locals": [
{
"embedded_interpolation": "${module.special_constants.aws_accounts[\"aaa-${local.foo}-${local.bar}\"]}/us-west-2/key_foo"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"locals": [
{
"multi_function": "${substr(split(\"-\",\"us-west-2\")[0],0,1)}",
"multi_function_embedded": "${substr(split(\"-\", \"us-west-2\")[0], 0, 1)}"
}
]
}
2 changes: 1 addition & 1 deletion test/helpers/terraform-config-json/variables.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,4 @@
"ids_level_3": "${flatten(local.nested_data[*].nested[*].again[*][0].foo.bar[0])}"
}
]
}
}
3 changes: 3 additions & 0 deletions test/helpers/terraform-config/locals_embedded_function.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
locals {
function_test = "${var.basename}-${var.forwarder_function_name}_${md5("${var.vpc_id}${data.aws_region.current.name}")}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
locals {
embedded_interpolation = "${module.special_constants.aws_accounts["aaa-${local.foo}-${local.bar}"]}/us-west-2/key_foo"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# provided with and without the quotes, to validate the test output.
# both values should evaluate to the same values
locals {
multi_function = substr(split("-", "us-west-2")[0], 0, 1)
multi_function_embedded = "${substr(split("-", "us-west-2")[0], 0, 1)}"
}
7 changes: 5 additions & 2 deletions test/unit/test_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ def _load_test_files(self):

with self.subTest(msg=file_path):
with open(file_path, 'r') as hcl2_file, open(json_file_path, 'r') as json_file:
hcl2_dict = hcl2.load(hcl2_file)
try:
hcl2_dict = hcl2.load(hcl2_file)
except Exception as ex:
raise RuntimeError(f"failed to tokenize terraform in `{file_path}`") from ex
json_dict = json.load(json_file)

self.assertDictEqual(hcl2_dict, json_dict)
self.assertDictEqual(hcl2_dict, json_dict, f"failed comparing {file_path}")