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

Merge slice with override no effect #64

Closed
wen-bing opened this issue Feb 11, 2018 · 6 comments
Closed

Merge slice with override no effect #64

wen-bing opened this issue Feb 11, 2018 · 6 comments

Comments

@wen-bing
Copy link

package test5

import (
	"testing"

	"github.com/imdario/mergo"
)

type Strudent struct {
	Name  string
	Books []string
}

func TestSliceNotOverride(t *testing.T) {
	var s1 = Strudent{
		Name:  "Jack",
		Books: []string{"a", "B"},
	}

	var s2 = Strudent{
		Name:  "Tom",
		Books: []string{"1"},
	}

	err := mergo.Merge(&s2, s1, mergo.WithOverride)
	if err != nil {
		t.Log("merge error")
		t.Fail()
	}
	if len(s2.Books) != 2 {
		t.Log("slice not ovrride")
		t.Fail()
	}
}

I expect s2's Books are overrided by s1's Books, however the test failed.

@ravern
Copy link

ravern commented Feb 13, 2018

I think the slice is appended.

@darccio
Copy link
Owner

darccio commented Apr 2, 2018

@wen-bing, I checked your code and I confirm that the slice is being appended as @ravernkoh pointed. I think can be a bug but I need to check all the issues to see if it was reported before.

@darccio
Copy link
Owner

darccio commented Apr 2, 2018

Ok, after reviewing other issues, Mergo works as intended. Merge will append to slices as it adds missing keys in maps. But you are right. WithOverride should work different, as it shouldn't append to destination slice.

@darccio
Copy link
Owner

darccio commented Apr 2, 2018

It has been solved thanks to PR #70.

@darccio darccio closed this as completed Apr 2, 2018
@darccio
Copy link
Owner

darccio commented Apr 2, 2018

And it has been released as v0.3.3.

@wen-bing
Copy link
Author

wen-bing commented Jul 4, 2018

Awesome, Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants