Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge doesn't work as expected #1035

Open
keisuke713 opened this issue Jun 7, 2024 · 0 comments
Open

Merge doesn't work as expected #1035

keisuke713 opened this issue Jun 7, 2024 · 0 comments

Comments

@keisuke713
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant