-
Notifications
You must be signed in to change notification settings - Fork 152
/
diff.go
138 lines (117 loc) · 2.62 KB
/
diff.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
package table
import (
"fmt"
"strings"
"github.com/andreyvit/diff"
"github.com/influxdata/flux"
)
// Diff will perform a diff between two table iterators.
// This will sort the tables within the table iterators and produce
// a diff of the full output.
func Diff(want, got flux.TableIterator, opts ...DiffOption) string {
if want == nil {
want = Iterator{}
}
var wantS string
if wantT, err := Sort(want); err != nil {
wantS = fmt.Sprintf("table error: %s\n", err)
} else {
var sb strings.Builder
if err := wantT.Do(func(table flux.Table) error {
sb.WriteString(Stringify(table))
return nil
}); err != nil {
_, _ = fmt.Fprintf(&sb, "table error: %s\n", err)
}
wantS = sb.String()
}
if got == nil {
got = Iterator{}
}
var gotS string
if gotT, err := Sort(got); err != nil {
gotS = fmt.Sprintf("table error: %s\n", err)
} else {
var sb strings.Builder
if err := gotT.Do(func(table flux.Table) error {
sb.WriteString(Stringify(table))
return nil
}); err != nil {
_, _ = fmt.Fprintf(&sb, "table error: %s\n", err)
}
gotS = sb.String()
}
differ := newDiffer(opts...)
return differ.diff(wantS, gotS)
}
type differ struct {
ctx *[2]int
}
func newDiffer(opts ...DiffOption) (d differ) {
for _, opt := range diffDefaultOptions {
opt.apply(&d)
}
for _, opt := range opts {
opt.apply(&d)
}
return d
}
func (d differ) diff(want, got string) string {
lines := diff.LineDiffAsLines(want, got)
if d.ctx == nil {
return strings.Join(lines, "\n")
}
difflines := make([]string, 0, len(lines))
OUTER:
for {
for i := 0; i < len(lines); i++ {
if lines[i][0] == ' ' {
continue
}
// This is the start of a diff section. Store this location.
start := i - (*d.ctx)[0]
if start < 0 {
start = 0
}
// Find the end of this section.
for ; i < len(lines); i++ {
if lines[i][0] == ' ' {
break
}
}
// Look n points in the future and, if they are
// not part of a diff or don't overrun the number
// of lines, include them.
stop := i
for n := (*d.ctx)[1]; n > 0; n-- {
if stop+1 >= len(lines) || lines[stop+1][0] != ' ' {
break
}
stop++
}
difflines = append(difflines, lines[start:stop]...)
lines = lines[stop:]
continue OUTER
}
return strings.Join(difflines, "\n")
}
}
type DiffOption interface {
apply(*differ)
}
type diffOptionFn func(d *differ)
func (opt diffOptionFn) apply(d *differ) {
opt(d)
}
var diffDefaultOptions = []DiffOption{
DiffContext(3),
}
func DiffContext(n int) DiffOption {
return diffOptionFn(func(d *differ) {
if n < 0 {
d.ctx = nil
}
ctx := [2]int{n, n}
d.ctx = &ctx
})
}