-
Notifications
You must be signed in to change notification settings - Fork 431
Expand file tree
/
Copy pathdiff.go
More file actions
294 lines (238 loc) · 6.53 KB
/
diff.go
File metadata and controls
294 lines (238 loc) · 6.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright 2020 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package diff
import (
"fmt"
"reflect"
"regexp"
"strings"
"github.com/google/go-cmp/cmp"
"go.uber.org/zap"
)
type Diffeable interface {
Diff(right any, options ...Option)
}
type Option interface {
apply(o *options)
}
type optionFunc func(o *options)
func (f optionFunc) apply(opts *options) {
f(opts)
}
func CmpOption(cmpOption cmp.Option) Option {
return optionFunc(func(opts *options) { opts.cmpOptions = append(opts.cmpOptions, cmpOption) })
}
func OnEvent(callback func(Event)) Option {
return optionFunc(func(opts *options) { opts.onEvent = callback })
}
type options struct {
cmpOptions []cmp.Option
onEvent func(Event)
}
type Kind uint8
const (
KindAdded Kind = iota
KindChanged
KindRemoved
)
func (k Kind) String() string {
switch k {
case KindAdded:
return "added"
case KindChanged:
return "changed"
case KindRemoved:
return "removed"
}
return "unknown"
}
type Path cmp.Path
func (pa Path) SliceIndex() (int, bool) {
last := pa[len(pa)-1]
if slcIdx, ok := last.(cmp.SliceIndex); ok {
xkey, ykey := slcIdx.SplitKeys()
switch {
case xkey == ykey:
return xkey, true
case ykey == -1:
// [5->?] means "I don't know where X[5] went"
return xkey, true
case xkey == -1:
// [?->3] means "I don't know where Y[3] came from"
return ykey, true
default:
// [5->3] means "X[5] moved to Y[3]"
return ykey, true
}
}
return 0, false
}
func (pa Path) String() string {
if len(pa) == 1 {
return ""
}
return strings.TrimPrefix(cmp.Path(pa[1:]).GoString(), ".")
}
type Event struct {
Path Path
Kind Kind
Old reflect.Value
New reflect.Value
}
// Match currently simply ensure that `pattern` parameter is the start of the path string
// which represents the direct access from top-level to struct.
func (p *Event) Match(pattern string) (match bool, matches []string) {
regexRaw := regexp.QuoteMeta(pattern)
regexRaw = strings.ReplaceAll("^"+regexRaw+"$", "#", `([0-9]+|.->[0-9]+|[0-9]+->.|[0-9]+->[0-9]+)`)
return p.RawMatch(regexRaw)
}
func (p *Event) RawMatch(rawPattern string) (match bool, matches []string) {
regex := regexp.MustCompile(rawPattern)
regexMatch := regex.FindAllStringSubmatch(p.Path.String(), 1)
if len(regexMatch) != 1 {
return false, nil
}
// For now we accept only array indices, will need to re-write logic if we ever need to check for keys also
subMatches := regexMatch[0][1:]
if len(subMatches) == 0 {
return true, nil
}
return true, subMatches
}
func (p *Event) AddedKind() bool {
return p.Kind == KindAdded
}
func (p *Event) ChangedKind() bool {
return p.Kind == KindChanged
}
func (p *Event) RemovedKind() bool {
return p.Kind == KindRemoved
}
// Element picks the element based on the Event's Kind, if it's removed, the element is the
// "old" value, if it's added or changed, the element is the "new" value.
func (p *Event) Element() reflect.Value {
if p.Kind == KindRemoved {
return p.Old
}
return p.New
}
func (p *Event) String() string {
path := ""
if len(p.Path) > 1 {
path = " @ " + p.Path.String()
}
return fmt.Sprintf("%s => %s (%s%s)", reflectValueToString(p.Old), reflectValueToString(p.New), p.Kind, path)
}
func reflectValueToString(value reflect.Value) string {
if !value.IsValid() {
return "<nil>"
}
if value.CanInterface() {
if reflectValueCanIsNil(value) && value.IsNil() {
return fmt.Sprintf("<nil> (%s)", value.Type())
}
v := value.Interface()
return fmt.Sprintf("%v (%T)", v, v)
}
return fmt.Sprintf("<type %T>", value.Type())
}
func reflectValueCanIsNil(value reflect.Value) bool {
switch value.Kind() {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.UnsafePointer, reflect.Interface, reflect.Slice:
return true
default:
return false
}
}
func Diff(left any, right any, opts ...Option) error {
options := options{}
for _, opt := range opts {
opt.apply(&options)
}
if options.onEvent == nil {
return fmt.Errorf("the option diff.OnEvent(...) must always be defined")
}
reporter := &diffReporter{notify: options.onEvent}
cmp.Equal(left, right, append(
[]cmp.Option{cmp.Reporter(reporter)},
options.cmpOptions...,
)...)
return nil
}
type diffReporter struct {
notify func(event Event)
path cmp.Path
diffs []string
}
func (r *diffReporter) PushStep(ps cmp.PathStep) {
if traceEnabled {
zlog.Debug("pushing path step", zap.Stringer("step", ps))
}
r.path = append(r.path, ps)
}
func (r *diffReporter) Report(rs cmp.Result) {
if !rs.Equal() {
lastStep := r.path.Last()
vLeft, vRight := lastStep.Values()
if !vLeft.IsValid() {
if traceEnabled {
zlog.Debug("added event", zap.Stringer("path", r.path))
}
// Left is not set but right is, we have added "right"
r.notify(Event{Kind: KindAdded, Path: Path(r.path), New: vRight})
return
}
if !vRight.IsValid() {
if traceEnabled {
zlog.Debug("removed event", zap.Stringer("path", r.path))
}
// Left is set but right is not, we have removed "left"
r.notify(Event{Kind: KindRemoved, Path: Path(r.path), Old: vLeft})
return
}
if isArrayPathStep(lastStep) {
// We might want to do this only on certain circumstances?
if traceEnabled {
zlog.Debug("array changed event, splitting in removed, added", zap.Stringer("path", r.path))
}
r.notify(Event{Kind: KindRemoved, Path: Path(r.path), Old: vLeft})
r.notify(Event{Kind: KindAdded, Path: Path(r.path), New: vRight})
return
}
if traceEnabled {
zlog.Debug("changed event", zap.Stringer("path", r.path))
}
r.notify(Event{Kind: KindChanged, Path: Path(r.path), Old: vLeft, New: vRight})
}
}
func (r *diffReporter) PopStep() {
if traceEnabled {
zlog.Debug("popping path step", zap.Stringer("step", r.path[len(r.path)-1]))
}
r.path = r.path[:len(r.path)-1]
}
func isArrayPathStep(step cmp.PathStep) bool {
_, ok := step.(cmp.SliceIndex)
return ok
}
func copyPath(path cmp.Path) Path {
if len(path) == 0 {
return Path(path)
}
out := make([]cmp.PathStep, len(path))
for i, step := range path {
out[i] = step
}
return Path(cmp.Path(out))
}