jinzhu / copier

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

In some cases, copying all variables failed when using tags to copy #197

Open xichencx opened 1 year ago

xichencx commented 1 year ago

Reproducible Example

func TestName(t testing.T) { t.Run("validate error flag name", func(t testing.T) { type SrcTags struct { field1 string Field2 string }

    type DestTags struct {
        Field1 string `copier:"field1"`
        Field2 string `copier:"Field2"`
    }

    dst := &DestTags{}
    src := &SrcTags{
        field1: "Field1->Field1",
        Field2: "Field2->Field2",
    }
    err := copier.Copy(dst, src)

    t.Log(dst)
    if err == nil {
        t.Fatal("must validate error")
    }
})

}

Description

src structure中存在私有变量,dst structure中使用copier标签进行拷贝会导致src structure中其他变量拷贝失败

XiXiangFiles commented 11 months ago

I don't believe this is a bug. The private field I expect should not be copied, and an error should be returned. What are your expectations regarding this?

xichencx commented 11 months ago

I don't believe this is a bug. The private field I expect should not be copied, and an error should be returned. What are your expectations regarding this?

如果按照上述说法,那我这种方式去进行copy的时候会成功

`func TestName(t testing.T) { t.Run("validate error flag name", func(t testing.T) { type SrcTags struct { Field1 string field2 string }

    type DestTags struct {
        Field1 string `copier:"Field1"`
        Field2 string `copier:"Field2"`
    }

    dst := &DestTags{}
    src := &SrcTags{
        Field1: "Field1->Field1",
        field2: "Field2->Field2",
    }
    err := copier.Copy(dst, src)

    t.Log(dst)
    if err == nil {
        t.Fatal("must validate error")
    }
})

}`

image
XiXiangFiles commented 11 months ago

@xichencx here I tried your codebase. it'll raise an error, is it in your expectation? here image