Skip to content

Commit a8b7269

Browse files
fix parsing of null values in expressions: (#206)
* conditionals * unary * binary * tuples * singular
1 parent 826c2f1 commit a8b7269

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

hcl2/transformer.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ def int_lit(self, args: List) -> int:
5252
def expr_term(self, args: List) -> Any:
5353
args = self.strip_new_line_tokens(args)
5454

55-
#
5655
if args[0] == "true":
5756
return True
5857
if args[0] == "false":
@@ -140,7 +139,7 @@ def provider_function_call(self, args: List) -> str:
140139
return f"{provider_func}({args_str})"
141140

142141
def arguments(self, args: List) -> List:
143-
return args
142+
return self.process_nulls(args)
144143

145144
@v_args(meta=True)
146145
def block(self, meta: Meta, args: List) -> Dict:
@@ -170,16 +169,19 @@ def attribute(self, args: List) -> Attribute:
170169

171170
def conditional(self, args: List) -> str:
172171
args = self.strip_new_line_tokens(args)
172+
args = self.process_nulls(args)
173173
return f"{args[0]} ? {args[1]} : {args[2]}"
174174

175175
def binary_op(self, args: List) -> str:
176176
return " ".join([self.to_tf_inline(arg) for arg in args])
177177

178178
def unary_op(self, args: List) -> str:
179+
args = self.process_nulls(args)
179180
return "".join([self.to_tf_inline(arg) for arg in args])
180181

181182
def binary_term(self, args: List) -> str:
182183
args = self.strip_new_line_tokens(args)
184+
args = self.process_nulls(args)
183185
return " ".join([self.to_tf_inline(arg) for arg in args])
184186

185187
def body(self, args: List) -> Dict[str, List]:
@@ -337,6 +339,9 @@ def process_escape_sequences(self, value: str) -> str:
337339
# for now, but this method can be extended in the future
338340
return value
339341

342+
def process_nulls(self, args: List) -> List:
343+
return ["null" if arg is None else arg for arg in args]
344+
340345
def to_tf_inline(self, value: Any) -> str:
341346
"""
342347
Converts complex objects (e.g.) dicts to an "inline" HCL syntax
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"terraform": {"unary": "${!null}", "binary": "${(a == null)}", "tuple": [null, 1, 2], "single": null, "conditional": "${null ? null : null}"}}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
terraform = {
2+
unary = !null
3+
binary = (a == null)
4+
tuple = [null, 1, 2]
5+
single = null
6+
conditional = null ? null : null
7+
8+
}

test/unit/test_hcl2_syntax.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def test_object(self):
119119
"key4": "${true == false}",
120120
"key5": "${5 + 5}",
121121
"key6": "${function()}",
122-
"key7": "${value == None ? 1 : 0}",
122+
"key7": "${value == null ? 1 : 0}",
123123
}
124124
},
125125
)
@@ -175,16 +175,16 @@ def test_null(self):
175175
result = self.load_to_dict(identifier)
176176
self.assertDictEqual(result, expected)
177177

178-
def test_expr_term_parentheses(self):
178+
def test_expr_term_parenthesis(self):
179179
literals = {
180180
"a = 1 * 2 + 3": {"a": "${1 * 2 + 3}"},
181181
"b = 1 * (2 + 3)": {"b": "${1 * (2 + 3)}"},
182182
"c = (1 * (2 + 3))": {"c": "${(1 * (2 + 3))}"},
183183
"conditional = value == null ? 1 : 0": {
184-
"conditional": "${value == None ? 1 : 0}"
184+
"conditional": "${value == null ? 1 : 0}"
185185
},
186186
"conditional = (value == null ? 1 : 0)": {
187-
"conditional": "${(value == None ? 1 : 0)}"
187+
"conditional": "${(value == null ? 1 : 0)}"
188188
},
189189
}
190190

0 commit comments

Comments
 (0)