登录
首页 >  Golang >  Go问答

terraform 提供程序中 TypeList 的类型断言

来源:stackoverflow

时间:2024-02-27 20:00:24 259浏览 收藏

今日不肯埋头,明日何以抬头!每日一句努力自己的话哈哈~哈喽,今天我将给大家带来一篇《terraform 提供程序中 TypeList 的类型断言》,主要内容是讲解等等,感兴趣的朋友可以收藏或者有更好的建议在评论提出,我都会认真看的!大家一起进步,一起学习!

问题内容

我正在编写一个 terraform 提供程序,并且当我有一个包含 typestring 元素的 typelist 时,我试图找出进行类型断言的最佳方法。

资源定义如下:

return &schema.resource{
        create: resourceconfigobjectcreate,
        read:   resourceconfigobjectread,
        update: resourceconfigobjectupdate,
        delete: resourceconfigobjectdelete,

        schema: map[string]*schema.schema{
            "name": &schema.schema{
                type:     schema.typestring,
                required: true,
            },
            "notification_options": &schema.schema{
                type:     schema.typelist,
                optional: true,
                elem: schema.schema{
                    type:             schema.typestring,
                },
            },
        },
    }
}

我想将这些值加载到这样定义的自定义类型:

type configobject struct {
    name                        string   `json:"name,omitempty"`
    notificationoptions         []string `json:"notification_options,omitempty"`

}

由于 schema.resourcedata.get 返回一个接口{},因此需要类型断言。

item := thruk.configobject{
        name: schema.resourcedata.get("name").(string),
        notificationoptions: extractsliceofstrings(schema.resourcedata.get("notification_options")),
    }

我已经轻松地完成了字符串的操作,但字符串切片更复杂,我创建了以下函数:

func extractSliceOfStrings(i interface{}) (slice []string) {
    s := reflect.ValueOf(i)
    if !s.IsValid() {
        return
    }
    for i := 0; i < s.Len(); i++ {
        slice = append(slice, s.Index(i).String())
    }
    return
}

这是正确的方法吗?


解决方案


在 terraform 提供程序中使用 resourcedata api 时,了解哪种 go 类型对应于每种架构类型会很有帮助。您已经推断出 schema.typestring 对应于 string。以下是完整列表:

  • typeboolbool
  • typestringstring
  • typeintint
  • typelist[]接口{}
  • typemapmap[string]接口{}
  • typeset*schema.set
  • elem 设置为 *schema 时的元素类型。resourcemap[string]interface{}

上述翻译记录在 sdk 的 Schema Types 文档页面中,每个标题下为“数据结构:”。

无论何时处理集合,从 go 的角度来看,元素类型始终是 interface{},以反映元素类型直到运行时才确定的事实。但是,上面定义的相同映射规则也适用于这些元素值,因此要转换 elemtypestringtypelist,您首先需要断言切片类型,然后依次断言每个元素:

itemsraw := d.get("example").([]interface{})
items := make([]string, len(itemsraw))
for i, raw := range itemsraw {
    items[i] = raw.(string)
}

不幸的是,由于 go 接口和类型断言的设计,无法一步直接从 []interface{}[]string

如果您最终需要 map[string]string,则可以对 typemap 采用类似的方法:

itemsraw := d.get("example").(map[string]interface{})
items := make(map[string]string, len(itemsraw))
for k, raw := range itemsraw {
    items[k] = raw.(string)
}

由于自定义了 *schema.set 容器,typeset 稍微复杂一些,但是您可以调用该集合的 list 方法来获取 []interface{},然后您可以将其与 typelist 进行相同的处理上图:

itemsRaw := d.Get("example").(*schema.Set).List()
items := make([]string, len(itemsRaw))
for i, raw := range itemsRaw {
    items[i] = raw.(string)
}

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>