jinzhu / copier

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

fixes panic when types are different #8

Closed borystomala closed 8 years ago

borystomala commented 8 years ago

Hi,

if you've got fields with same name but different types, Copy panics due to attempt to assign value of wrong type.

type TypeStruct1 struct {
    Field1  string
    Field2  string
}

type TypeStruct2 struct {
    Field1  int
    Field2  string
}

func main() {
    ts := &TypeStruct1 {
        Field1: "str1",
        Field2: "str2",
    }

    ts2 := &TypeStruct2 {}

    Copy(ts2, ts) // panic!
}

This PR fixes this by checking if types are assignable (eg. string => string, string => interface{}).

resouer commented 8 years ago

I like this feature!