我正在尋找一種在hclwrite中使用go-cty包查找變數型別的方法。
我的目標是生成一個變數檔案,如下所示
variable "test_var" {
val1 = bool
val2 = string
val3 = number
}
參考:https ://developer.hashicorp.com/terraform/language/values/variables
我正在使用下面的代碼來生成它。
vars := hclwrite.NewEmptyFile()
vars_root_body := vars.Body()
vars_file, vars_create_err := os.Create("variables.tf")
logErrors(vars_create_err)
vars_block := vars_root_body.AppendNewBlock("variable",[]string{"test_var"})
vars_block_body := vars_block.Body()
vars_block_body.SetAttributeValue("val", cty.Value{})
_, vars_write_err := vars_file.Write(vars.Bytes())
logErrors(vars_write_err)
defer vars_file.Close()
上面的代碼生成這個
variable "test_var" {
val = null
}
我想獲取該變數的型別并根據該型別設定屬性值,如上面的參考鏈接所示。我嘗試了很多方法,但沒有得到任何東西。有人可以幫我嗎?
我嘗試了上面的代碼和很多其他方法,比如
cty.SetValEmpty(cty.Bool)
但它沒有用。
uj5u.com熱心網友回復:
variableTerraform 中塊的預期語法包括一個名為 的引數type,而不是一個名為 的引數val。從您的示例中,我假設您打算填充type.
Terraform 使用的型別約束語法不是 HCL 的直接一部分,因此沒有任何內置方法可以僅一步生成該語法。但是,型別約束是從 HCL 的識別符號和函式呼叫語法構建的,并且hclwrite確實有一些函式可以幫助將它們生成為單獨的部分:
TokensForIdentifierTokensForFunctionCall
f := hclwrite.NewEmptyFile()
rootBody := f.Body()
varBlock := rootBody.AppendNewBlock("variable", []string{"example"})
varBody := varBlock.Body()
varBody.SetAttributeRaw(
"type",
hclwrite.TokensForFunctionCall(
"set",
hclwrite.TokensForIdentifier("string"),
),
)
fmt.Printf("%s", f.Bytes())
以上將生成以下內容:
variable "example" {
type = set(string)
}
如果您已經有一個cty.Value值,那么您可以使用該Type方法獲取其型別。但是,如上所述,沒有任何現成的函式可以將型別轉換為型別運算式,因此如果您希望能夠為任何值生成型別約束,那么您需要為這個自己,包裝TokensForFunctionCallandTokensForIdentifier函式。例如:
package main
import (
"fmt"
"sort"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/zclconf/go-cty/cty"
)
func main() {
f := hclwrite.NewEmptyFile()
rootBody := f.Body()
varBlock := rootBody.AppendNewBlock("variable", []string{"example"})
varBody := varBlock.Body()
varBody.SetAttributeRaw(
"type",
typeExprTokens(cty.Set(cty.String)),
)
fmt.Printf("%s", f.Bytes())
}
func typeExprTokens(ty cty.Type) hclwrite.Tokens {
switch ty {
case cty.String:
return hclwrite.TokensForIdentifier("string")
case cty.Bool:
return hclwrite.TokensForIdentifier("bool")
case cty.Number:
return hclwrite.TokensForIdentifier("number")
case cty.DynamicPseudoType:
return hclwrite.TokensForIdentifier("any")
}
if ty.IsCollectionType() {
etyTokens := typeExprTokens(ty.ElementType())
switch {
case ty.IsListType():
return hclwrite.TokensForFunctionCall("list", etyTokens)
case ty.IsSetType():
return hclwrite.TokensForFunctionCall("set", etyTokens)
case ty.IsMapType():
return hclwrite.TokensForFunctionCall("map", etyTokens)
default:
// Should never happen because the above is exhaustive
panic("unsupported collection type")
}
}
if ty.IsObjectType() {
atys := ty.AttributeTypes()
names := make([]string, 0, len(atys))
for name := range atys {
names = append(names, name)
}
sort.Strings(names)
items := make([]hclwrite.ObjectAttrTokens, len(names))
for i, name := range names {
items[i] = hclwrite.ObjectAttrTokens{
Name: hclwrite.TokensForIdentifier(name),
Value: typeExprTokens(atys[name]),
}
}
return hclwrite.TokensForObject(items)
}
if ty.IsTupleType() {
etys := ty.TupleElementTypes()
items := make([]hclwrite.Tokens, len(etys))
for i, ety := range etys {
items[i] = typeExprTokens(ety)
}
return hclwrite.TokensForTuple(items)
}
panic(fmt.Errorf("unsupported type %#v", ty))
}
該程式將生成與上一個示例相同的輸出。您可以更改func main以傳遞不同的型別,typeExprTokens以查看它對某些不同型別的行為。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/530856.html
標籤:去地形盐酸
上一篇:如何從游標位置向下清除終端螢屏?
