goccy / go-yaml

YAML support for the Go language
MIT License
1.12k stars 129 forks source link

Cannot unmarshall structs with slices correctly #408

Closed sercxanto closed 10 months ago

sercxanto commented 10 months ago

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

goccy commented 10 months ago

@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.

sercxanto commented 10 months ago

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