忽略的YAML标签

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

Ignored YAML tag

问题

我有一个config.yml文件:

  1. vcenter:
  2. connection: https
  3. hostname: vcenter.mydomain.lan
  4. username: readonlyauto@vsphere.local
  5. password: mypasspord
  6. port: 443
  7. EsxiExcludeDatastores:
  8. - datastore1
  9. - datastore2
  10. EsxiExcludeDatastores2:
  11. datastores:
  12. - datastore1
  13. - datastore2

我试图使用"gopkg.in/yaml.v2"解析它。

我创建了一个结构体:

  1. type FileConfig struct {
  2. Vcenter struct {
  3. ConnectionType string `yaml:"connection"`
  4. Hostname string `yaml:"hostname"`
  5. Username string `yaml:"username"`
  6. Password string `yaml:"password"`
  7. Port int `yaml:"port"`
  8. } `yaml:"vcenter"`
  9. EsxiExcludeDatastores struct {
  10. Datastores []string `yaml:"EsxiExcludeDatastores"`
  11. }
  12. EsxiExcludeDatastores2 struct {
  13. Datastores []string `yaml:"datastores"`
  14. } `yaml:"EsxiExcludeDatastores2"`
  15. }
  16. var AppConfig FileConfig

解析后,我打印结果:

  1. fmt.Println("Num of EsxiExcludeDatastores.Datastores = ", len(AppConfig.EsxiExcludeDatastores.Datastores))
  2. fmt.Println("Num of EsxiExcludeDatastores2.Datastores = ", len(AppConfig.EsxiExcludeDatastores2.Datastores))
  3. Num of EsxiExcludeDatastores.Datastores = 0
  4. Num of EsxiExcludeDatastores2.Datastores = 2

你能帮我看看,在EsxiExcludeDatastores结构体中我犯了什么错误吗?如你所见,EsxiExcludeDatastores是空的,但EsxiExcludeDatastores2一切正常。

我尝试了不同的方法,但没有结果...

英文:

I have config.yml file:

  1. vcenter:
  2. connection: https
  3. hostname: vcenter.mydomain.lan
  4. username: readonlyauto@vsphere.local
  5. password: mypasspord
  6. port: 443
  7. EsxiExcludeDatastores:
  8. - datastore1
  9. - datastore2
  10. EsxiExcludeDatastores2:
  11. datastores:
  12. - datastore1
  13. - datastore2

I'm trying to parse it with "gopkg.in/yaml.v2"

I created struct:

  1. type FileConfig struct {
  2. Vcenter struct {
  3. ConnectionType string `yaml:"connection"`
  4. Hostname string `yaml:"hostname"`
  5. Username string `yaml:"username"`
  6. Password string `yaml:"password"`
  7. Port int `yaml:"port"`
  8. } `yaml:"vcenter"`
  9. EsxiExcludeDatastores struct {
  10. Datastores []string `yaml:"EsxiExcludeDatastores"`
  11. }
  12. EsxiExcludeDatastores2 struct {
  13. Datastores []string `yaml:"datastores"`
  14. } `yaml:"EsxiExcludeDatastores2"`
  15. }
  16. var AppConfig FileConfig

After parsing, I print results:

  1. fmt.Println("Num of EsxiExcludeDatastores.Datastores = ", len(AppConfig.EsxiExcludeDatastores.Datastores))
  2. fmt.Println("Num of EsxiExcludeDatastores2.Datastores = ", len(AppConfig.EsxiExcludeDatastores2.Datastores))
  3. Num of EsxiExcludeDatastores.Datastores = 0
  4. 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 结构标签“提升”到“父级”字段,可以添加以下内容:

  1. EsxiExcludeDatastores struct {
  2. Datastores []string `yaml:"EsxiExcludeDatastores"`
  3. } `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:

  1. EsxiExcludeDatastores struct {
  2. Datastores []string `yaml:&quot;EsxiExcludeDatastores&quot;`
  3. } `yaml:&quot;,inline&quot;` // &lt;- 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.

huangapple
  • 本文由 发表于 2021年11月14日 03:34:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/69957605.html
匿名

发表评论

匿名网友

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

确定