Skip to content
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

Support loading comments #134

Open
Arianna2028 opened this issue May 8, 2023 · 2 comments
Open

Support loading comments #134

Arianna2028 opened this issue May 8, 2023 · 2 comments

Comments

@Arianna2028
Copy link

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"
                        }
                    ]
                }
            }
        }
    ]
}
@IButskhrikidze
Copy link
Contributor

IButskhrikidze commented Jun 20, 2023

Seems would be helpful for some cases but requires making some keys reserved.

for instance, what if comments will be key itself?

@Arianna2028
Copy link
Author

Yeah, I guess it would mean the __comments__ key would be reserved. Isn't this technically true for __start_line__ and __end_line__ already as well, though?

Alternatively, maybe __comments__ could be renamed to something with characters that are not considered valid in Terraform, so there is no possible conflict? I believe symbols are not considered valid syntax, so that might work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants