goccy / go-yaml

YAML support for the Go language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot unmarshall structs with slices correctly

sercxanto opened this issue · comments

go-yaml is unable to unmarshall the following yaml:

firstlevel:
  secondlevel:
  - field: first field
  - field: second field

given the following structs:

type thirdLevelStruct struct {
	Field string `yaml:"field"`
}
type secondLevelStruct struct {
	SecondLevel []thirdLevelStruct `yaml:"secondlevel"`
}

type firstLevelStruct struct {
	FirstLevel secondLevelStruct `yaml:"firstlevel"`
}

It can marshall the struct to a byte slice, but can't unmarshal the same byte slice back again, but fails instead with an error "cannot unmarshal map[string]interface {} into Go value of type *main.firstLevelStruct"

The struct / yaml combination above works with the library "gopkg.in/yaml.v3", so I guess it is an issue in go-yaml.

Please see this go playground URL on how it can be reproduced: https://go.dev/play/p/5RSLg2-vQFQ

@sercxanto It seems that it is not that the above structure cannot be unmarshalled, but that the variable that your program is specifying to unmarshal is **main.firstLevelStruct ( not *main.firstLevelStruct ).
This is not the way it is supposed to be specified.

@goccy Ah I see, my mistake. I am closing this issue.