python-hcl2 icon indicating copy to clipboard operation
python-hcl2 copied to clipboard

reverse_transform not working with variables of type object

Open MargaridaMC opened this issue 8 months ago • 0 comments

Describe the bug I'm using this great library to parse a terraform script, manipulate it and write it back to a .tf file. I've noticed that parsing a string and reconstructing it does not return the original value, when we have a variable block of type object.

Software:

  • OS: macOS
  • Python version 3.13.1
  • python-hcl2 version 7.2.0

Snippet of HCL2 code causing the unexpected behaviour:

import hcl2

original_string = """
    variable "object" {
        type = object({
            key   = string
            value = string
        })
    }
    """

parsed_hcl = hcl2.loads(original_string, with_meta=True)
ast = hcl2.reverse_transform(parsed_hcl)
reconstructed_string = hcl2.writes(ast)

print(reconstructed_string)

# Prints:
# variable "object" {
#   type = object({"key":"string","value":"string"})
# }

Expected behavior

One would expect to get back the original value:

 variable "object" {
        type = object({
            key   = string
            value = string
        })
    }

Exception traceback (if applicable): N/a

MargaridaMC avatar May 09 '25 13:05 MargaridaMC