forked from Azure/azure-sdk-for-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
delta.go
345 lines (300 loc) · 8.84 KB
/
delta.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
package delta
import (
"sort"
"github.com/Azure/azure-sdk-for-go/tools/apidiff/exports"
)
// Content defines the set of exported constants, funcs, and structs.
type Content struct {
exports.Content
// contains the names of structs that are modified in whole (i.e. new/removed)
CompleteStructs []string `json:"newStructs,omitempty"`
}
// Count returns the count of items
func (c Content) Count() int {
return c.Content.Count() + len(c.CompleteStructs)
}
// NewContent returns an initialized Content object.
func NewContent() Content {
return Content{
Content: exports.NewContent(),
}
}
// GetModifiedStructs returns the subset, if any, of structs that are modified.
func (c Content) GetModifiedStructs() map[string]exports.Struct {
if len(c.CompleteStructs) == 0 {
return c.Structs
}
ms := map[string]exports.Struct{}
for k, v := range c.Structs {
if contains(c.CompleteStructs, k) {
continue
}
ms[k] = v
}
return ms
}
// returns true if sl contains x
func contains(sl []string, x string) bool {
for _, s := range sl {
if s == x {
return true
}
}
return false
}
// GetExports returns a exports.Content struct containing all exports in rhs that aren't in lhs.
// This includes any new fields added to structs or methods added to interfaces.
func GetExports(lhs, rhs exports.Content) Content {
nc := NewContent()
for n, v := range rhs.Consts {
if _, ok := lhs.Consts[n]; !ok {
nc.Consts[n] = v
}
}
for n, v := range rhs.Funcs {
if _, ok := lhs.Funcs[n]; !ok {
nc.Funcs[n] = v
}
}
for n, v := range rhs.Interfaces {
if _, ok := lhs.Interfaces[n]; !ok {
nc.Interfaces[n] = v
}
}
for n, v := range rhs.Structs {
if _, ok := lhs.Structs[n]; !ok {
nc.Structs[n] = v
nc.CompleteStructs = append(nc.CompleteStructs, n)
}
}
structFields := GetStructFields(lhs, rhs)
if len(structFields) > 0 {
for k, v := range structFields {
nc.Structs[k] = v
}
}
intMethods := GetInterfaceMethods(lhs, rhs)
if len(intMethods) > 0 {
for k, v := range intMethods {
nc.Interfaces[k] = v
}
}
sort.Strings(nc.CompleteStructs)
return nc
}
// GetStructFields returns structs common to lhs and rhs where structs in rhs contain
// fields not in lhs. Key is the struct type name, value contains the added content.
func GetStructFields(lhs, rhs exports.Content) map[string]exports.Struct {
nf := map[string]exports.Struct{}
for rhsKey, rhsVal := range rhs.Structs {
if lhsStruct, ok := lhs.Structs[rhsKey]; ok {
nc := exports.Struct{}
for _, rhsAnon := range rhsVal.AnonymousFields {
found := false
for _, lhsAnon := range lhsStruct.AnonymousFields {
if lhsAnon == rhsAnon {
found = true
break
}
}
if !found {
nc.AnonymousFields = append(nc.AnonymousFields, rhsAnon)
}
}
for fn, fv := range rhsVal.Fields {
if _, ok := lhsStruct.Fields[fn]; !ok {
if nc.Fields == nil {
nc.Fields = map[string]string{}
}
nc.Fields[fn] = fv
}
}
// only add it if there's new content
if len(nc.AnonymousFields) > 0 || len(nc.Fields) > 0 {
nf[rhsKey] = nc
}
}
}
return nf
}
// GetInterfaceMethods returns interfaces common to lhs and rhs where interfaces in rhs contain
// methods not in lhs. Key is the interface type name, value contains the added content.
func GetInterfaceMethods(lhs, rhs exports.Content) map[string]exports.Interface {
ni := map[string]exports.Interface{}
for rhsKey, rhsValue := range rhs.Interfaces {
if lhsInterface, ok := lhs.Interfaces[rhsKey]; ok {
nc := exports.Interface{}
for in, iv := range rhsValue.Methods {
if _, ok := lhsInterface.Methods[in]; !ok {
if nc.Methods == nil {
nc.Methods = map[string]exports.Func{}
}
nc.Methods[in] = iv
}
}
// only add it if there's new content
if len(nc.Methods) > 0 {
ni[rhsKey] = nc
}
}
}
return ni
}
// Signature contains the details of how a type signature changed (e.g. From:"int" To:"string").
type Signature struct {
// From contains the original signature.
From string `json:"from"`
// To contains the new signature.
To string `json:"to"`
}
// GetConstTypeChanges returns a collection of const where the type has changed.
// Key is the const name, value contains the type change information.
func GetConstTypeChanges(lhs, rhs exports.Content) map[string]Signature {
cc := map[string]Signature{}
for rhsKey, rhsValue := range rhs.Consts {
if _, ok := lhs.Consts[rhsKey]; !ok {
continue
}
if lhs.Consts[rhsKey].Type != rhsValue.Type {
cc[rhsKey] = Signature{
From: lhs.Consts[rhsKey].Type,
To: rhsValue.Type,
}
}
}
return cc
}
// None is the value used for functions with no parameters and/or no return values.
const None = "<none>"
// FuncSig contains the details of how a function's signature changed.
type FuncSig struct {
// Params contains the parameter signature changes, may be nil.
Params *Signature `json:"params,omitempty"`
// Returns contains the return signature changes, may be nil.
Returns *Signature `json:"returns,omitempty"`
}
func (fs FuncSig) isEmpty() bool {
return fs.Params == nil && fs.Returns == nil
}
// GetFuncSigChanges returns a collection of functions that contain signature changes (params and/or returns).
// Key is the function name, value contains the signature change information.
func GetFuncSigChanges(lhs, rhs exports.Content) map[string]FuncSig {
fsc := map[string]FuncSig{}
for rhsKey, rhsValue := range rhs.Funcs {
if _, ok := lhs.Funcs[rhsKey]; !ok {
continue
}
sig := FuncSig{}
if !safeStrCmp(lhs.Funcs[rhsKey].Params, rhsValue.Params) {
sig.Params = &Signature{
From: safeFuncSig(lhs.Funcs[rhsKey].Params),
To: safeFuncSig(rhsValue.Params),
}
}
if !safeStrCmp(lhs.Funcs[rhsKey].Returns, rhsValue.Returns) {
sig.Returns = &Signature{
From: safeFuncSig(lhs.Funcs[rhsKey].Returns),
To: safeFuncSig(rhsValue.Returns),
}
}
if !sig.isEmpty() {
fsc[rhsKey] = sig
}
}
return fsc
}
// InterfaceDef contains a collection of interface methods with signature changes.
// Key is the method name, value contains the signature change information.
type InterfaceDef struct {
MethodSigs map[string]FuncSig `json:"funcSig"`
}
// GetInterfaceMethodSigChanges returns a collection of interfaces with method signature changes.
// Key is the interface name, value contains the method signature change information.
func GetInterfaceMethodSigChanges(lhs, rhs exports.Content) map[string]InterfaceDef {
isc := map[string]InterfaceDef{}
for rhsKey, rhsValue := range rhs.Interfaces {
if _, ok := lhs.Interfaces[rhsKey]; !ok {
continue
}
id := InterfaceDef{}
for rhsMethod, rhsSig := range rhsValue.Methods {
if _, ok := lhs.Interfaces[rhsKey].Methods[rhsMethod]; !ok {
continue
}
sig := FuncSig{}
if !safeStrCmp(lhs.Interfaces[rhsKey].Methods[rhsMethod].Params, rhsSig.Params) {
sig.Params = &Signature{
From: safeFuncSig(lhs.Interfaces[rhsKey].Methods[rhsMethod].Params),
To: safeFuncSig(rhsSig.Params),
}
}
if !safeStrCmp(lhs.Interfaces[rhsKey].Methods[rhsMethod].Returns, rhsSig.Returns) {
sig.Returns = &Signature{
From: safeFuncSig(lhs.Interfaces[rhsKey].Methods[rhsMethod].Returns),
To: safeFuncSig(rhsSig.Returns),
}
}
if !sig.isEmpty() {
if id.MethodSigs == nil {
id.MethodSigs = map[string]FuncSig{}
}
id.MethodSigs[rhsMethod] = sig
}
}
if len(id.MethodSigs) > 0 {
isc[rhsKey] = id
}
}
return isc
}
// StructDef contains a collection of fields within a struct where the field's type has changed.
// Key is the field name, value contains the signature change information.
type StructDef struct {
Fields map[string]Signature `json:"fields"`
}
// GetStructFieldChanges returns a collection of structs with fields that changed their type.
// Key is the struct name, value contains fields with signature changes.
func GetStructFieldChanges(lhs, rhs exports.Content) map[string]StructDef {
sfc := map[string]StructDef{}
for rhsKey, rhsValue := range rhs.Structs {
if _, ok := lhs.Structs[rhsKey]; !ok {
continue
}
sd := StructDef{}
for rhsField, rhsSig := range rhsValue.Fields {
if _, ok := lhs.Structs[rhsKey].Fields[rhsField]; !ok {
continue
}
if lhs.Structs[rhsKey].Fields[rhsField] != rhsSig {
if sd.Fields == nil {
sd.Fields = map[string]Signature{}
}
sd.Fields[rhsField] = Signature{
From: lhs.Structs[rhsKey].Fields[rhsField],
To: rhsSig,
}
}
}
if len(sd.Fields) > 0 {
sfc[rhsKey] = sd
}
}
return sfc
}
func safeFuncSig(s *string) string {
if s == nil {
return None
}
return *s
}
func safeStrCmp(lhs, rhs *string) bool {
if lhs == nil && rhs == nil {
return true
}
if lhs == nil || rhs == nil {
return false
}
return *lhs == *rhs
}