Skip to content

Commit 7afa933

Browse files
DictTransformer - do not wrap type literals into ${ and } (#186)
1 parent ed6ca76 commit 7afa933

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

hcl2/transformer.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ class DictTransformer(Transformer):
3030

3131
with_meta: bool
3232

33+
@staticmethod
34+
def is_type_keyword(value: str) -> bool:
35+
return value in {"bool", "number", "string"}
36+
3337
def __init__(self, with_meta: bool = False):
3438
"""
3539
:param with_meta: If set to true then adds `__start_line__` and `__end_line__`
@@ -301,6 +305,10 @@ def to_string_dollar(self, value: Any) -> Any:
301305
if value.startswith('"') and value.endswith('"'):
302306
value = str(value)[1:-1]
303307
return self.process_escape_sequences(value)
308+
309+
if self.is_type_keyword(value):
310+
return value
311+
304312
return f"${{{value}}}"
305313
return value
306314

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@
4242
{
4343
"some_var2": {
4444
"description": "description",
45-
"type": "${string}",
45+
"type": "string",
4646
"default": "${cidrsubnets(\"10.0.0.0/24\", 2, 2)}"
4747
}
4848
},
4949
{
50-
"some_var2": {
50+
"some_var3": {
5151
"description": "description",
5252
"default": "${concat([{\"1\": \"1\"}], [{\"2\": \"2\"}])}"
5353
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
},
2323
{
2424
"options": {
25-
"type": "${string}",
25+
"type": "string",
2626
"default": {}
2727
}
2828
},
2929
{
3030
"var_with_validation": {
31-
"type": "${list(object({\"id\": \"${string}\", \"nested\": \"${list(object({\"id\": \"${string}\", \"type\": \"${string}\"}))}\"}))}",
31+
"type": "${list(object({\"id\": \"string\", \"nested\": \"${list(object({\"id\": \"string\", \"type\": \"string\"}))}\"}))}",
3232
"validation": [
3333
{
3434
"condition": "${!contains([for v in flatten(var.var_with_validation[*].id) : can(regex(\"^(A|B)$\", v))], false)}",

test/helpers/terraform-config/multiline_expressions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ variable "some_var2" {
4747
)
4848
}
4949

50-
variable "some_var2" {
50+
variable "some_var3" {
5151
description = "description"
5252
default = concat(
5353
# comment 1

test/unit/test_dict_transformer.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# pylint:disable=C0114,C0116,C0103,W0612
2+
3+
from unittest import TestCase
4+
5+
from hcl2.transformer import DictTransformer
6+
7+
8+
class TestDictTransformer(TestCase):
9+
"""Test behaviour of hcl2.transformer.DictTransformer class"""
10+
11+
@staticmethod
12+
def build_dict_transformer(with_meta: bool = False) -> DictTransformer:
13+
return DictTransformer(with_meta)
14+
15+
def test_to_string_dollar(self):
16+
string_values = {
17+
'"bool"': "bool",
18+
'"number"': "number",
19+
'"string"': "string",
20+
"${value_1}": "${value_1}",
21+
'"value_2': '${"value_2}',
22+
'value_3"': '${value_3"}',
23+
'"value_4"': "value_4",
24+
"value_5": "${value_5}",
25+
}
26+
27+
dict_transformer = self.build_dict_transformer()
28+
29+
for value, expected in string_values.items():
30+
actual = dict_transformer.to_string_dollar(value)
31+
32+
self.assertEqual(actual, expected)

0 commit comments

Comments
 (0)