r3labs / diff

A library for diffing golang structures
Mozilla Public License 2.0
888 stars 80 forks source link

run this test ,find panic #69

Closed lchjczw closed 2 years ago

lchjczw commented 2 years ago

func TestDiff(t *testing.T) {
    type args struct {
        from interface{}
        to   interface{}
    }
    type Info struct {
        Qq     string
        Wechat string
    }
    type data struct {
        Name   string
        Age    int
        Height int
        Info
        listInt []int
    }

    tests := []struct {
        name    string
        args    args
        wantErr bool
    }{
        {
            name: "lch",
            args: args{
                from: data{
                    Name:   "lch",
                    Age:    1,
                    Height: 1,
                    Info: Info{
                        Qq:     "qq",
                        Wechat: "wechat",
                    },
                    listInt: []int{1, 2, 3, 4},
                },
                to: data{
                    Name:   "lchjczw",
                    Age:    1,
                    Height: 2,
                    Info: Info{
                        Qq:     "myqq",
                        Wechat: "wechat",
                    },
                    listInt: []int{1, 2, 3},
                },
            },
        },
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {

            d, err := diff.NewDiffer(diff.AllowTypeMismatch(true),
                diff.FlattenEmbeddedStructs())
            if err != nil {
                panic(err)
            }

            _, err = d.Diff(tt.args.from, tt.args.to)
            if (err != nil) != tt.wantErr {
                t.Errorf("Diff error = %v, wantErr %v", err, tt.wantErr)
                return
            }

        })
    }
}
lchjczw commented 2 years ago

What did I do wrong? Or maybe it's a bug

purehyperbole commented 2 years ago

Hey @lchjczw , thanks for raising the issue! This should now be fixed with v2.14.2