使用Golang解析YAML文件

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

Parsing a yaml file with Golang

问题

我正在尝试使用Golang解析一个yaml文件。我定义了以下types

  1. type DockerNetwork struct {
  2. MyNetwork struct {
  3. driver string
  4. } `yaml:"my_network"`
  5. }
  6. // DockerNetworks represent the docker Networks type
  7. type DockerNetworks struct {
  8. networks []DockerNetwork
  9. }

所以我已经准备好了单元测试:

  1. func TestDockerNetwork(t *testing.T) {
  2. dn := DockerNetworks{}
  3. var data = `
  4. networks:
  5. my_network:
  6. driver: bridge
  7. `
  8. err := yaml.Unmarshal([]byte(data), &dn)
  9. if err != nil {
  10. log.Fatalf("error: %v", err)
  11. t.Error("Could not Unmarshal the data")
  12. }
  13. log.Println(fmt.Sprintf("--- t:\n%v\n\n", dn))
  14. }

我期望它能正常工作,但是我得不到任何输出:

  1. 2016/12/14 13:38:12 --- t:
  2. {{}}

我做错了什么?

英文:

I'm trying to parse a yaml file with Golang. I defined the following types:

  1. type DockerNetwork struct {
  2. MyNetwork struct {
  3. driver string
  4. } `yaml:"my_network"`
  5. }
  6. // DockerNetworks represent the docker Networks type
  7. type DockerNetworks struct {
  8. networks []DockerNetwork
  9. }

so I have my unit test in place:

  1. func TestDockerNetwork(t *testing.T) {
  2. dn := DockerNetworks{}
  3. var data = `
  4. networks:
  5. my_network:
  6. driver: bridge
  7. `
  8. err := yaml.Unmarshal([]byte(data), &dn)
  9. if err != nil {
  10. log.Fatalf("error: %v", err)
  11. t.Error("Could not Unmarshal the data")
  12. }
  13. log.Println(fmt.Sprintf("--- t:\n%v\n\n", dn))
  14. }

I expected it to work, however I'm getting no input:

  1. 2016/12/14 13:38:12 --- t:
  2. {{}}

what am I doing wrong?

答案1

得分: 2

我的yaml文件是一个docker-compose.yml文件,格式如上所述(部分内容)。我同意Marius的观点,我有未导出的字段,这是一个错误。我使用的解决方案是map

  1. m := make(map[interface{}]interface{})
  2. err = yaml.Unmarshal([]byte(data), &m)
  3. if err != nil {
  4. log.Fatalf("错误:%v", err)
  5. }
  6. fmt.Printf("--- m:\n%v\n\n", m)
英文:

My yaml file is a docker-compose.yml file, which is in the format described above (part of it). I agree with Marius that I have unexported fields, which is an error. The solution I used was map:

  1. m := make(map[interface{}]interface{})
  2. err = yaml.Unmarshal([]byte(data), &m)
  3. if err != nil {
  4. log.Fatalf("error: %v", err)
  5. }
  6. fmt.Printf("--- m:\n%v\n\n", m)

答案2

得分: 0

根据你提供的内容,我为你进行了翻译:

据我所知,有两个问题,一个是具有未导出字段的数据结构,另一个是与你的数据结构不匹配的测试数据。

类似这样的代码可以帮助你:

  1. type DockerNetwork struct {
  2. MyNetwork struct {
  3. Driver string `yaml:"driver"`
  4. } `yaml:"my_network"`
  5. }
  6. type DockerNetworks struct {
  7. Networks []DockerNetwork `yaml:"networks"`
  8. }

测试数据如下:

  1. networks:
  2. - my_network:
  3. driver: bridge

输出结果如下:

  1. 2016/12/14 23:47:23 --- t:
  2. {[{{bridge}}]}
  3. PASS

很难确定你认为的“工作”是什么意思。

英文:

AFAIKT there to things, data structures with unexported fields and test data which doesn't match your data structures.

Something like this would give you a go:

  1. type DockerNetwork struct {
  2. MyNetwork struct {
  3. Driver string `yaml:driver`
  4. } `yaml:"my_network"`
  5. }
  6. type DockerNetworks struct {
  7. Networks []DockerNetwork `yaml:networks`
  8. }

And test data:

  1. networks:
  2. - my_network:
  3. driver: bridge

Gives an output:

  1. 2016/12/14 23:47:23 --- t:
  2. {[{{bridge}}]}
  3. PASS

And It's hard to tell, what is "working" in your opinion.

huangapple
  • 本文由 发表于 2016年12月15日 05:21:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/41152405.html
匿名

发表评论

匿名网友

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

确定