英文:
Ignored YAML tag
问题
我有一个config.yml文件:
vcenter:
connection: https
hostname: vcenter.mydomain.lan
username: readonlyauto@vsphere.local
password: mypasspord
port: 443
EsxiExcludeDatastores:
- datastore1
- datastore2
EsxiExcludeDatastores2:
datastores:
- datastore1
- datastore2
我试图使用"gopkg.in/yaml.v2"解析它。
我创建了一个结构体:
type FileConfig struct {
Vcenter struct {
ConnectionType string `yaml:"connection"`
Hostname string `yaml:"hostname"`
Username string `yaml:"username"`
Password string `yaml:"password"`
Port int `yaml:"port"`
} `yaml:"vcenter"`
EsxiExcludeDatastores struct {
Datastores []string `yaml:"EsxiExcludeDatastores"`
}
EsxiExcludeDatastores2 struct {
Datastores []string `yaml:"datastores"`
} `yaml:"EsxiExcludeDatastores2"`
}
var AppConfig FileConfig
解析后,我打印结果:
fmt.Println("Num of EsxiExcludeDatastores.Datastores = ", len(AppConfig.EsxiExcludeDatastores.Datastores))
fmt.Println("Num of EsxiExcludeDatastores2.Datastores = ", len(AppConfig.EsxiExcludeDatastores2.Datastores))
Num of EsxiExcludeDatastores.Datastores = 0
Num of EsxiExcludeDatastores2.Datastores = 2
你能帮我看看,在EsxiExcludeDatastores结构体中我犯了什么错误吗?如你所见,EsxiExcludeDatastores是空的,但EsxiExcludeDatastores2一切正常。
我尝试了不同的方法,但没有结果...
英文:
I have config.yml file:
vcenter:
connection: https
hostname: vcenter.mydomain.lan
username: readonlyauto@vsphere.local
password: mypasspord
port: 443
EsxiExcludeDatastores:
- datastore1
- datastore2
EsxiExcludeDatastores2:
datastores:
- datastore1
- datastore2
I'm trying to parse it with "gopkg.in/yaml.v2"
I created struct:
type FileConfig struct {
Vcenter struct {
ConnectionType string `yaml:"connection"`
Hostname string `yaml:"hostname"`
Username string `yaml:"username"`
Password string `yaml:"password"`
Port int `yaml:"port"`
} `yaml:"vcenter"`
EsxiExcludeDatastores struct {
Datastores []string `yaml:"EsxiExcludeDatastores"`
}
EsxiExcludeDatastores2 struct {
Datastores []string `yaml:"datastores"`
} `yaml:"EsxiExcludeDatastores2"`
}
var AppConfig FileConfig
After parsing, I print results:
fmt.Println("Num of EsxiExcludeDatastores.Datastores = ", len(AppConfig.EsxiExcludeDatastores.Datastores))
fmt.Println("Num of EsxiExcludeDatastores2.Datastores = ", len(AppConfig.EsxiExcludeDatastores2.Datastores))
Num of EsxiExcludeDatastores.Datastores = 0
Num of EsxiExcludeDatastores2.Datastores = 2
Can you help me, where I did mistake in EsxiExcludeDatastores struct? As you see, everything is fine with EsxiExcludeDatastores2, but EsxiExcludeDatastores is empty.
I tried to do this in different ways, but with no result ...
答案1
得分: 3
你已经接近成功了。
要将 yaml 结构标签“提升”到“父级”字段,可以添加以下内容:
EsxiExcludeDatastores struct {
Datastores []string `yaml:"EsxiExcludeDatastores"`
} `yaml:",inline"` // <- 这里
https://play.golang.org/p/S7QxGaspTyN
根据 yaml.v2 文档,使用 inline
标签的结构标签会执行以下操作:
内联字段,该字段必须是一个结构体或映射,导致其所有字段或键被处理为外部结构体的一部分。对于映射,键不能与其他结构字段的 yaml 键冲突。
英文:
You're almost there.
To "promote" yaml struct tag(s) to the "parent" field, add:
EsxiExcludeDatastores struct {
Datastores []string `yaml:"EsxiExcludeDatastores"`
} `yaml:",inline"` // <- this
https://play.golang.org/p/S7QxGaspTyN
From the yaml.v2 docs, a struct tag that uses the inline
flag does the following:
> Inline the field, which must be a struct or a map, causing all of its
> fields or keys to be processed as if they were part of the outer
> struct. For maps, keys must not conflict with the yaml keys of other
> struct fields.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论