忽略的YAML标签

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

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:&quot;EsxiExcludeDatastores&quot;`
} `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:

确定