hashicorp / hcl

HCL is the HashiCorp configuration language.
Mozilla Public License 2.0
5.27k stars 590 forks source link

Variable evaluation in labels #289

Open c4milo opened 5 years ago

c4milo commented 5 years ago

Has there been discussions about supporting variable evaluations in labels? I understand it may not make sense for Terraform but it may be useful for other applications.

apparentlymart commented 5 years ago

Hi @c4milo,

A key design constraint for HCL was that it should be possible to traverse the tree of nested blocks and analyze them before expression evaluation is possible, because the block types and labels are commonly used to decide which variables to make available for expression evaluation.

The current API has no room for supporting such a thing because the labels are read out into constant strings immediately during decoding. In principle it would've been possible to make these instead be hcl.Expression and have the caller call Value(nil) on them to get the constant value, but I'm not really sure how we'd retrofit that into the existing API without a significant breaking change at this point.

We could certainly have a think about it. I expect there is a way to make it work, but it likely won't be trivial, particularly in the JSON syntax where we can't tell from the input alone whether the user is intending to use expressions and so we currently just assume the interpretation is as a literal string. I don't expect to have time to work on this until some time after Terraform v0.12 is out, but if we can find a reasonable way to do it without breaking API compatibility at any of the levels of abstraction I'm open to it.

c4milo commented 5 years ago

@apparentlymart, thanks for the thorough answer! I agree that this doesn't seem to have a simple fix without breaking backwards compatibility.