Skip to content

Commit

Permalink
Support slices of same type
Browse files Browse the repository at this point in the history
Fixes #209
  • Loading branch information
heaths committed May 16, 2022
1 parent fd3dfc9 commit 200f90d
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 200f90d

Please sign in to comment.