Skip to content

Commit 22500a7

Browse files
author
XIE Long
authored
fix if structure contains slice inside, but the items in the internal slice are not in the same order give wrong result (#82)
1 parent db3f716 commit 22500a7

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

diff_slice.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (d *Differ) diffSliceGeneric(path []string, a, b reflect.Value) error {
3737
for i := 0; i < a.Len(); i++ {
3838
ae := a.Index(i)
3939

40-
if (d.SliceOrdering && !hasAtSameIndex(b, ae, i)) || (!d.SliceOrdering && !slice.has(b, ae)) {
40+
if (d.SliceOrdering && !hasAtSameIndex(b, ae, i)) || (!d.SliceOrdering && !slice.has(b, ae, d)) {
4141
missing.addA(i, &ae)
4242
}
4343
}
@@ -46,7 +46,7 @@ func (d *Differ) diffSliceGeneric(path []string, a, b reflect.Value) error {
4646
for i := 0; i < b.Len(); i++ {
4747
be := b.Index(i)
4848

49-
if (d.SliceOrdering && !hasAtSameIndex(a, be, i)) || (!d.SliceOrdering && !slice.has(a, be)) {
49+
if (d.SliceOrdering && !hasAtSameIndex(a, be, i)) || (!d.SliceOrdering && !slice.has(a, be, d)) {
5050
missing.addB(i, &be)
5151
}
5252
}
@@ -88,7 +88,7 @@ func (d *Differ) diffSliceComparative(path []string, a, b reflect.Value) error {
8888
// keeps track of elements that have already been matched, to stop duplicate matches from occurring
8989
type sliceTracker []bool
9090

91-
func (st *sliceTracker) has(s, v reflect.Value) bool {
91+
func (st *sliceTracker) has(s, v reflect.Value, d *Differ) bool {
9292
if len(*st) != s.Len() {
9393
(*st) = make([]bool, s.Len())
9494
}
@@ -100,7 +100,17 @@ func (st *sliceTracker) has(s, v reflect.Value) bool {
100100
}
101101

102102
x := s.Index(i)
103-
if reflect.DeepEqual(exportInterface(x), exportInterface(v)) {
103+
104+
var nd Differ
105+
nd.Filter = d.Filter
106+
nd.customValueDiffers = d.customValueDiffers
107+
108+
err := nd.diff([]string{}, x, v, nil)
109+
if err != nil {
110+
continue
111+
}
112+
113+
if len(nd.cl) == 0 {
104114
(*st)[i] = true
105115
return true
106116
}

diff_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,13 @@ func TestDiff(t *testing.T) {
610610
},
611611
nil,
612612
},
613+
{
614+
"slice-of-struct-with-slice",
615+
[]tnstruct{{[]tmstruct{struct1, struct2}}, {[]tmstruct{struct2, struct2}}},
616+
[]tnstruct{{[]tmstruct{struct2, struct2}}, {[]tmstruct{struct2, struct1}}},
617+
diff.Changelog{},
618+
nil,
619+
},
613620
}
614621

615622
for _, tc := range cases {

0 commit comments

Comments
 (0)