jinzhu / copier

Copier for golang, copy value from struct to struct and more
MIT License
5.58k stars 490 forks source link

Bug with a nested struct #54

Closed georgysavva closed 3 years ago

georgysavva commented 4 years ago

Here is the setup:

type Outer struct {
    Inner Inner1
}

type Inner1 struct {
    Inner *Inner2
}

type Inner2 struct {
    Foo string
}

func TestDeepCopy(t *testing.T) {
    original := &Outer{Inner: Inner1{Inner: &Inner2{Foo: "foo"}}}
    copied := &Outer{}
    copier.Copy(copied, original)
    copied.Inner.Inner.Foo = "bar"
    assert.Equal(t, "foo", original.Inner.Inner.Foo)
}

Outer is the type that I want to copy, it has a nested struct via value: Inner1, that struct has a nested struct via pointer: *Inner2. After coping I update the most inner field Foo in the new struct, and expect, that it won't change in the original struct. But it does change.

pfortin-urbn commented 3 years ago

This is a huge problem for us as well since we use copier in our unit tests and now have to copy the sub structs and then add them to the top level structs this can be very tedious!

jinzhu commented 3 years ago

should fixed in latest master.