github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/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  	Extensions     spec.Extensions `json:"extensions"`
    35  }
    36  
    37  // URLMethod - combines url and method into a single keyed entity
    38  type URLMethod struct {
    39  	Path   string `json:"path"`
    40  	Method string `json:"method"`
    41  }
    42  
    43  // DataDirection indicates the direction of change Request vs Response
    44  type DataDirection int
    45  
    46  const (
    47  	// Request Used for messages/param diffs in a request
    48  	Request DataDirection = iota
    49  	// Response Used for messages/param diffs in a response
    50  	Response
    51  )
    52  
    53  func getParams(pathParams, opParams []spec.Parameter, location string) map[string]spec.Parameter {
    54  	params := map[string]spec.Parameter{}
    55  	// add shared path params
    56  	for _, eachParam := range pathParams {
    57  		if eachParam.In == location {
    58  			params[eachParam.Name] = eachParam
    59  		}
    60  	}
    61  	// add any overridden params
    62  	for _, eachParam := range opParams {
    63  		if eachParam.In == location {
    64  			params[eachParam.Name] = eachParam
    65  		}
    66  	}
    67  	return params
    68  }
    69  
    70  func getNameOnlyDiffNode(forLocation string) *Node {
    71  	node := Node{
    72  		Field: forLocation,
    73  	}
    74  	return &node
    75  }
    76  
    77  func primitiveTypeString(typeName, typeFormat string) string {
    78  	if typeFormat != "" {
    79  		return fmt.Sprintf("%s.%s", typeName, typeFormat)
    80  	}
    81  	return typeName
    82  }
    83  
    84  // TypeDiff - describes a primitive type change
    85  type TypeDiff struct {
    86  	Change      SpecChangeCode `json:"change-type,omitempty"`
    87  	Description string         `json:"description,omitempty"`
    88  	FromType    string         `json:"from-type,omitempty"`
    89  	ToType      string         `json:"to-type,omitempty"`
    90  }
    91  
    92  // didn't use 'width' so as not to confuse with bit width
    93  var numberWideness = map[string]int{
    94  	"number":        3,
    95  	"number.double": 3,
    96  	"double":        3,
    97  	"number.float":  2,
    98  	"float":         2,
    99  	"long":          1,
   100  	"integer.int64": 1,
   101  	"integer":       0,
   102  	"integer.int32": 0,
   103  }
   104  
   105  func prettyprint(b []byte) (io.ReadWriter, error) {
   106  	var out bytes.Buffer
   107  	err := json.Indent(&out, b, "", "  ")
   108  	return &out, err
   109  }
   110  
   111  // JSONMarshal allows the item to be correctly rendered to json
   112  func JSONMarshal(t interface{}) ([]byte, error) {
   113  	buffer := &bytes.Buffer{}
   114  	encoder := json.NewEncoder(buffer)
   115  	encoder.SetEscapeHTML(false)
   116  	err := encoder.Encode(t)
   117  	return buffer.Bytes(), err
   118  }