Skip to content

reverse_transform not working with variables of type object #231

@MargaridaMC

Description

@MargaridaMC

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

Metadata

Metadata

Labels

bugSomething isn't workingfixed-in-8.0this issue has been fixed in `transformer-overhaul` feature branch, to be released as v8.0transformationThe issue is related to logic of transforming Lark AST into Python data structure

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions