【发布时间】:2022-07-06 07:32:07
【问题描述】:
我正在创建一个自定义 terraform 提供程序,但遇到了这个问题。
我试图将schema.TypeList 字段转换为结构,TypeList 看起来像这样:
"template": {
Type: schema.TypeList,
Required: true,
ForceNew: false,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"lists_test": {
Type: schema.TypeSet,
Required: true,
ForceNew: false,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"name_test": {
Type: schema.TypeString,
Required: true,
ForceNew: false,
},
},},
我试图对齐的结构看起来像这样:
type TestStruct struct {
NameTest string `json:"name_test"`
ListsTests []string `json:"lists_test"`
}
我尝试了几个解决方案,例如我尝试将其解组为 json。如下所示:
template := d.Get("template").([]interface{})[0].(map[string]interface{})
templateStr, err := json.Marshal(template)
templateConverted := &TestStruct{}
json.Unmarshal(template, templateConverted)
但是,我收到错误 json: unsupported type: SchemaSetFunc,这可能是因为它试图编组 schema.Schema 类型而不是 map[string]interface{} 类型,这让我感到困惑。我也尝试使用gohcl.DecodeBody,但我放弃了这个想法,因为它的用法似乎更倾向于直接读取 tf 文件而不是*schema.ResourceData 类型。
有没有人在处理这种情况时有同样的经历?任何帮助或建议表示赞赏。谢谢!
【问题讨论】:
标签: json go terraform terraform-provider-gcp hcl