Skip to content

fix parsing of null values in expressions #206

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

Merged
merged 1 commit into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 7 additions & 2 deletions hcl2/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/helpers/terraform-config-json/nulls.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"terraform": {"unary": "${!null}", "binary": "${(a == null)}", "tuple": [null, 1, 2], "single": null, "conditional": "${null ? null : null}"}}
8 changes: 8 additions & 0 deletions test/helpers/terraform-config/nulls.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform = {
unary = !null
binary = (a == null)
tuple = [null, 1, 2]
single = null
conditional = null ? null : null

}
8 changes: 4 additions & 4 deletions test/unit/test_hcl2_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
}
},
)
Expand Down Expand Up @@ -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)}"
},
}

Expand Down