-
Notifications
You must be signed in to change notification settings - Fork 0
/
change.go
190 lines (159 loc) · 3.11 KB
/
change.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
package semverlint
import (
"fmt"
"go/types"
"strings"
)
type APIChanges []PackageChanges
type PackageChanges struct {
Name string
Path string
Changes []Change
}
func NewPackageChanges(name, path string, changes ...Change) PackageChanges {
return PackageChanges{name, path, changes}
}
type DeclType byte
const (
VarType DeclType = iota
ConstType
FuncType
InterfaceType
StructType
TypeDefType
PackageType
)
func (d DeclType) String() string {
switch d {
case VarType:
return "package-level variable"
case ConstType:
return "package-level constant"
case FuncType:
return "function"
case InterfaceType:
return "interface"
case StructType:
return "struct"
case TypeDefType:
return "type definition"
case PackageType:
return "package"
default:
return "INVALID"
}
}
type Change interface {
String() string
}
type DeclChange struct {
Name string
Type DeclType
Changes []Change
}
func NewDeclChange(name string, typ DeclType, changes ...Change) DeclChange {
return DeclChange{name, typ, changes}
}
func (d DeclChange) String() string {
return fmt.Sprintf(
"%s %s: %s",
d.Type,
d.Name,
joinChanges(d.Changes),
)
}
type ArgumentChanged struct {
Pos int
Name string
Type types.Type
Changes []Change
}
func (a ArgumentChanged) String() string {
return fmt.Sprintf(
"argument %s with type %s at position %d: %s",
a.Name,
typeString(a.Type),
a.Pos,
joinChanges(a.Changes),
)
}
type ResultChanged struct {
Pos int
Type types.Type
Changes []Change
}
func (r ResultChanged) String() string {
return fmt.Sprintf(
"result with type %s at position %d: %s",
typeString(r.Type),
r.Pos,
joinChanges(r.Changes),
)
}
type FieldChanged struct {
Pos int
Name string
Changes []Change
}
func (f FieldChanged) String() string {
return fmt.Sprintf(
"field %q at position %d: %s",
f.Name,
f.Pos,
joinChanges(f.Changes),
)
}
type TypeChanged struct {
From types.Type
To types.Type
}
func (tc TypeChanged) String() string {
return fmt.Sprintf("type changed from %q to %q", tc.From, tc.To)
}
type PositionChanged struct {
From int
To int
}
func (p PositionChanged) String() string {
return fmt.Sprintf("position changed from %d to %d", p.From, p.To)
}
type Removed struct{}
func (Removed) String() string { return "was removed" }
type Added struct{}
func (Added) String() string { return "was added" }
type ValueChanged struct {
From string
To string
}
func (v ValueChanged) String() string {
return fmt.Sprintf("value changed from %s to %s", v.From, v.To)
}
func IsBreaking(change Change) bool {
switch c := change.(type) {
case Removed,
PositionChanged,
TypeChanged,
FieldChanged,
ResultChanged,
ArgumentChanged:
return true
case DeclChange:
for _, c := range c.Changes {
if IsBreaking(c) {
return true
}
}
}
return false
}
func joinChanges(cs []Change) string {
var strs = make([]string, len(cs))
for i, c := range cs {
strs[i] = c.String()
}
return strings.Join(strs, ", ")
}
func typeString(t types.Type) string {
// TODO: check actual printing here is what's required for display
return t.String()
}