Skip to content

Support loading comments #134

@Arianna2028

Description

@Arianna2028

Context
We use this library to parse and modify Terraform files. We do this by reading a particular resource using this library, then using the data from that to write a new resource back to the file in the same place, removing the old one (using with_meta to know where it was). However, since no comments are output, we end up removing any that were present inside the original block.

Possible Enhancement
If possible, it would be really helpful to optionally include parsed comments in the output from loads/load.

For example, if I parse a resource like this:

resource "aws_ec2_tag" "example" {
  # This is a comment
  resource_id = aws_vpn_connection.example.transit_gateway_attachment_id
  key         = "Name"
  value       = "Hello World"
}

I currently get the following dictionary representation:

{
    "resource": [
        {
            "aws_ec2_tag": {
                "example": {
                    "resource_id": "${aws_vpn_connection.example.transit_gateway_attachment_id}",
                    "key": "Name",
                    "value": "Hello World",
                    "__start_line__": 1,
                    "__end_line__": 6
                }
            }
        }
    ]
}

But maybe comment data could be added alongside it, something like:

{
    "resource": [
        {
            "aws_ec2_tag": {
                "example": {
                    "resource_id": "${aws_vpn_connection.example.transit_gateway_attachment_id}",
                    "key": "Name",
                    "value": "Hello World",
                    "__start_line__": 1,
                    "__end_line__": 6,
                    "__comments__": [
                        {
                            "__start_line__": 2,
                            "__end_line__": 2,
                            "text": "This is a comment"
                        }
                    ]
                }
            }
        }
    ]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions