Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
VihasMakwana committed Jan 28, 2025
1 parent 77be9b9 commit 7b5fe09
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
30 changes: 15 additions & 15 deletions confmap/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func merge(path string, src, dest map[string]any) {
maps.Delete(src, pathSplit)

// Note: path is a delimited by KeyDelimiter. We will call "Unflatten" later to get the final config
src[path] = mergeSlice(destVal, srcVal)
src[path] = mergeSlice(srcVal, destVal)
case reflect.Map:
// both of them are maps. Recursively call the mergeMaps
mergeMaps(sVal.(map[string]any), dVal.(map[string]any))
Expand All @@ -83,42 +83,42 @@ func mergeMaps(src, dest map[string]any) {
continue
}

newVal := reflect.ValueOf(sVal)
oldVal := reflect.ValueOf(dVal)
srcVal := reflect.ValueOf(sVal)
destVal := reflect.ValueOf(dVal)

if newVal.Kind() != oldVal.Kind() {
if destVal.Kind() != srcVal.Kind() {
// different kinds, maps.Merge will override the old config
continue
}

switch oldVal.Kind() {
switch srcVal.Kind() {
case reflect.Array, reflect.Slice:
// both of them are array. Merge them
src[dKey] = mergeSlice(oldVal, newVal)
src[dKey] = mergeSlice(srcVal, destVal)
case reflect.Map:
// both of them are maps. Recursively call the mergeMaps
mergeMaps(sVal.(map[string]any), dVal.(map[string]any))
}
}
}

func mergeSlice(old, new reflect.Value) any {
if old.Type() != new.Type() {
return new
func mergeSlice(src, dest reflect.Value) any {
if src.Type() != dest.Type() {
return src
}
slice := reflect.MakeSlice(old.Type(), 0, old.Cap()+new.Cap())
for i := 0; i < old.Len(); i++ {
slice = reflect.Append(slice, old.Index(i))
slice := reflect.MakeSlice(src.Type(), 0, src.Cap()+dest.Cap())
for i := 0; i < dest.Len(); i++ {
slice = reflect.Append(slice, dest.Index(i))
}

OUTER2:
for i := 0; i < new.Len(); i++ {
for i := 0; i < src.Len(); i++ {
for j := 0; j < slice.Len(); j++ {
if slice.Index(j).Equal(new.Index(i)) {
if slice.Index(j).Equal(src.Index(i)) {
continue OUTER2
}
}
slice = reflect.Append(slice, new.Index(i))
slice = reflect.Append(slice, src.Index(i))
}
return slice.Interface()
}
1 change: 0 additions & 1 deletion confmap/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,6 @@ func TestMergeFunctionality(t *testing.T) {
require.NoError(t, err)
for _, tt := range testcases {
t.Run(tt.Name, func(t *testing.T) {

conf := New()
for _, c := range tt.Configs {
require.NoError(t, conf.Merge(NewFromStringMap(c), WithMergePaths(tt.AppendPaths)))
Expand Down

0 comments on commit 7b5fe09

Please sign in to comment.