/
difftypes.go
276 lines (254 loc) · 10.4 KB
/
difftypes.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
package diff
import (
"bytes"
"encoding/json"
"fmt"
)
// SpecChangeCode enumerates the various types of diffs from one spec to another
type SpecChangeCode int
const (
// NoChangeDetected - the specs have no changes
NoChangeDetected SpecChangeCode = iota
// DeletedProperty - A message property has been deleted in the new spec
DeletedProperty
// AddedProperty - A message property has been added in the new spec
AddedProperty
// AddedRequiredProperty - A required message property has been added in the new spec
AddedRequiredProperty
// DeletedOptionalParam - An endpoint parameter has been deleted in the new spec
DeletedOptionalParam
// ChangedDescripton - Changed a description
ChangedDescripton
// AddedDescripton - Added a description
AddedDescripton
// DeletedDescripton - Deleted a description
DeletedDescripton
// ChangedTag - Changed a tag
ChangedTag
// AddedTag - Added a tag
AddedTag
// DeletedTag - Deleted a tag
DeletedTag
// DeletedResponse - An endpoint response has been deleted in the new spec
DeletedResponse
// DeletedEndpoint - An endpoint has been deleted in the new spec
DeletedEndpoint
// DeletedDeprecatedEndpoint - A deprecated endpoint has been deleted in the new spec
DeletedDeprecatedEndpoint
// AddedRequiredParam - A required parameter has been added in the new spec
AddedRequiredParam
// DeletedRequiredParam - A required parameter has been deleted in the new spec
DeletedRequiredParam
// ChangedRequiredToOptional - A required parameter has been made optional in the new spec
ChangedRequiredToOptional
// AddedEndpoint - An endpoint has been added in the new spec
AddedEndpoint
// WidenedType - An type has been changed to a more permissive type eg int->string
WidenedType
// NarrowedType - An type has been changed to a less permissive type eg string->int
NarrowedType
// ChangedToCompatibleType - An type has been changed to a compatible type eg password->string
ChangedToCompatibleType
// ChangedType - An type has been changed to a type whose relative compatibility cannot be determined
ChangedType
// AddedEnumValue - An enum type has had a new potential value added to it
AddedEnumValue
// DeletedEnumValue - An enum type has had a existing value removed from it
DeletedEnumValue
// AddedOptionalParam - A new optional parameter has been added to the new spec
AddedOptionalParam
// ChangedOptionalToRequiredParam - An optional parameter is now required in the new spec
ChangedOptionalToRequiredParam
// ChangedRequiredToOptionalParam - An required parameter is now optional in the new spec
ChangedRequiredToOptionalParam
// AddedResponse An endpoint has new response code in the new spec
AddedResponse
// AddedConsumesFormat - a new consumes format (json/xml/yaml etc) has been added in the new spec
AddedConsumesFormat
// DeletedConsumesFormat - an existing format has been removed in the new spec
DeletedConsumesFormat
// AddedProducesFormat - a new produces format (json/xml/yaml etc) has been added in the new spec
AddedProducesFormat
// DeletedProducesFormat - an existing produces format has been removed in the new spec
DeletedProducesFormat
// AddedSchemes - a new scheme has been added to the new spec
AddedSchemes
// DeletedSchemes - a scheme has been removed from the new spec
DeletedSchemes
// ChangedHostURL - the host url has been changed. If this is used in the client generation, then clients will break.
ChangedHostURL
// ChangedBasePath - the host base path has been changed. If this is used in the client generation, then clients will break.
ChangedBasePath
// AddedResponseHeader Added a header Item
AddedResponseHeader
// ChangedResponseHeader Added a header Item
ChangedResponseHeader
// DeletedResponseHeader Added a header Item
DeletedResponseHeader
)
var toLongStringSpecChangeCode = map[SpecChangeCode]string{
NoChangeDetected: "No Change detected",
AddedEndpoint: "Added endpoint",
DeletedEndpoint: "Deleted endpoint",
DeletedDeprecatedEndpoint: "Deleted a deprecated endpoint",
AddedRequiredProperty: "Added required property",
DeletedProperty: "Deleted property",
ChangedDescripton: "Changed a description",
AddedDescripton: "Added a description",
DeletedDescripton: "Deleted a description",
ChangedTag: "Changed a tag",
AddedTag: "Added a tag",
DeletedTag: "Deleted a tag",
AddedProperty: "Added property",
AddedOptionalParam: "Added optional param",
AddedRequiredParam: "Added required param",
DeletedOptionalParam: "Deleted optional param",
DeletedRequiredParam: "Deleted required param",
DeletedResponse: "Deleted response",
AddedResponse: "Added response",
WidenedType: "Widened type",
NarrowedType: "Narrowed type",
ChangedType: "Changed type",
ChangedToCompatibleType: "Changed type to equivalent type",
ChangedOptionalToRequiredParam: "Changed optional param to required",
ChangedRequiredToOptionalParam: "Changed required param to optional",
AddedEnumValue: "Added possible enumeration(s)",
DeletedEnumValue: "Deleted possible enumeration(s)",
AddedConsumesFormat: "Added a consumes format",
DeletedConsumesFormat: "Deleted a consumes format",
AddedProducesFormat: "Added produces format",
DeletedProducesFormat: "Deleted produces format",
AddedSchemes: "Added schemes",
DeletedSchemes: "Deleted schemes",
ChangedHostURL: "Changed host URL",
ChangedBasePath: "Changed base path",
AddedResponseHeader: "Added response header",
ChangedResponseHeader: "Changed response header",
DeletedResponseHeader: "Deleted response header",
}
var toStringSpecChangeCode = map[SpecChangeCode]string{
AddedEndpoint: "AddedEndpoint",
NoChangeDetected: "NoChangeDetected",
DeletedEndpoint: "DeletedEndpoint",
DeletedDeprecatedEndpoint: "DeletedDeprecatedEndpoint",
AddedRequiredProperty: "AddedRequiredProperty",
DeletedProperty: "DeletedProperty",
AddedProperty: "AddedProperty",
ChangedDescripton: "ChangedDescription",
AddedDescripton: "AddedDescription",
DeletedDescripton: "DeletedDescription",
ChangedTag: "ChangedTag",
AddedTag: "AddedTag",
DeletedTag: "DeletedTag",
AddedOptionalParam: "AddedOptionalParam",
AddedRequiredParam: "AddedRequiredParam",
DeletedOptionalParam: "DeletedRequiredParam",
DeletedRequiredParam: "Deleted required param",
DeletedResponse: "DeletedResponse",
AddedResponse: "AddedResponse",
WidenedType: "WidenedType",
NarrowedType: "NarrowedType",
ChangedType: "ChangedType",
ChangedToCompatibleType: "ChangedToCompatibleType",
ChangedOptionalToRequiredParam: "ChangedOptionalToRequiredParam",
ChangedRequiredToOptionalParam: "ChangedRequiredToOptionalParam",
AddedEnumValue: "AddedEnumValue",
DeletedEnumValue: "DeletedEnumValue",
AddedConsumesFormat: "AddedConsumesFormat",
DeletedConsumesFormat: "DeletedConsumesFormat",
AddedProducesFormat: "AddedProducesFormat",
DeletedProducesFormat: "DeletedProducesFormat",
AddedSchemes: "AddedSchemes",
DeletedSchemes: "DeletedSchemes",
ChangedHostURL: "ChangedHostURL",
ChangedBasePath: "ChangedBasePath",
AddedResponseHeader: "AddedResponseHeader",
ChangedResponseHeader: "ChangedResponseHeader",
DeletedResponseHeader: "DeletedResponseHeader",
}
var toIDSpecChangeCode = map[string]SpecChangeCode{}
// Description returns an english version of this error
func (s *SpecChangeCode) Description() (result string) {
result, ok := toLongStringSpecChangeCode[*s]
if !ok {
fmt.Printf("WARNING: No description for %v", *s)
result = "UNDEFINED"
}
return
}
// MarshalJSON marshals the enum as a quoted json string
func (s *SpecChangeCode) MarshalJSON() ([]byte, error) {
return stringAsQuotedBytes(toStringSpecChangeCode[*s])
}
// UnmarshalJSON unmashalls a quoted json string to the enum value
func (s *SpecChangeCode) UnmarshalJSON(b []byte) error {
str, err := readStringFromByteStream(b)
if err != nil {
return err
}
// Note that if the string cannot be found then it will return an error to the caller.
val, ok := toIDSpecChangeCode[str]
if ok {
*s = val
} else {
return fmt.Errorf("unknown enum value. cannot unmarshal '%s'", str)
}
return nil
}
// Compatibility - whether this is a breaking or non-breaking change
type Compatibility int
const (
// Breaking this change could break existing clients
Breaking Compatibility = iota
// NonBreaking This is a backwards-compatible API change
NonBreaking
)
func (s Compatibility) String() string {
return toStringCompatibility[s]
}
var toStringCompatibility = map[Compatibility]string{
Breaking: "Breaking",
NonBreaking: "NonBreaking",
}
var toIDCompatibility = map[string]Compatibility{}
// MarshalJSON marshals the enum as a quoted json string
func (s *Compatibility) MarshalJSON() ([]byte, error) {
return stringAsQuotedBytes(toStringCompatibility[*s])
}
// UnmarshalJSON unmashals a quoted json string to the enum value
func (s *Compatibility) UnmarshalJSON(b []byte) error {
str, err := readStringFromByteStream(b)
if err != nil {
return err
}
// Note that if the string cannot be found then it will return an error to the caller.
val, ok := toIDCompatibility[str]
if ok {
*s = val
} else {
return fmt.Errorf("unknown enum value. cannot unmarshal '%s'", str)
}
return nil
}
func stringAsQuotedBytes(str string) ([]byte, error) {
buffer := bytes.NewBufferString(`"`)
buffer.WriteString(str)
buffer.WriteString(`"`)
return buffer.Bytes(), nil
}
func readStringFromByteStream(b []byte) (string, error) {
var j string
err := json.Unmarshal(b, &j)
if err != nil {
return "", err
}
return j, nil
}
func init() {
for key, val := range toStringSpecChangeCode {
toIDSpecChangeCode[val] = key
}
for key, val := range toStringCompatibility {
toIDCompatibility[val] = key
}
}