Error when parsing YAML file on GOLANG when there's a dash before the square brackets list

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

Error when parsing YAML file on GOLANG when there's a dash before the square brackets list

问题

在使用gopkg.in/yaml.v3解析GOLANG中的YAML文件时,如果YAML文件中的括号前面有一个破折号,则无法读取它。

我有一个类似于以下语法的YAML文件:

servers:

    - server_name: ratamahatta
      server_groups:
          - [r1, r2, r3]

    - server_name: kaiowas
      server_groups:
          - [c1, c2]

如果我在GO中使用以下结构:

package main

import (
	"fmt"
	"io/ioutil"

	"gopkg.in/yaml.v3"
)

type S struct {
	Servers []struct {
		ServerName   string   `yaml:"server_name"`
		ServerGroups []string `yaml:"server_groups"`
	} `yaml:"servers"`
}

func main() {
	file, err := ioutil.ReadFile("example2.yaml")

	if err != nil {
		panic(err)
	}

	var s S

	err = yaml.Unmarshal(file, &s)
	if err != nil {
		panic(err)
	}

	
	fmt.Printf("Value: %#v\n", s.Servers[0].ServerGroups[0])
}

我得到:

  line 5: cannot unmarshal !!seq into string
  line 9: cannot unmarshal !!seq into string

但是,如果我在括号前面删除“-”:

servers:

    - server_name: ratamahatta
      server_groups:
           [r1, r2, r3]

    - server_name: kaiowas
      server_groups:
           [c1, c2]

那么它就可以正常工作。

如果我将这个:

ServerGroups []string `yaml:"server_groups"`

改为

ServerGroups struct {} `yaml:"server_groups"` 

那么我会得到

cannot unmarshal !!seq into struct {}

所以基本上我无法弄清楚如何读取这种情况下的YAML序列。

谢谢

英文:

When parsing a YAML file in GOLANG with gopkg.in/yaml.v3, if the YAML file has a dash before the brackets then it doesn't read it.

I have a YAML file with a syntax similar to this:

servers:

    - server_name: ratamahatta
      server_groups:
          - [r1, r2, r3]

    - server_name: kaiowas
      server_groups:
          - [c1, c2]

If I use the following struct in GO:

package main

import (
	"fmt"
	"io/ioutil"

	"gopkg.in/yaml.v3"
)

type S struct {
	Servers []struct {
		ServerName string   `yaml:"server_name"`
		ServerGroups []string `yaml:"server_groups"`
	} `yaml:"servers"`
}

func main() {
	file, err := ioutil.ReadFile("example2.yaml")

	if err != nil {
		panic(err)
	}

	var s S

	err = yaml.Unmarshal(file, &s)
	if err != nil {
		panic(err)
	}

	
	fmt.Printf("Value: %#v\n", s.Servers[0].ServerGroups[0])
}

I get:

  line 5: cannot unmarshal !!seq into string
  line 9: cannot unmarshal !!seq into string

But, if I remove the "-" before the brackets:

servers:

    - server_name: ratamahatta
      server_groups:
           [r1, r2, r3]

    - server_name: kaiowas
      server_groups:
           [c1, c2]

Then it works perfectly.

If I change this:

ServerGroups []string `yaml:"server_groups"`

To

ServerGroups struct {} `yaml:"server_groups"` 

Then I get

cannot unmarshal !!seq into struct {}

So basically I can't figure out how to read the YAML sequence in this case.

Thanks

答案1

得分: 1

你正在处理的YAML - [r1, r2, r3] 是一个序列的序列,这意味着可能会有其他条目,例如:

- server_name: ratamahatta
  server_groups:
      - [r1, r2, r3]
      - [r4, r5, r6]    

因此,为了解析它,你需要一个额外的维度,即[][]string。以下是一个可工作的示例(playground):

const testYaml = `servers:
- server_name: ratamahatta
  server_groups:
    - [r1, r2, r3]
    - [r4, r5, r6]
- server_name: kaiowas
  server_groups:
    - [c1, c2]
`

type S struct {
	Servers []struct {
		ServerName   string     `yaml:"server_name"`
		ServerGroups [][]string `yaml:"server_groups"`
	} `yaml:"servers"`
}

func main() {
	var s S
	if err := yaml.Unmarshal([]byte(testYaml), &s); err != nil {
		panic(err)
	}
	fmt.Printf("Value: %#v\n", s.Servers[0].ServerGroups[0]) // []string{"r1", "r2", "r3"}
	fmt.Printf("Value: %#v\n", s.Servers[0].ServerGroups[1]) // []string{"r4", "r5", "r6"}
	fmt.Printf("Value: %#v\n", s.Servers[1].ServerGroups[0]) // []string{"c1", "c2"}
}

希望对你有帮助!

英文:

The YAML you are processing - [r1, r2, r3] is a Sequence of Sequences meaning there could be additional entries e.g.

- server_name: ratamahatta
      server_groups:
          - [r1, r2, r3]
          - [r4, r5, r6]    

So to parse this you need an extra dimension i.e. [][]string. A working sample follows (playground):

const testYaml = `servers:
    - server_name: ratamahatta
      server_groups:
          - [r1, r2, r3]
          - [r4, r5, r6]
    - server_name: kaiowas
      server_groups:
          - [c1, c2]
`

type S struct {
	Servers []struct {
		ServerName   string     `yaml:"server_name"`
		ServerGroups [][]string `yaml:"server_groups"`
	} `yaml:"servers"`
}

func main() {
	var s S
	if err := yaml.Unmarshal([]byte(testYaml), &s); err != nil {
		panic(err)
	}
	fmt.Printf("Value: %#v\n", s.Servers[0].ServerGroups[0]) // []string{"r1", "r2", "r3"}
	fmt.Printf("Value: %#v\n", s.Servers[0].ServerGroups[1]) // []string{"r4", "r5", "r6"}
	fmt.Printf("Value: %#v\n", s.Servers[1].ServerGroups[0]) // []string{"c1", "c2"}
}

huangapple
  • 本文由 发表于 2023年1月18日 07:21:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75153219.html
匿名

发表评论

匿名网友

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

确定