Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions issue256_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package mergo_test

import (
"reflect"
"testing"

"dario.cat/mergo"
)

func TestIssue256MergeMapCaseInsensitively(t *testing.T) {
dst := map[string]map[string]string{
"Config": {
"Host": "localhost",
},
}
src := map[string]map[string]string{
"config": {
"host": "example.com",
"Port": "443",
},
}

if err := mergo.Merge(&dst, src, mergo.WithOverride, mergo.WithCaseInsensitiveMapKeys); err != nil {
t.Fatal(err)
}

expected := map[string]map[string]string{
"Config": {
"Host": "example.com",
"Port": "443",
},
}
if !reflect.DeepEqual(dst, expected) {
t.Fatalf("got %#v, want %#v", dst, expected)
}
}

func TestIssue256MergeMapCaseSensitiveByDefault(t *testing.T) {
dst := map[string]string{"Host": "localhost"}
src := map[string]string{"host": "example.com"}

if err := mergo.Merge(&dst, src, mergo.WithOverride); err != nil {
t.Fatal(err)
}

expected := map[string]string{"Host": "localhost", "host": "example.com"}
if !reflect.DeepEqual(dst, expected) {
t.Fatalf("got %#v, want %#v", dst, expected)
}
}
25 changes: 21 additions & 4 deletions merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package mergo
import (
"fmt"
"reflect"
"strings"
)

func hasMergeableFields(dst reflect.Value) (exported bool) {
Expand Down Expand Up @@ -46,6 +47,7 @@ type Config struct {
overwriteWithEmptyValue bool
overwriteSliceWithEmptyValue bool
sliceDeepCopy bool
caseInsensitiveMapKeys bool
}

type Transformers interface {
Expand Down Expand Up @@ -116,16 +118,26 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co
}

for _, key := range src.MapKeys() {
mergeKey := key
if config.caseInsensitiveMapKeys && key.Kind() == reflect.String && dst.Type().Key().Kind() == reflect.String {
for _, dstKey := range dst.MapKeys() {
if strings.EqualFold(dstKey.String(), key.String()) {
mergeKey = dstKey
break
}
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

srcElement := src.MapIndex(key)
if !srcElement.IsValid() {
continue
}
dstElement := dst.MapIndex(key)
dstElement := dst.MapIndex(mergeKey)
switch srcElement.Kind() {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Interface, reflect.Slice:
if srcElement.IsNil() {
if overwrite {
dst.SetMapIndex(key, srcElement)
dst.SetMapIndex(mergeKey, srcElement)
}
continue
}
Expand Down Expand Up @@ -190,7 +202,7 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co
}

}
dst.SetMapIndex(key, dstSlice)
dst.SetMapIndex(mergeKey, dstSlice)
}
}

Expand All @@ -207,7 +219,7 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co
if dst.IsNil() {
dst.Set(reflect.MakeMap(dst.Type()))
}
dst.SetMapIndex(key, srcElement)
dst.SetMapIndex(mergeKey, srcElement)
}
}

Expand Down Expand Up @@ -367,6 +379,11 @@ func WithSliceDeepCopy(config *Config) {
config.Overwrite = true
}

// WithCaseInsensitiveMapKeys will make merge match string map keys case-insensitively.
func WithCaseInsensitiveMapKeys(config *Config) {
config.caseInsensitiveMapKeys = true
}

func merge(dst, src interface{}, opts ...func(*Config)) error {
if dst != nil && reflect.ValueOf(dst).Kind() != reflect.Ptr {
return ErrNonPointerArgument
Expand Down