@@ -3,15 +3,17 @@ package diff
3
3
import (
4
4
"fmt"
5
5
"reflect"
6
+ "strconv"
6
7
"strings"
7
8
8
9
myersdiff "github.com/mb0/diff"
9
10
)
10
11
11
12
type slice struct {
12
- diffs []Differ
13
- lhs interface {}
14
- rhs interface {}
13
+ diffs []Differ
14
+ indices []int
15
+ lhs interface {}
16
+ rhs interface {}
15
17
}
16
18
17
19
type sliceMissing struct {
@@ -40,39 +42,45 @@ func (d *diffData) Equal(i, j int) bool {
40
42
return diff .Diff () == Identical
41
43
}
42
44
43
- func myersToDiff (conf config , lhs , rhs reflect.Value , changes []myersdiff.Change ) []Differ {
45
+ func myersToDiff (conf config , lhs , rhs reflect.Value , changes []myersdiff.Change ) ( []Differ , [] int ) {
44
46
res := []Differ {}
47
+ indices := []int {}
45
48
46
49
lhsIdx := 0
47
50
rhsIdx := 0
48
51
for _ , c := range changes {
49
52
for i := 0 ; lhsIdx + i < c .A ; i ++ {
50
53
diff , _ := diff (conf , lhs .Index (lhsIdx + i ).Interface (), rhs .Index (rhsIdx + i ).Interface (), & visited {})
51
54
res = append (res , diff )
55
+ indices = append (indices , lhsIdx + i )
52
56
}
53
57
lhsIdx = c .A
54
58
rhsIdx = c .B
55
59
for d := 0 ; d < c .Del ; d ++ {
56
60
res = append (res , sliceMissing {lhs .Index (lhsIdx + d ).Interface ()})
61
+ indices = append (indices , lhsIdx + d )
57
62
}
58
63
lhsIdx += c .Del
59
64
for i := 0 ; i < c .Ins ; i ++ {
60
65
res = append (res , sliceExcess {rhs .Index (rhsIdx + i ).Interface ()})
66
+ indices = append (indices , rhsIdx + i )
61
67
}
62
68
rhsIdx += c .Ins
63
69
}
64
70
65
71
for lhsIdx < lhs .Len () && rhsIdx < rhs .Len () {
66
72
diff , _ := diff (conf , lhs .Index (lhsIdx ).Interface (), rhs .Index (rhsIdx ).Interface (), & visited {})
67
73
res = append (res , diff )
74
+ indices = append (indices , lhsIdx )
68
75
lhsIdx ++
69
76
rhsIdx ++
70
77
}
71
- return res
78
+ return res , indices
72
79
}
73
80
74
81
func newMyersSlice (c config , lhs , rhs interface {}, visited * visited ) (Differ , error ) {
75
82
var diffs []Differ
83
+ var indices []int
76
84
77
85
lhsVal := reflect .ValueOf (lhs )
78
86
rhsVal := reflect .ValueOf (rhs )
@@ -91,25 +99,28 @@ func newMyersSlice(c config, lhs, rhs interface{}, visited *visited) (Differ, er
91
99
}
92
100
myers := myersdiff .Diff (lhsVal .Len (), rhsVal .Len (), & dData )
93
101
94
- diffs = myersToDiff (c , lhsVal , rhsVal , myers )
102
+ diffs , indices = myersToDiff (c , lhsVal , rhsVal , myers )
95
103
if dData .lastError != nil {
96
104
return slice {
97
- lhs : lhs ,
98
- rhs : rhs ,
99
- diffs : diffs ,
105
+ lhs : lhs ,
106
+ rhs : rhs ,
107
+ diffs : diffs ,
108
+ indices : indices ,
100
109
}, dData .lastError
101
110
}
102
111
}
103
112
104
113
return slice {
105
- lhs : lhs ,
106
- rhs : rhs ,
107
- diffs : diffs ,
114
+ lhs : lhs ,
115
+ rhs : rhs ,
116
+ diffs : diffs ,
117
+ indices : indices ,
108
118
}, nil
109
119
}
110
120
111
121
func newSlice (c config , lhs , rhs interface {}, visited * visited ) (Differ , error ) {
112
122
var diffs []Differ
123
+ var indices []int
113
124
114
125
lhsVal := reflect .ValueOf (lhs )
115
126
rhsVal := reflect .ValueOf (rhs )
@@ -126,15 +137,17 @@ func newSlice(c config, lhs, rhs interface{}, visited *visited) (Differ, error)
126
137
}
127
138
128
139
for i := 0 ; i < nElems ; i ++ {
140
+ indices = append (indices , i )
129
141
if i < lhsVal .Len () && i < rhsVal .Len () {
130
142
diff , err := diff (c , lhsVal .Index (i ).Interface (), rhsVal .Index (i ).Interface (), visited )
131
143
diffs = append (diffs , diff )
132
144
133
145
if err != nil {
134
146
return slice {
135
- lhs : lhs ,
136
- rhs : rhs ,
137
- diffs : diffs ,
147
+ lhs : lhs ,
148
+ rhs : rhs ,
149
+ diffs : diffs ,
150
+ indices : indices ,
138
151
}, err
139
152
}
140
153
continue
@@ -148,9 +161,10 @@ func newSlice(c config, lhs, rhs interface{}, visited *visited) (Differ, error)
148
161
}
149
162
150
163
return slice {
151
- lhs : lhs ,
152
- rhs : rhs ,
153
- diffs : diffs ,
164
+ lhs : lhs ,
165
+ rhs : rhs ,
166
+ diffs : diffs ,
167
+ indices : indices ,
154
168
}, nil
155
169
}
156
170
@@ -247,7 +261,7 @@ func (s slice) openString(key, prefix string, conf Output) string {
247
261
248
262
func (s slice ) Walk (path string , fn WalkFn ) error {
249
263
for i , diff := range s .diffs {
250
- d , err := walk (s , diff , path + "[]" , fn )
264
+ d , err := walk (s , diff , path + "[" + strconv . Itoa ( s . lhsIndex ( i )) + " ]" , fn )
251
265
if err != nil {
252
266
return err
253
267
}
@@ -259,6 +273,10 @@ func (s slice) Walk(path string, fn WalkFn) error {
259
273
return nil
260
274
}
261
275
276
+ func (s slice ) lhsIndex (i int ) int {
277
+ return s .indices [i ]
278
+ }
279
+
262
280
func (s slice ) LHS () interface {} {
263
281
return s .lhs
264
282
}
0 commit comments