Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

MapSlice does not support anchors/merges #184

@Mange

Description

@Mange

Hi! I seem to have found a bug in MapSlice:

import (
    "gopkg.in/yaml.v2"
    "reflect"
    "testing"
)

func TestMapMerging(t *testing.T) {
    data := `---
world: &world
  greeting: Hello
earth:
  << : *world`

    config := map[string]interface{}{}
    err := yaml.Unmarshal([]byte(data), &config)
    if err != nil {
        t.Errorf("Failed to parse\n%v\n\ninto a %T: %v", data, config, err)
    }

    earth := config["earth"]
    expected := map[interface{}]interface{}{
        "greeting": "Hello",
    }

    if !reflect.DeepEqual(earth, expected) {
        t.Errorf("Merging does not work. Expected %v (%T), got %v (%T)", expected, expected, earth, earth)
    }
}

func TestMapSliceMerging(t *testing.T) {
    data := `---
world: &world
  greeting: Hello
earth:
  << : *world`

    config := make(yaml.MapSlice, 0)
    err := yaml.Unmarshal([]byte(data), &config)
    if err != nil {
        t.Errorf("Failed to parse\n%v\n\ninto a %T: %v", data, config, err)
    }

    earth := config[1].Value
    expected := yaml.MapSlice{{Key: "greeting", Value: "Hello"}}

    if !reflect.DeepEqual(earth, expected) {
        t.Errorf("Merging does not work. Expected %v (%T), got %v (%T)", expected, expected, earth, earth)
    }
}

Output:

--- FAIL: TestMapSliceMerging (0.00s)
    foo_test.go:264: Merging does not work. Expected [{greeting Hello}] (yaml.MapSlice), got [] (yaml.MapSlice)

Note how the test with the normal map[string]interface{} works properly. I'm still a beginner in Go, so I'm not sure where to start to fix this one. I have a really hard time understanding the decoding code, but a small pointer in the right direction might help me find some way of fixing this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions