Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support slices of same type #210

Merged
merged 1 commit into from May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}
}