Skip to content

Inconsistent merge behavior #103

@svyotov

Description

@svyotov

The first test, when merging maps with succeed, while the second, where the same maps are being merged as fields to structs it will fail.
I am not sure if this is intended behavior or not, IMO this is a bug.

package mergo

import (
	"testing"

	"github.com/magiconair/properties/assert"
)

func TestMergoSimpleMap(t *testing.T) {
	dst := map[string]string{"key1": "loosethis", "key2": "keepthis"}
	src := map[string]string{"key1": "key10"}
	exp := map[string]string{"key1": "key10", "key2": "keepthis"}
	Merge(&dst, src, WithAppendSlice, WithOverride)
	assert.Equal(t, dst, exp)
}

type CustomStruct struct {
	SomeMap map[string]string
}

func TestMergoComplexStructMap(t *testing.T) {
	dst := map[string]CustomStruct{
		"Normal": CustomStruct{SomeMap: map[string]string{"key1": "loosethis", "key2": "keepthis"}},
	}
	src := map[string]CustomStruct{
		"Normal": CustomStruct{SomeMap: map[string]string{"key1": "key10"}},
	}
	exp := map[string]CustomStruct{
		"Normal": CustomStruct{SomeMap: map[string]string{"key1": "key10", "key2": "keepthis"}},
	}
	Merge(&dst, src, WithAppendSlice, WithOverride)
	assert.Equal(t, dst, exp)
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions