如何使用HCL编写带有${}的表达式?

huangapple go评论76阅读模式
英文:

How to use hcl write to set expressions with ${}?

问题

我正在尝试使用hclwrite生成.tf文件。

根据hclwrite示例中的例子,我可以生成像foo = env.PATH这样的变量,但我不知道如何生成更多形式的表达式。例如,以下代码:

stage = "prod"
foo   = "hello${var.stage}"

当我使用以下代码设置foo:

SetAttributeValue("foo", cty.StringVal("hello${bar}"))

我得到的结果是:

foo = "hello$${bar}"
英文:

I am trying to use hclwrite to generate .tf files.

According to the example in hclwrite Example, I can generate variables like foo = env.PATH, but I don't know how to generate more forms of expressions. For example, the following.

stage = "prod"
foo   = "hello${var.stage}"

when i set foo with

SetAttributeValue("foo", cty.StringVal("hello${bar}"))

i get

foo = "hello$${bar}"

答案1

得分: 0

hclwrite工具目前没有自动生成任意表达式的功能。它的辅助函数仅限于生成普通引用和字面值。SetAttributeValue用于字面值,所以库正确地转义了你字符串中的${序列,以确保它被按字面意义解释。

如果你想构建一个更复杂的表达式,你需要手动组装形成表达式的标记,然后调用SetAttributeRaw

在你的示例中,看起来你需要生成以下八个标记:

  • TokenOQuote,字节为"
  • TokenQuotedLit,字节为hello
  • TokenTemplateInterp,字节为${
  • TokenIdent,字节为var
  • TokenDot,字节为.
  • TokenIdent,字节为stage
  • TokenTemplateSeqEnd,字节为}
  • TokenCQuote,字节为"

SetAttributeValue函数自动化了生成三个标记的简单情况:TokenOQuoteTokenQuotedLitTokenCQuote

你可以使用TokensForTraversal来自动创建var.stage部分表达式的标记,这也是SetAttributeTraversal在内部使用的方法。然而,除非你已经有一个表示var.stage的解析后的hcl.Traversal,或者除非你需要的东西比你实际展示的更加动态,否则我认为编写代码来构造输入遍历所需的标记比直接按照我上面展示的方式写出这三个标记要更简单。

英文:

The hclwrite tool currently has no facility to automatically generate arbitrary expressions. Its helper functions are limited only to generating plain references and literal values. SetAttributeValue is the one for literal values, and so that's why the library correctly escaped the ${ sequence in your string, to ensure that it will be interpreted literally.

If you want to construct a more elaborate expression then you'll need to do so manually by assembling the tokens that form the expression and then calling SetAttributeRaw instead.

In the case of your example, it looks like you'd need to generate the following eight tokens:

  • TokenOQuote with the bytes "
  • TokenQuotedLit with the bytes hello
  • TokenTemplateInterp with the bytes ${
  • TokenIdent with the bytes var
  • TokenDot with the bytes .
  • TokenIdent with the bytes stage
  • TokenTemplateSeqEnd with the bytes }
  • TokenCQuote with the bytes "

The SetAttributeValue function is automating the simpler case of generating three tokens: TokenOQuote, TokenQuotedLit, TokenCQuote.

You can potentially automate the creation of tokens for the var.stage portion of this expression by using TokensForTraversal, which is what SetAttributeTraversal does internally. However, unless you already have a parsed hcl.Traversal representing var.stage, or unless you need things to be more dynamic than you've shown in practice, I expect that it would take more code to construct that input traversal than to just write out the three tokens literally as I showed above.

huangapple
  • 本文由 发表于 2021年6月12日 11:50:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/67945463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定