diff --git a/hcl2/transformer.py b/hcl2/transformer.py index e2c5f8db..a1b84c35 100644 --- a/hcl2/transformer.py +++ b/hcl2/transformer.py @@ -52,7 +52,6 @@ def int_lit(self, args: List) -> int: def expr_term(self, args: List) -> Any: args = self.strip_new_line_tokens(args) - # if args[0] == "true": return True if args[0] == "false": @@ -140,7 +139,7 @@ def provider_function_call(self, args: List) -> str: return f"{provider_func}({args_str})" def arguments(self, args: List) -> List: - return args + return self.process_nulls(args) @v_args(meta=True) def block(self, meta: Meta, args: List) -> Dict: @@ -170,16 +169,19 @@ def attribute(self, args: List) -> Attribute: def conditional(self, args: List) -> str: args = self.strip_new_line_tokens(args) + args = self.process_nulls(args) return f"{args[0]} ? {args[1]} : {args[2]}" def binary_op(self, args: List) -> str: return " ".join([self.to_tf_inline(arg) for arg in args]) def unary_op(self, args: List) -> str: + args = self.process_nulls(args) return "".join([self.to_tf_inline(arg) for arg in args]) def binary_term(self, args: List) -> str: args = self.strip_new_line_tokens(args) + args = self.process_nulls(args) return " ".join([self.to_tf_inline(arg) for arg in args]) def body(self, args: List) -> Dict[str, List]: @@ -337,6 +339,9 @@ def process_escape_sequences(self, value: str) -> str: # for now, but this method can be extended in the future return value + def process_nulls(self, args: List) -> List: + return ["null" if arg is None else arg for arg in args] + def to_tf_inline(self, value: Any) -> str: """ Converts complex objects (e.g.) dicts to an "inline" HCL syntax diff --git a/test/helpers/terraform-config-json/nulls.json b/test/helpers/terraform-config-json/nulls.json new file mode 100644 index 00000000..d4a9d448 --- /dev/null +++ b/test/helpers/terraform-config-json/nulls.json @@ -0,0 +1 @@ +{"terraform": {"unary": "${!null}", "binary": "${(a == null)}", "tuple": [null, 1, 2], "single": null, "conditional": "${null ? null : null}"}} diff --git a/test/helpers/terraform-config/nulls.tf b/test/helpers/terraform-config/nulls.tf new file mode 100644 index 00000000..4f15c206 --- /dev/null +++ b/test/helpers/terraform-config/nulls.tf @@ -0,0 +1,8 @@ +terraform = { + unary = !null + binary = (a == null) + tuple = [null, 1, 2] + single = null + conditional = null ? null : null + +} diff --git a/test/unit/test_hcl2_syntax.py b/test/unit/test_hcl2_syntax.py index b234a369..440d1b68 100644 --- a/test/unit/test_hcl2_syntax.py +++ b/test/unit/test_hcl2_syntax.py @@ -119,7 +119,7 @@ def test_object(self): "key4": "${true == false}", "key5": "${5 + 5}", "key6": "${function()}", - "key7": "${value == None ? 1 : 0}", + "key7": "${value == null ? 1 : 0}", } }, ) @@ -175,16 +175,16 @@ def test_null(self): result = self.load_to_dict(identifier) self.assertDictEqual(result, expected) - def test_expr_term_parentheses(self): + def test_expr_term_parenthesis(self): literals = { "a = 1 * 2 + 3": {"a": "${1 * 2 + 3}"}, "b = 1 * (2 + 3)": {"b": "${1 * (2 + 3)}"}, "c = (1 * (2 + 3))": {"c": "${(1 * (2 + 3))}"}, "conditional = value == null ? 1 : 0": { - "conditional": "${value == None ? 1 : 0}" + "conditional": "${value == null ? 1 : 0}" }, "conditional = (value == null ? 1 : 0)": { - "conditional": "${(value == None ? 1 : 0)}" + "conditional": "${(value == null ? 1 : 0)}" }, }