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

hclwrite: handle legacy dot access of numeric indexes #369

Merged
merged 2 commits into from
May 13, 2020

Conversation

bendrucker
Copy link
Contributor

@bendrucker bendrucker commented Apr 29, 2020

This PR allows hclwriter to handle dot syntax for index accesses, e.g.:

module "test" {
  test = kubernetes_config_map.test.metadata.0.name
}

Previously, attempting to partition on an open bracket caused a panic:

https://github.com/hashicorp/hcl/blob/hcl2/hclwrite/parser.go#L408

This PR adds a PartitionTypeOk that is a copy of PartitionType. It returns the same values, plus an ok bool signifying that the token type was found. Otherwise, it returns zero-value inputTokens objects and a false boolean indicating that a partition token could not be found.

The approach to detecting that a traversal is .# style is admittedly fairly naive. However, adding a passing test on a = foo[bar.baz] makes me feel a bit more confident.

Related bug reports:

apparentlymart/terraform-clean-syntax#5
bendrucker/terraform-cloud-migrate#3

I tested a copy of this patch and confirmed that it resolves the second report.

Thank you for all your work here, it has been a joy to develop on! Looking forward to contributing more.

Copy link
Member

@alisdair alisdair left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the diagnosis and fix, @bendrucker! 🎉

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

Successfully merging this pull request may close these issues.

None yet

2 participants