Skip to content

Commit

Permalink
Merge pull request #210 from heaths/issue209
Browse files Browse the repository at this point in the history
Support slices of same type
  • Loading branch information
darccio committed May 25, 2022
2 parents fd7d2bc + 200f90d commit 39ebbbb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
26 changes: 26 additions & 0 deletions issue209_test.go
@@ -0,0 +1,26 @@
package mergo_test

import (
"testing"

"github.com/imdario/mergo"
)

func TestIssue209(t *testing.T) {
dst := []string{"a", "b"}
src := []string{"c", "d"}

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

expected := []string{"a", "b", "c", "d"}
if len(dst) != len(expected) {
t.Errorf("arrays not equal length")
}
for i := range expected {
if dst[i] != expected[i] {
t.Errorf("array elements at %d are not equal", i)
}
}
}
4 changes: 2 additions & 2 deletions mergo.go
Expand Up @@ -17,7 +17,7 @@ import (
var (
ErrNilArguments = errors.New("src and dst must not be nil")
ErrDifferentArgumentsTypes = errors.New("src and dst must be of same type")
ErrNotSupported = errors.New("only structs and maps are supported")
ErrNotSupported = errors.New("only structs, maps, and slices are supported")
ErrExpectedMapAsDestination = errors.New("dst was expected to be a map")
ErrExpectedStructAsDestination = errors.New("dst was expected to be a struct")
ErrNonPointerAgument = errors.New("dst must be a pointer")
Expand Down Expand Up @@ -65,7 +65,7 @@ func resolveValues(dst, src interface{}) (vDst, vSrc reflect.Value, err error) {
return
}
vDst = reflect.ValueOf(dst).Elem()
if vDst.Kind() != reflect.Struct && vDst.Kind() != reflect.Map {
if vDst.Kind() != reflect.Struct && vDst.Kind() != reflect.Map && vDst.Kind() != reflect.Slice {
err = ErrNotSupported
return
}
Expand Down
4 changes: 2 additions & 2 deletions mergo_test.go
Expand Up @@ -930,11 +930,11 @@ func TestMergeMapWithInnerSliceOfDifferentType(t *testing.T) {
}
}

func TestMergeSlicesIsNotSupported(t *testing.T) {
func TestMergeDifferentSlicesIsNotSupported(t *testing.T) {
src := []string{"a", "b"}
dst := []int{1, 2}

if err := mergo.Merge(&src, &dst, mergo.WithOverride, mergo.WithAppendSlice); err != mergo.ErrNotSupported {
if err := mergo.Merge(&src, &dst, mergo.WithOverride, mergo.WithAppendSlice); err != mergo.ErrDifferentArgumentsTypes {
t.Errorf("expected %q, got %q", mergo.ErrNotSupported, err)
}
}

0 comments on commit 39ebbbb

Please sign in to comment.