jinzhu / copier

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

[bug report] The private field is hindering the copier from functioning properly. #201

Closed XiXiangFiles closed 1 year ago

XiXiangFiles commented 1 year ago

Reproducible Example

reproduce

Description

func TestCase(t *testing.T) {
    type DstStruct struct {
        state string

        Id    int32
        State string
    }

    type MockState string
    type SrcStruct struct {
        Id    int32
        State MockState
    }

    src := &SrcStruct{
        Id:    1,
        State: "test_str",
    }
    dst := DstStruct{}

    _ = copier.CopyWithOption(&dst, src, copier.Option{
        Converters: []copier.TypeConverter{
            {
                SrcType: "",
                DstType: MockState(""),
                Fn: func(src interface{}) (interface{}, error) {
                    s, _ := src.(string)
                    return MockState(s), nil
                },
            },
        },
    })
    if dst.Id != 1 {
        t.Errorf("dst.id expect is 1, but it is '%v'", dst.Id)
    }
    if dst.State != "test_str" {
        t.Errorf("dst.State expect is 'test_str', but it is '%v'", dst.State)
    }
}

I believe it's a case-sensitive issue. Removing DstStruct.state should resolve it, but since that is a private field, it should not be causing the problem.

In the version 0.3.5, it work.

XiXiangFiles commented 1 year ago

I discovered that this is not a bug issue. I found the optional CaseSensitive and opened it. This problem will be fixed. https://go.dev/play/p/JZLLT4TG1Ho