jinzhu / copier

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

panic when copying field with same name but different type #43

Closed radaiming closed 1 year ago

radaiming commented 5 years ago

It's a bit similar to #17:


import (
    "log"

    "github.com/jinzhu/copier"
)

type x struct{ Name string }

type structSameName1 struct {
    A string
    B []x
}

type structSameName2 struct {
    A string
    B x
}

func main() {
    obj1 := structSameName1{A: "123", B: []x{{Name: "ee"}}}
    obj2 := &structSameName2{}
    err := copier.Copy(obj2, &obj1)
    log.Println(err)
}

Running this will lead to panic: panic: reflect: call of reflect.Value.FieldByName on slice Value

Not sure if this is a bug, but it won't panic in an old e15f8e2a. And thanks for your time on this project :)

nevernet commented 5 years ago

yep, it's an issue, could you push a pull request?

uded commented 1 year ago

It seems to be a duplicate (or the other one is a duplicate) of #158