/
spec_diff_visitor.go
534 lines (421 loc) · 18.7 KB
/
spec_diff_visitor.go
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
package http_rest_diff
import (
"fmt"
"reflect"
"sort"
pb "github.com/akitasoftware/akita-ir/go/api_spec"
"github.com/akitasoftware/akita-libs/spec_util"
. "github.com/akitasoftware/akita-libs/visitors"
"github.com/akitasoftware/akita-libs/visitors/go_ast_pair"
"github.com/akitasoftware/akita-libs/visitors/http_rest"
"golang.org/x/exp/maps"
)
// A SpecPairVisitor with hooks for processing each difference found between
// two IR trees. A node is considered changed if a difference can be observed
// at that level of the IR. For example, HTTPAuth nodes with different Types
// are considered changed, but their parents might not necessarily be
// considered changed.
//
// Go lacks virtual functions, so all functions here take the visitor itself as
// an argument, and call functions on that instance.
type SpecDiffVisitor interface {
http_rest.SpecPairVisitor
EnterAddedOrRemovedData(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Data) Cont
LeaveAddedOrRemovedData(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Data, cont Cont) Cont
EnterAddedOrRemovedList(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.List) Cont
LeaveAddedOrRemovedList(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.List, cont Cont) Cont
EnterAddedOrRemovedOneOf(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.OneOf) Cont
EnterChangedOneOf(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.OneOf) Cont
LeaveAddedOrRemovedOneOf(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.OneOf, cont Cont) Cont
LeaveChangedOneOf(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.OneOf, cont Cont) Cont
EnterAddedOrRemovedOptional(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Optional) Cont
LeaveAddedOrRemovedOptional(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Optional, cont Cont) Cont
EnterAddedOrRemovedPrimitive(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Primitive) Cont
EnterChangedPrimitive(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Primitive) Cont
LeaveAddedOrRemovedPrimitive(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Primitive, cont Cont) Cont
LeaveChangedPrimitive(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Primitive, cont Cont) Cont
EnterAddedOrRemovedStruct(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Struct) Cont
LeaveAddedOrRemovedStruct(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Struct, cont Cont) Cont
}
// A SpecDiffVisitor with convenience functions for entering and leaving nodes
// with diffs.
type DefaultSpecDiffVisitor interface {
SpecDiffVisitor
EnterDiff(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right interface{}) Cont
LeaveDiff(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right interface{}, cont Cont) Cont
// Delegates to EnterDiff by default.
EnterAddedOrRemovedNode(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right interface{}) Cont
// Delegates to EnterDiff by default.
EnterChangedNode(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right interface{}) Cont
// Delegates to LeaveDiff by default.
LeaveAddedOrRemovedNode(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right interface{}, cont Cont) Cont
// Delegates to LeaveDiff by default.
LeaveChangedNode(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right interface{}, cont Cont) Cont
}
// A SpecDiffVisitor implementation. This does not traverse into the children
// of nodes that were added, removed, or changed.
type DefaultSpecDiffVisitorImpl struct {
http_rest.DefaultSpecPairVisitorImpl
}
var _ DefaultSpecDiffVisitor = (*DefaultSpecDiffVisitorImpl)(nil)
// == Default implementations =================================================
func (*DefaultSpecDiffVisitorImpl) EnterDiff(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right interface{}) Cont {
return SkipChildren
}
func (*DefaultSpecDiffVisitorImpl) LeaveDiff(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right interface{}, cont Cont) Cont {
return cont
}
// Delegates to EnterDiff.
func (*DefaultSpecDiffVisitorImpl) EnterAddedOrRemovedNode(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right interface{}) Cont {
v := self.(DefaultSpecDiffVisitor)
return v.EnterDiff(self, ctx, left, right)
}
// Delegates to EnterDiff.
func (*DefaultSpecDiffVisitorImpl) EnterChangedNode(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right interface{}) Cont {
v := self.(DefaultSpecDiffVisitor)
return v.EnterDiff(self, ctx, left, right)
}
// Delegates to LeaveDiff.
func (*DefaultSpecDiffVisitorImpl) LeaveAddedOrRemovedNode(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right interface{}, cont Cont) Cont {
v := self.(DefaultSpecDiffVisitor)
return v.LeaveDiff(self, ctx, left, right, cont)
}
// Delegates to LeaveDiff.
func (*DefaultSpecDiffVisitorImpl) LeaveChangedNode(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right interface{}, cont Cont) Cont {
v := self.(DefaultSpecDiffVisitor)
return v.LeaveDiff(self, ctx, left, right, cont)
}
// == Data ====================================================================
func (*DefaultSpecDiffVisitorImpl) EnterData(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Data) Cont {
v := self.(DefaultSpecDiffVisitor)
if left == nil && right == nil {
return SkipChildren
}
if left == nil || right == nil {
return v.EnterAddedOrRemovedData(self, ctx, left, right)
}
return Continue
}
func (*DefaultSpecDiffVisitorImpl) VisitDataChildren(self interface{}, ctx http_rest.SpecPairVisitorContext, vm PairVisitorManager, left, right *pb.Data) Cont {
// Only visit the value.
childCtx := ctx.EnterStructs(left, "Value", right, "Value")
return go_ast_pair.ApplyWithContext(vm, childCtx, left.Value, right.Value)
}
func (*DefaultSpecDiffVisitorImpl) LeaveData(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Data, cont Cont) Cont {
v := self.(DefaultSpecDiffVisitor)
if left == nil && right == nil {
return cont
}
if left == nil || right == nil {
return v.LeaveAddedOrRemovedData(self, ctx, left, right, cont)
}
return cont
}
// Delegates to EnterAddedOrRemovedNode.
func (*DefaultSpecDiffVisitorImpl) EnterAddedOrRemovedData(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Data) Cont {
v := self.(DefaultSpecDiffVisitor)
return v.EnterAddedOrRemovedNode(self, ctx, left, right)
}
// Delegates to LeaveAddedOrRemovedNode.
func (*DefaultSpecDiffVisitorImpl) LeaveAddedOrRemovedData(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Data, cont Cont) Cont {
v := self.(DefaultSpecDiffVisitor)
return v.LeaveAddedOrRemovedNode(self, ctx, left, right, cont)
}
// == List ====================================================================
func (*DefaultSpecDiffVisitorImpl) EnterLists(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.List) Cont {
v := self.(DefaultSpecDiffVisitor)
if left == nil && right == nil {
return SkipChildren
}
if left == nil || right == nil {
return v.EnterAddedOrRemovedList(self, ctx, left, right)
}
return Continue
}
func (*DefaultSpecDiffVisitorImpl) LeaveLists(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.List, cont Cont) Cont {
v := self.(DefaultSpecDiffVisitor)
if left == nil && right == nil {
return cont
}
if left == nil || right == nil {
return v.LeaveAddedOrRemovedList(self, ctx, left, right, cont)
}
return cont
}
// Delegates to EnterAddedOrRemovedNode.
func (*DefaultSpecDiffVisitorImpl) EnterAddedOrRemovedList(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.List) Cont {
v := self.(DefaultSpecDiffVisitor)
return v.EnterAddedOrRemovedNode(self, ctx, left, right)
}
// Delegates to LeaveAddedOrRemovedNode.
func (*DefaultSpecDiffVisitorImpl) LeaveAddedOrRemovedList(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.List, cont Cont) Cont {
v := self.(DefaultSpecDiffVisitor)
return v.LeaveAddedOrRemovedNode(self, ctx, left, right, cont)
}
// == OneOf ===================================================================
func (*DefaultSpecDiffVisitorImpl) EnterOneOfs(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.OneOf) Cont {
v := self.(DefaultSpecDiffVisitor)
if left == nil && right == nil {
return SkipChildren
}
if left == nil || right == nil {
return v.EnterAddedOrRemovedOneOf(self, ctx, left, right)
}
if oneOfsDiffer(left, right) {
return v.EnterChangedOneOf(self, ctx, left, right)
}
return Continue
}
func oneOfsDiffer(left, right *pb.OneOf) bool {
if left.PotentialConflict != right.PotentialConflict {
return true
}
if len(left.Options) != len(right.Options) {
return true
}
// Manually pair up the options to see if we can get things to match.
rightOptions := maps.Clone(right.Options)
OUTER:
for _, leftOption := range left.Options {
for rightKey, rightOption := range rightOptions {
if IsSameData(leftOption, rightOption) {
delete(rightOptions, rightKey)
continue OUTER
}
}
// No match found for leftOption.
return true
}
return false
}
func (*DefaultSpecDiffVisitorImpl) VisitOneOfChildren(self interface{}, ctx http_rest.SpecPairVisitorContext, vm PairVisitorManager, left, right *pb.OneOf) Cont {
// Override visitor behaviour for OneOf nodes by manually pairing up the
// options to see if we can get things to match.
childCtx := ctx.EnterStructs(left, "Options", right, "Options")
rightOptions := make(map[string]*pb.Data, len(right.Options))
for k, v := range right.Options {
rightOptions[k] = v
}
OUTER:
for leftKey, leftOption := range left.Options {
for rightKey, rightOption := range rightOptions {
if IsSameData(leftOption, rightOption) {
// Found a match.
delete(rightOptions, rightKey)
continue OUTER
}
}
// No match found for leftOption.
childCtx := childCtx.EnterMapValues(left.Options, leftKey, right.Options, nil)
switch keepGoing := go_ast_pair.ApplyWithContext(vm, childCtx, leftOption, go_ast_pair.ZeroOf(leftOption)); keepGoing {
case Abort:
return Abort
case Continue:
case SkipChildren:
panic("go_ast_pair.ApplyWithContext returned SkipChildren")
case Stop:
return Stop
default:
panic(fmt.Sprintf("Unknown Cont value: %d", keepGoing))
}
}
// Anything remaining in `rightOptions` has no match in `left`.
for rightKey, rightOption := range rightOptions {
childCtx := childCtx.EnterMapValues(left.Options, nil, right.Options, rightKey)
switch keepGoing := go_ast_pair.ApplyWithContext(vm, childCtx, go_ast_pair.ZeroOf(rightOption), rightOption); keepGoing {
case Abort:
return Abort
case Continue:
case SkipChildren:
panic("go_ast_pair.ApplyWithContext returned SkipChildren")
case Stop:
return Stop
default:
panic(fmt.Sprintf("Unknown Cont value: %d", keepGoing))
}
}
return Continue
}
func (*DefaultSpecDiffVisitorImpl) LeaveOneOfs(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.OneOf, cont Cont) Cont {
v := self.(DefaultSpecDiffVisitor)
if left == nil && right == nil {
return cont
}
if left == nil || right == nil {
return v.LeaveAddedOrRemovedOneOf(self, ctx, left, right, cont)
}
if oneOfsDiffer(left, right) {
return v.LeaveChangedOneOf(self, ctx, left, right, cont)
}
return cont
}
// Delegates to EnterAddedOrRemovedNode.
func (*DefaultSpecDiffVisitorImpl) EnterAddedOrRemovedOneOf(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.OneOf) Cont {
v := self.(DefaultSpecDiffVisitor)
return v.EnterAddedOrRemovedNode(self, ctx, left, right)
}
// Delegates to EnterChangedNode.
func (*DefaultSpecDiffVisitorImpl) EnterChangedOneOf(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.OneOf) Cont {
v := self.(DefaultSpecDiffVisitor)
return v.EnterChangedNode(self, ctx, left, right)
}
// Delegates to LeaveAddedOrRemovedNode.
func (*DefaultSpecDiffVisitorImpl) LeaveAddedOrRemovedOneOf(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.OneOf, cont Cont) Cont {
v := self.(DefaultSpecDiffVisitor)
return v.LeaveAddedOrRemovedNode(self, ctx, left, right, cont)
}
// Delegates to LeaveChangedNode.
func (*DefaultSpecDiffVisitorImpl) LeaveChangedOneOf(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.OneOf, cont Cont) Cont {
v := self.(DefaultSpecDiffVisitor)
return v.LeaveChangedNode(self, ctx, left, right, cont)
}
// == Optional ================================================================
func (*DefaultSpecDiffVisitorImpl) EnterOptionals(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Optional) Cont {
v := self.(DefaultSpecDiffVisitor)
if left == nil && right == nil {
return SkipChildren
}
if left == nil || right == nil {
return v.EnterAddedOrRemovedOptional(self, ctx, left, right)
}
return Continue
}
func (*DefaultSpecDiffVisitorImpl) LeaveOptionals(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Optional, cont Cont) Cont {
v := self.(DefaultSpecDiffVisitor)
if left == nil && right == nil {
return cont
}
if left == nil || right == nil {
return v.LeaveAddedOrRemovedOptional(self, ctx, left, right, cont)
}
return cont
}
// Delegates to EnterAddedOrRemovedNode.
func (*DefaultSpecDiffVisitorImpl) EnterAddedOrRemovedOptional(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Optional) Cont {
v := self.(DefaultSpecDiffVisitor)
return v.EnterAddedOrRemovedNode(self, ctx, left, right)
}
// Delegates to LeaveAddedOrRemovedNode.
func (*DefaultSpecDiffVisitorImpl) LeaveAddedOrRemovedOptional(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Optional, cont Cont) Cont {
v := self.(DefaultSpecDiffVisitor)
return v.LeaveAddedOrRemovedNode(self, ctx, left, right, cont)
}
// == Primitive ===============================================================
func (*DefaultSpecDiffVisitorImpl) EnterPrimitives(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Primitive) Cont {
v := self.(DefaultSpecDiffVisitor)
if left == nil && right == nil {
return SkipChildren
}
if left == nil || right == nil {
return v.EnterAddedOrRemovedPrimitive(self, ctx, left, right)
}
if primitivesDiffer(left, right) {
return v.EnterChangedPrimitive(self, ctx, left, right)
}
return SkipChildren
}
// Determines whether two primitives are different.
func primitivesDiffer(p1, p2 *pb.Primitive) bool {
// Compare types.
type1 := spec_util.TypeOfPrimitive(p1)
type2 := spec_util.TypeOfPrimitive(p2)
if type1 != type2 {
return true
}
// Compare format kinds.
if p1.FormatKind != p2.FormatKind {
return true
}
// Compare formats.
formats1 := formatsOfPrimitive(p1)
formats2 := formatsOfPrimitive(p2)
if !reflect.DeepEqual(formats1, formats2) {
return true
}
return false
}
// Extracts a list of formats from a primitive.
func formatsOfPrimitive(p *pb.Primitive) []string {
result := make([]string, 0, len(p.Formats))
for format, present := range p.Formats {
if present {
result = append(result, format)
}
}
sort.Strings(result)
return result
}
func (*DefaultSpecDiffVisitorImpl) LeavePrimitives(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Primitive, cont Cont) Cont {
v := self.(DefaultSpecDiffVisitor)
if left == nil && right == nil {
return cont
}
if left == nil || right == nil {
return v.LeaveAddedOrRemovedPrimitive(self, ctx, left, right, cont)
}
if primitivesDiffer(left, right) {
return v.LeaveChangedPrimitive(self, ctx, left, right, cont)
}
return cont
}
// Delegates to EnterAddedOrRemovedNode.
func (*DefaultSpecDiffVisitorImpl) EnterAddedOrRemovedPrimitive(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Primitive) Cont {
v := self.(DefaultSpecDiffVisitor)
return v.EnterAddedOrRemovedNode(self, ctx, left, right)
}
// Delegates to EnterChangedNode.
func (*DefaultSpecDiffVisitorImpl) EnterChangedPrimitive(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Primitive) Cont {
v := self.(DefaultSpecDiffVisitor)
return v.EnterChangedNode(self, ctx, left, right)
}
// Delegates to LeaveAddedOrRemovedNode.
func (*DefaultSpecDiffVisitorImpl) LeaveAddedOrRemovedPrimitive(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Primitive, cont Cont) Cont {
v := self.(DefaultSpecDiffVisitor)
return v.LeaveAddedOrRemovedNode(self, ctx, left, right, cont)
}
// Delegates to LeaveChangedNode.
func (*DefaultSpecDiffVisitorImpl) LeaveChangedPrimitive(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Primitive, cont Cont) Cont {
v := self.(DefaultSpecDiffVisitor)
return v.LeaveChangedNode(self, ctx, left, right, cont)
}
// == Struct ==================================================================
func (*DefaultSpecDiffVisitorImpl) EnterStructs(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Struct) Cont {
v := self.(DefaultSpecDiffVisitor)
if left == nil && right == nil {
return SkipChildren
}
if left == nil || right == nil {
return v.EnterAddedOrRemovedStruct(self, ctx, left, right)
}
return Continue
}
func (*DefaultSpecDiffVisitorImpl) LeaveStructs(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Struct, cont Cont) Cont {
v := self.(DefaultSpecDiffVisitor)
if left == nil && right == nil {
return cont
}
if left == nil || right == nil {
return v.LeaveAddedOrRemovedStruct(self, ctx, left, right, cont)
}
return cont
}
// Delegates to EnterAddedOrRemovedNode.
func (*DefaultSpecDiffVisitorImpl) EnterAddedOrRemovedStruct(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Struct) Cont {
v := self.(DefaultSpecDiffVisitor)
return v.EnterAddedOrRemovedNode(self, ctx, left, right)
}
// Delegates to LeaveAddedOrRemovedNode.
func (*DefaultSpecDiffVisitorImpl) LeaveAddedOrRemovedStruct(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right *pb.Struct, cont Cont) Cont {
v := self.(DefaultSpecDiffVisitor)
return v.LeaveAddedOrRemovedNode(self, ctx, left, right, cont)
}
// Delegates to EnterDiff.
func (*DefaultSpecDiffVisitorImpl) EnterDifferentTypes(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right interface{}) Cont {
v := self.(DefaultSpecDiffVisitor)
return v.EnterChangedNode(self, ctx, left, right)
}
// Delegates to LeaveDiff.
func (*DefaultSpecDiffVisitorImpl) LeaveDifferentTypes(self interface{}, ctx http_rest.SpecPairVisitorContext, left, right interface{}, cont Cont) Cont {
v := self.(DefaultSpecDiffVisitor)
return v.LeaveChangedNode(self, ctx, left, right, cont)
}