github.com/rzurga/go-swagger@v0.28.1-0.20211109195225-5d1f453ffa3a/cmd/swagger/commands/diff/reporting.go (about) 1 package diff 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "io" 8 9 "github.com/go-openapi/spec" 10 ) 11 12 // ArrayType const for array 13 var ArrayType = "array" 14 15 // ObjectType const for object 16 var ObjectType = "object" 17 18 // Compare returns the result of analysing breaking and non breaking changes 19 // between to Swagger specs 20 func Compare(spec1, spec2 *spec.Swagger) (diffs SpecDifferences, err error) { 21 analyser := NewSpecAnalyser() 22 err = analyser.Analyse(spec1, spec2) 23 if err != nil { 24 return nil, err 25 } 26 diffs = analyser.Diffs 27 return 28 } 29 30 // PathItemOp - combines path and operation into a single keyed entity 31 type PathItemOp struct { 32 ParentPathItem *spec.PathItem `json:"pathitem"` 33 Operation *spec.Operation `json:"operation"` 34 } 35 36 // URLMethod - combines url and method into a single keyed entity 37 type URLMethod struct { 38 Path string `json:"path"` 39 Method string `json:"method"` 40 } 41 42 // DataDirection indicates the direction of change Request vs Response 43 type DataDirection int 44 45 const ( 46 // Request Used for messages/param diffs in a request 47 Request DataDirection = iota 48 // Response Used for messages/param diffs in a response 49 Response 50 ) 51 52 func getParams(pathParams, opParams []spec.Parameter, location string) map[string]spec.Parameter { 53 params := map[string]spec.Parameter{} 54 // add shared path params 55 for _, eachParam := range pathParams { 56 if eachParam.In == location { 57 params[eachParam.Name] = eachParam 58 } 59 } 60 // add any overridden params 61 for _, eachParam := range opParams { 62 if eachParam.In == location { 63 params[eachParam.Name] = eachParam 64 } 65 } 66 return params 67 } 68 69 func getNameOnlyDiffNode(forLocation string) *Node { 70 node := Node{ 71 Field: forLocation, 72 } 73 return &node 74 } 75 76 func primitiveTypeString(typeName, typeFormat string) string { 77 if typeFormat != "" { 78 return fmt.Sprintf("%s.%s", typeName, typeFormat) 79 } 80 return typeName 81 } 82 83 // TypeDiff - describes a primitive type change 84 type TypeDiff struct { 85 Change SpecChangeCode `json:"change-type,omitempty"` 86 Description string `json:"description,omitempty"` 87 FromType string `json:"from-type,omitempty"` 88 ToType string `json:"to-type,omitempty"` 89 } 90 91 // didn't use 'width' so as not to confuse with bit width 92 var numberWideness = map[string]int{ 93 "number": 3, 94 "number.double": 3, 95 "double": 3, 96 "number.float": 2, 97 "float": 2, 98 "long": 1, 99 "integer.int64": 1, 100 "integer": 0, 101 "integer.int32": 0, 102 } 103 104 func prettyprint(b []byte) (io.ReadWriter, error) { 105 var out bytes.Buffer 106 err := json.Indent(&out, b, "", " ") 107 return &out, err 108 } 109 110 // JSONMarshal allows the item to be correctly rendered to json 111 func JSONMarshal(t interface{}) ([]byte, error) { 112 buffer := &bytes.Buffer{} 113 encoder := json.NewEncoder(buffer) 114 encoder.SetEscapeHTML(false) 115 err := encoder.Encode(t) 116 return buffer.Bytes(), err 117 }