go-yaml / yaml

YAML support for the Go language.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Merge doesn't work as expected

keisuke713 opened this issue · comments

I'm having a slight trouble when merging. When I convert yaml declaration into golang struct, the result is not expected.
reproduce code is the follows:

package main

import (
	"fmt"
	"log"

	"gopkg.in/yaml.v3"
)

type (
	paramType string

	yamlMap map[string]keywords

	keywords struct {
		Param map[paramType]params `yaml:"parameter"`
	}

	params struct {
		Keywords []string `yaml:"keywords"`
	}
)

const data = `
mergee_param: &mergee
  param_key:
    keywords:
      - "mergee_val"

merger_param:
  parameter:
    <<: *mergee
    param_key:
      keywords:
        - "merger_val"
`

func main() {
	var mk yamlMap
	err := yaml.Unmarshal([]byte(data), &mk)
	if err != nil {
		log.Fatalf("cannot unmarshal data: %v", err)
	}
	fmt.Println(mk)
}

When I run this program, I expect mk variable to hold merger_val, but mergee_val instead.
According to the doc, overwrite should not occur because current map has the param_key key.
Diving into source code, comparing two bytes does not work. In my opnion, user defined type paramType cause this problem. Actually, using string instead of paramType, the result holds "merger_val".