-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.go
296 lines (246 loc) · 6.48 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
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
package semverlint
import "go/types"
// Diff computes the difference between two given public APIs.
func Diff(current, prev API) APIChanges {
var changes APIChanges
currentPkgs := packagesIndex(current)
prevPkgs := packagesIndex(prev)
var seen = make(map[string]struct{})
for path, p1 := range prevPkgs {
seen[path] = struct{}{}
p2, ok := currentPkgs[path]
if !ok {
changes = append(changes, NewPackageChanges(
p1.Name, p1.Path,
NewDeclChange(p1.Name, PackageType, Removed{}),
))
continue
}
changes = append(changes, packageDiff(p2, p1))
}
// Add the packages that were not present as new.
for path, p := range currentPkgs {
// Skip packages we've already seen.
if _, ok := seen[path]; !ok {
changes = append(changes, NewPackageChanges(
p.Name, p.Path,
NewDeclChange(p.Name, PackageType, Added{}),
))
}
}
return changes
}
func packageDiff(prev, current Package) PackageChanges {
var changes []Change
changes = append(changes, constsDiff(prev.Consts, current.Consts)...)
changes = append(changes, varsDiff(prev.Vars, current.Vars)...)
changes = append(changes, funcsDiff(prev.Funcs, current.Funcs)...)
changes = append(changes, structsDiff(prev.Structs, current.Structs)...)
changes = append(changes, interfacesDiff(prev.Interfaces, current.Interfaces)...)
changes = append(changes, typesDiff(prev.Types, current.Types)...)
return PackageChanges{
Path: current.Path,
Name: current.Name,
Changes: changes,
}
}
func constsDiff(prev, current []Const) []Change {
var changes []Change
currentConsts := constsIndex(current)
prevConsts := constsIndex(prev)
var seen = make(map[string]struct{})
for name, v := range prevConsts {
seen[name] = struct{}{}
v2, ok := currentConsts[name]
if !ok {
changes = append(changes, NewDeclChange(name, ConstType, Removed{}))
}
if !typesEqual(v.Type, v2.Type) {
changes = append(changes, NewDeclChange(name, ConstType, TypeChanged{
From: v.Type,
To: v2.Type,
}))
}
if v.Value != v2.Value {
changes = append(changes, NewDeclChange(name, ConstType, ValueChanged{
From: v.Value,
To: v2.Value,
}))
}
}
for name := range currentConsts {
if _, ok := seen[name]; !ok {
changes = append(changes, NewDeclChange(name, ConstType, Added{}))
}
}
return changes
}
func varsDiff(prev, current []Var) []Change {
var changes []Change
currentVars := varsIndex(current)
prevVars := varsIndex(prev)
var seen = make(map[string]struct{})
for name, v := range prevVars {
seen[name] = struct{}{}
v2, ok := currentVars[name]
if !ok {
changes = append(changes, NewDeclChange(name, VarType, Removed{}))
} else if !typesEqual(v.Type, v2.Type) {
changes = append(changes, NewDeclChange(name, VarType, TypeChanged{
From: v.Type,
To: v2.Type,
}))
}
}
for name := range currentVars {
if _, ok := seen[name]; !ok {
changes = append(changes, NewDeclChange(name, VarType, Added{}))
}
}
return changes
}
func funcsDiff(prev, current []Func) []Change {
var changes []Change
currentFuncs := funcsIndex(current)
prevFuncs := funcsIndex(prev)
var seen = make(map[string]struct{})
for name, v := range prevFuncs {
seen[name] = struct{}{}
v2, ok := currentFuncs[name]
if !ok {
changes = append(changes, NewDeclChange(name, FuncType, Removed{}))
}
_ = v
_ = v2
// TODO: check args
// TODO: check returns
}
for name := range currentFuncs {
if _, ok := seen[name]; !ok {
changes = append(changes, NewDeclChange(name, FuncType, Added{}))
}
}
return changes
}
func structsDiff(prev, current []Struct) []Change {
var changes []Change
currentStructs := structsIndex(current)
prevStructs := structsIndex(prev)
var seen = make(map[string]struct{})
for name, v := range prevStructs {
seen[name] = struct{}{}
v2, ok := currentStructs[name]
if !ok {
changes = append(changes, NewDeclChange(name, StructType, Removed{}))
}
_ = v
_ = v2
// TODO: check fields
// TODO: check methods
}
for name := range currentStructs {
if _, ok := seen[name]; !ok {
changes = append(changes, NewDeclChange(name, StructType, Added{}))
}
}
return changes
}
func interfacesDiff(prev, current []Interface) []Change {
var changes []Change
currentInterfaces := interfacesIndex(current)
prevInterfaces := interfacesIndex(prev)
var seen = make(map[string]struct{})
for name, v := range prevInterfaces {
seen[name] = struct{}{}
v2, ok := currentInterfaces[name]
if !ok {
changes = append(changes, NewDeclChange(name, InterfaceType, Removed{}))
}
_ = v
_ = v2
// TODO: check methods
}
for name := range currentInterfaces {
if _, ok := seen[name]; !ok {
changes = append(changes, NewDeclChange(name, InterfaceType, Added{}))
}
}
return changes
}
func typesDiff(prev, current []TypeDef) []Change {
var changes []Change
currentTypes := typesIndex(current)
prevTypes := typesIndex(prev)
var seen = make(map[string]struct{})
for name, v := range prevTypes {
seen[name] = struct{}{}
v2, ok := currentTypes[name]
if !ok {
changes = append(changes, NewDeclChange(name, TypeDefType, Removed{}))
}
if !typesEqual(v.Type, v2.Type) {
changes = append(changes, NewDeclChange(name, TypeDefType, TypeChanged{
From: v.Type,
To: v2.Type,
}))
}
}
for name := range currentTypes {
if _, ok := seen[name]; !ok {
changes = append(changes, NewDeclChange(name, TypeDefType, Added{}))
}
}
return changes
}
func packagesIndex(a API) map[string]Package {
var result = make(map[string]Package)
for _, p := range a {
result[p.Path] = p
}
return result
}
func constsIndex(xs []Const) map[string]Const {
var result = make(map[string]Const)
for _, x := range xs {
result[x.Name] = x
}
return result
}
func varsIndex(xs []Var) map[string]Var {
var result = make(map[string]Var)
for _, x := range xs {
result[x.Name] = x
}
return result
}
func funcsIndex(xs []Func) map[string]Func {
var result = make(map[string]Func)
for _, x := range xs {
result[x.Name] = x
}
return result
}
func interfacesIndex(xs []Interface) map[string]Interface {
var result = make(map[string]Interface)
for _, x := range xs {
result[x.Name] = x
}
return result
}
func structsIndex(xs []Struct) map[string]Struct {
var result = make(map[string]Struct)
for _, x := range xs {
result[x.Name] = x
}
return result
}
func typesIndex(xs []TypeDef) map[string]TypeDef {
var result = make(map[string]TypeDef)
for _, x := range xs {
result[x.Name] = x
}
return result
}
func typesEqual(a, b types.Type) bool {
return false
}