github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/cmd/swagger/commands/diff/difftypes.go (about) 1 package diff 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "log" 8 ) 9 10 // SpecChangeCode enumerates the various types of diffs from one spec to another 11 type SpecChangeCode int 12 13 const ( 14 // NoChangeDetected - the specs have no changes 15 NoChangeDetected SpecChangeCode = iota 16 // DeletedProperty - A message property has been deleted in the new spec 17 DeletedProperty 18 // AddedProperty - A message property has been added in the new spec 19 AddedProperty 20 // AddedRequiredProperty - A required message property has been added in the new spec 21 AddedRequiredProperty 22 // DeletedOptionalParam - An endpoint parameter has been deleted in the new spec 23 DeletedOptionalParam 24 // ChangedDescripton - Changed a description 25 ChangedDescripton 26 // AddedDescripton - Added a description 27 AddedDescripton 28 // DeletedDescripton - Deleted a description 29 DeletedDescripton 30 // ChangedTag - Changed a tag 31 ChangedTag 32 // AddedTag - Added a tag 33 AddedTag 34 // DeletedTag - Deleted a tag 35 DeletedTag 36 // DeletedResponse - An endpoint response has been deleted in the new spec 37 DeletedResponse 38 // DeletedEndpoint - An endpoint has been deleted in the new spec 39 DeletedEndpoint 40 // DeletedDeprecatedEndpoint - A deprecated endpoint has been deleted in the new spec 41 DeletedDeprecatedEndpoint 42 // AddedRequiredParam - A required parameter has been added in the new spec 43 AddedRequiredParam 44 // DeletedRequiredParam - A required parameter has been deleted in the new spec 45 DeletedRequiredParam 46 // AddedEndpoint - An endpoint has been added in the new spec 47 AddedEndpoint 48 // WidenedType - An type has been changed to a more permissive type eg int->string 49 WidenedType 50 // NarrowedType - An type has been changed to a less permissive type eg string->int 51 NarrowedType 52 // ChangedToCompatibleType - An type has been changed to a compatible type eg password->string 53 ChangedToCompatibleType 54 // ChangedType - An type has been changed to a type whose relative compatibility cannot be determined 55 ChangedType 56 // AddedEnumValue - An enum type has had a new potential value added to it 57 AddedEnumValue 58 // DeletedEnumValue - An enum type has had a existing value removed from it 59 DeletedEnumValue 60 // AddedOptionalParam - A new optional parameter has been added to the new spec 61 AddedOptionalParam 62 // ChangedOptionalToRequired - An optional parameter is now required in the new spec 63 ChangedOptionalToRequired 64 // ChangedRequiredToOptional - An required parameter is now optional in the new spec 65 ChangedRequiredToOptional 66 // AddedResponse An endpoint has new response code in the new spec 67 AddedResponse 68 // AddedConsumesFormat - a new consumes format (json/xml/yaml etc) has been added in the new spec 69 AddedConsumesFormat 70 // DeletedConsumesFormat - an existing format has been removed in the new spec 71 DeletedConsumesFormat 72 // AddedProducesFormat - a new produces format (json/xml/yaml etc) has been added in the new spec 73 AddedProducesFormat 74 // DeletedProducesFormat - an existing produces format has been removed in the new spec 75 DeletedProducesFormat 76 // AddedSchemes - a new scheme has been added to the new spec 77 AddedSchemes 78 // DeletedSchemes - a scheme has been removed from the new spec 79 DeletedSchemes 80 // ChangedHostURL - the host url has been changed. If this is used in the client generation, then clients will break. 81 ChangedHostURL 82 // ChangedBasePath - the host base path has been changed. If this is used in the client generation, then clients will break. 83 ChangedBasePath 84 // AddedResponseHeader Added a header Item 85 AddedResponseHeader 86 // ChangedResponseHeader Added a header Item 87 ChangedResponseHeader 88 // DeletedResponseHeader Added a header Item 89 DeletedResponseHeader 90 // RefTargetChanged Changed a ref to point to a different object 91 RefTargetChanged 92 // RefTargetRenamed Renamed a ref to point to the same object 93 RefTargetRenamed 94 // DeletedConstraint Deleted a schema constraint 95 DeletedConstraint 96 // AddedConstraint Added a schema constraint 97 AddedConstraint 98 // DeletedDefinition removed one of the definitions 99 DeletedDefinition 100 // AddedDefinition removed one of the definitions 101 AddedDefinition 102 // ChangedDefault - Changed default value 103 ChangedDefault 104 // AddedDefault - Added a default value 105 AddedDefault 106 // DeletedDefault - Deleted a default value 107 DeletedDefault 108 // ChangedExample - Changed an example value 109 ChangedExample 110 // AddedExample - Added an example value 111 AddedExample 112 // DeletedExample - Deleted an example value 113 DeletedExample 114 // ChangedCollectionFormat - A collectionFormat has been changed to a collectionFormat whose relative compatibility cannot be determined 115 ChangedCollectionFormat 116 // DeletedExtension deleted an extension 117 DeletedExtension 118 // AddedExtension added an extension 119 AddedExtension 120 // ChangedExtensionValue changed an extension value 121 ChangedExtensionValue 122 ) 123 124 var toLongStringSpecChangeCode = map[SpecChangeCode]string{ 125 NoChangeDetected: "No Change detected", 126 AddedEndpoint: "Added endpoint", 127 DeletedEndpoint: "Deleted endpoint", 128 DeletedDeprecatedEndpoint: "Deleted a deprecated endpoint", 129 AddedRequiredProperty: "Added required property", 130 DeletedProperty: "Deleted property", 131 ChangedDescripton: "Changed a description", 132 AddedDescripton: "Added a description", 133 DeletedDescripton: "Deleted a description", 134 ChangedTag: "Changed a tag", 135 AddedTag: "Added a tag", 136 DeletedTag: "Deleted a tag", 137 AddedProperty: "Added property", 138 AddedOptionalParam: "Added optional param", 139 AddedRequiredParam: "Added required param", 140 DeletedOptionalParam: "Deleted optional param", 141 DeletedRequiredParam: "Deleted required param", 142 DeletedResponse: "Deleted response", 143 AddedResponse: "Added response", 144 WidenedType: "Widened type", 145 NarrowedType: "Narrowed type", 146 ChangedType: "Changed type", 147 ChangedToCompatibleType: "Changed type to equivalent type", 148 ChangedOptionalToRequired: "Changed optional param to required", 149 ChangedRequiredToOptional: "Changed required param to optional", 150 AddedEnumValue: "Added possible enumeration(s)", 151 DeletedEnumValue: "Deleted possible enumeration(s)", 152 AddedConsumesFormat: "Added a consumes format", 153 DeletedConsumesFormat: "Deleted a consumes format", 154 AddedProducesFormat: "Added produces format", 155 DeletedProducesFormat: "Deleted produces format", 156 AddedSchemes: "Added schemes", 157 DeletedSchemes: "Deleted schemes", 158 ChangedHostURL: "Changed host URL", 159 ChangedBasePath: "Changed base path", 160 AddedResponseHeader: "Added response header", 161 ChangedResponseHeader: "Changed response header", 162 DeletedResponseHeader: "Deleted response header", 163 RefTargetChanged: "Changed ref to different object", 164 RefTargetRenamed: "Changed ref to renamed object", 165 DeletedConstraint: "Deleted a schema constraint", 166 AddedConstraint: "Added a schema constraint", 167 DeletedDefinition: "Deleted a schema definition", 168 AddedDefinition: "Added a schema definition", 169 ChangedDefault: "Default value is changed", 170 AddedDefault: "Default value is added", 171 DeletedDefault: "Default value is removed", 172 ChangedExample: "Example value is changed", 173 AddedExample: "Example value is added", 174 DeletedExample: "Example value is removed", 175 ChangedCollectionFormat: "Changed collection format", 176 DeletedExtension: "Deleted Extension", 177 AddedExtension: "Added Extension", 178 ChangedExtensionValue: "Changed Extension Value", 179 } 180 181 var toStringSpecChangeCode = map[SpecChangeCode]string{ 182 AddedEndpoint: "AddedEndpoint", 183 NoChangeDetected: "NoChangeDetected", 184 DeletedEndpoint: "DeletedEndpoint", 185 DeletedDeprecatedEndpoint: "DeletedDeprecatedEndpoint", 186 AddedRequiredProperty: "AddedRequiredProperty", 187 DeletedProperty: "DeletedProperty", 188 AddedProperty: "AddedProperty", 189 ChangedDescripton: "ChangedDescription", 190 AddedDescripton: "AddedDescription", 191 DeletedDescripton: "DeletedDescription", 192 ChangedTag: "ChangedTag", 193 AddedTag: "AddedTag", 194 DeletedTag: "DeletedTag", 195 AddedOptionalParam: "AddedOptionalParam", 196 AddedRequiredParam: "AddedRequiredParam", 197 DeletedOptionalParam: "DeletedRequiredParam", 198 DeletedRequiredParam: "Deleted required param", 199 DeletedResponse: "DeletedResponse", 200 AddedResponse: "AddedResponse", 201 WidenedType: "WidenedType", 202 NarrowedType: "NarrowedType", 203 ChangedType: "ChangedType", 204 ChangedToCompatibleType: "ChangedToCompatibleType", 205 ChangedOptionalToRequired: "ChangedOptionalToRequiredParam", 206 ChangedRequiredToOptional: "ChangedRequiredToOptionalParam", 207 AddedEnumValue: "AddedEnumValue", 208 DeletedEnumValue: "DeletedEnumValue", 209 AddedConsumesFormat: "AddedConsumesFormat", 210 DeletedConsumesFormat: "DeletedConsumesFormat", 211 AddedProducesFormat: "AddedProducesFormat", 212 DeletedProducesFormat: "DeletedProducesFormat", 213 AddedSchemes: "AddedSchemes", 214 DeletedSchemes: "DeletedSchemes", 215 ChangedHostURL: "ChangedHostURL", 216 ChangedBasePath: "ChangedBasePath", 217 AddedResponseHeader: "AddedResponseHeader", 218 ChangedResponseHeader: "ChangedResponseHeader", 219 DeletedResponseHeader: "DeletedResponseHeader", 220 RefTargetChanged: "RefTargetChanged", 221 RefTargetRenamed: "RefTargetRenamed", 222 DeletedConstraint: "DeletedConstraint", 223 AddedConstraint: "AddedConstraint", 224 DeletedDefinition: "DeletedDefinition", 225 AddedDefinition: "AddedDefinition", 226 ChangedDefault: "ChangedDefault", 227 AddedDefault: "AddedDefault", 228 DeletedDefault: "DeletedDefault", 229 ChangedExample: "ChangedExample", 230 AddedExample: "AddedExample", 231 DeletedExample: "DeletedExample", 232 ChangedCollectionFormat: "ChangedCollectionFormat", 233 DeletedExtension: "DeletedExtension", 234 AddedExtension: "AddedExtension", 235 ChangedExtensionValue: "ChangedExtensionValue", 236 } 237 238 var toIDSpecChangeCode = map[string]SpecChangeCode{} 239 240 // Description returns an english version of this error 241 func (s SpecChangeCode) Description() (result string) { 242 result, ok := toLongStringSpecChangeCode[s] 243 if !ok { 244 log.Printf("warning: No description for %v", s) 245 result = "UNDEFINED" 246 } 247 return 248 } 249 250 // MarshalJSON marshals the enum as a quoted json string 251 func (s SpecChangeCode) MarshalJSON() ([]byte, error) { 252 return stringAsQuotedBytes(toStringSpecChangeCode[s]) 253 } 254 255 // UnmarshalJSON unmashalls a quoted json string to the enum value 256 func (s *SpecChangeCode) UnmarshalJSON(b []byte) error { 257 str, err := readStringFromByteStream(b) 258 if err != nil { 259 return err 260 } 261 // Note that if the string cannot be found then it will return an error to the caller. 262 val, ok := toIDSpecChangeCode[str] 263 264 if ok { 265 *s = val 266 } else { 267 return fmt.Errorf("unknown enum value. cannot unmarshal '%s'", str) 268 } 269 return nil 270 } 271 272 // Compatibility - whether this is a breaking or non-breaking change 273 type Compatibility int 274 275 const ( 276 // Breaking this change could break existing clients 277 Breaking Compatibility = iota 278 // NonBreaking This is a backwards-compatible API change 279 NonBreaking 280 // Warning changes are technically non-breaking but can cause behavior changes in client and thus should be reported differently 281 Warning 282 ) 283 284 func (s Compatibility) String() string { 285 return toStringCompatibility[s] 286 } 287 288 var toStringCompatibility = map[Compatibility]string{ 289 Breaking: "Breaking", 290 NonBreaking: "NonBreaking", 291 Warning: "Warning", 292 } 293 294 var toIDCompatibility = map[string]Compatibility{} 295 296 // MarshalJSON marshals the enum as a quoted json string 297 func (s Compatibility) MarshalJSON() ([]byte, error) { 298 return stringAsQuotedBytes(toStringCompatibility[s]) 299 } 300 301 // UnmarshalJSON unmashals a quoted json string to the enum value 302 func (s *Compatibility) UnmarshalJSON(b []byte) error { 303 str, err := readStringFromByteStream(b) 304 if err != nil { 305 return err 306 } 307 // Note that if the string cannot be found then it will return an error to the caller. 308 val, ok := toIDCompatibility[str] 309 310 if ok { 311 *s = val 312 } else { 313 return fmt.Errorf("unknown enum value. cannot unmarshal '%s'", str) 314 } 315 return nil 316 } 317 318 func stringAsQuotedBytes(str string) ([]byte, error) { 319 buffer := bytes.NewBufferString(`"`) 320 buffer.WriteString(str) 321 buffer.WriteString(`"`) 322 return buffer.Bytes(), nil 323 } 324 325 func readStringFromByteStream(b []byte) (string, error) { 326 var j string 327 err := json.Unmarshal(b, &j) 328 if err != nil { 329 return "", err 330 } 331 return j, nil 332 } 333 334 func init() { 335 for key, val := range toStringSpecChangeCode { 336 toIDSpecChangeCode[val] = key 337 } 338 for key, val := range toStringCompatibility { 339 toIDCompatibility[val] = key 340 } 341 }