Parsing repeated mapping keys according to spec

Bug #1213276 reported by Cezar
10
This bug affects 2 people
Affects Status Importance Assigned to Milestone
goyaml
Confirmed
Undecided
Unassigned

Bug Description

According to http://www.yaml.org/spec/spec.html#mapping keys in a mapping should be unique.

However, goyaml has a mixed behavior when unmarshalling repeated keys depending on the type of the value. As it's shown by the code below.

If the value is a scalar, the last value is unmarshalled. If the value is a sequence, items from both sequences are merged in one.

This doesn't follow the spec and it's not even consistent, since sometimes the key is overridden and sometimes it's merged.

IMHO goyaml should fail to parse YAML's with repeated keys since they are not valid, however some libraries in other languages (like ruby) always consider the last key as the valid one.

I think either option would be better than the current behavior.

```
package main

import (
    "fmt"
    "launchpad.net/goyaml"
)

type conf struct {
    Something string `yaml:"something"`
    Other []string `yaml:"other"`
}

var yamlData = `
something: a
something: b
other:
    - x1
other:
    - x2
`

func main() {
    config := new(conf)
    goyaml.Unmarshal([]byte(yamlData), config)
    fmt.Println(config.Something)
    for _, val := range config.Other {
        fmt.Println(val)
    }
}

// Output:
// b
// x1
// x2
 ```

Changed in goyaml:
status: New → Confirmed
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.