zclconf / go-cty

A type system for dynamic values in Go applications
MIT License
348 stars 71 forks source link

cty.StringVal always doubles $ in `${}` output #161

Closed tcgbrett closed 1 year ago

tcgbrett commented 1 year ago

Hello! Apologies if I'm missing something obvious, but I keep running into a strange issue. I am trying to print some dynamic HCL so terraform can inject a var into a string.

Expected output - query = ${thing.thing.id} but body.SetAttributeValue("query", cty.StringVal("${thing.thing.id}")) produces query = "$${thing.thing.id}"

Can anyone help me understand why this is happening and how to avoid it? Thank you!

apparentlymart commented 1 year ago

Hi @tcgbrett,

I think you are describing a behavior of HCL, not a behavior of cty. HCL's string syntax uses ${ to represent the start of a template interpolation, and so HCL must escape it as $${ to ensure that the resulting string will be interpreted literally, rather than being interpreted as a template.

If your intention was to generate query = thing.thing.id then I think you may want body.SetAttributeTraversal instead. If that doesn't get you what you want then I suggest asking in Terraform's community forum, which is a reasonable place to ask general questions about Terraform.

tcgbrett commented 1 year ago

Hey @apparentlymart,

Thank you for the response! Hmm, my goal was to actually not escape it, as I don't want the resulting string to be interpreted literally. I am using CTY to generate HCL for me so I don't have to write dozens of HCL files by hand. My goal is that the CTY will spit out some HCL that has template interpolation baked into it.

To give the exact example, I'm building a bunch of New Relic dashboards. I want CTY to be able to produce HCL interpolation. Here's what I keep getting stuck with tho:

nrql {
  query = "FROM Metric SELECT clamp_max(sum(newrelic.sli.good) / sum(newrelic.sli.valid) * 100, 100) as 'SLO compliance' WHERE entity.guid = $${newrelic_service_level.asdf---test---order-management_apm.id}"
}

And here is the go code

baseSLOQuery := fmt.Sprintf("FROM Metric SELECT clamp_max(sum(newrelic.sli.good) / sum(newrelic.sli.valid) * 100, 100) as 'SLO compliance' WHERE entity.guid = ${newrelic_service_level.%s.id}", neatSLOName)
nrqlBody.SetAttributeValue("query", cty.StringVal(baseSLOQuery))

Do you have any suggestions? Thanks again for reading and commenting :)