...

Source file src/github.com/go-openapi/analysis/internal/flatten/operations/operations.go

Documentation: github.com/go-openapi/analysis/internal/flatten/operations

     1  package operations
     2  
     3  import (
     4  	"path"
     5  	"sort"
     6  	"strings"
     7  
     8  	"github.com/go-openapi/jsonpointer"
     9  	"github.com/go-openapi/spec"
    10  	"github.com/go-openapi/swag"
    11  )
    12  
    13  // AllOpRefsByRef returns an index of sortable operations
    14  func AllOpRefsByRef(specDoc Provider, operationIDs []string) map[string]OpRef {
    15  	return OpRefsByRef(GatherOperations(specDoc, operationIDs))
    16  }
    17  
    18  // OpRefsByRef indexes a map of sortable operations
    19  func OpRefsByRef(oprefs map[string]OpRef) map[string]OpRef {
    20  	result := make(map[string]OpRef, len(oprefs))
    21  	for _, v := range oprefs {
    22  		result[v.Ref.String()] = v
    23  	}
    24  
    25  	return result
    26  }
    27  
    28  // OpRef is an indexable, sortable operation
    29  type OpRef struct {
    30  	Method string
    31  	Path   string
    32  	Key    string
    33  	ID     string
    34  	Op     *spec.Operation
    35  	Ref    spec.Ref
    36  }
    37  
    38  // OpRefs is a sortable collection of operations
    39  type OpRefs []OpRef
    40  
    41  func (o OpRefs) Len() int           { return len(o) }
    42  func (o OpRefs) Swap(i, j int)      { o[i], o[j] = o[j], o[i] }
    43  func (o OpRefs) Less(i, j int) bool { return o[i].Key < o[j].Key }
    44  
    45  // Provider knows how to collect operations from a spec
    46  type Provider interface {
    47  	Operations() map[string]map[string]*spec.Operation
    48  }
    49  
    50  // GatherOperations builds a map of sorted operations from a spec
    51  func GatherOperations(specDoc Provider, operationIDs []string) map[string]OpRef {
    52  	var oprefs OpRefs
    53  
    54  	for method, pathItem := range specDoc.Operations() {
    55  		for pth, operation := range pathItem {
    56  			vv := *operation
    57  			oprefs = append(oprefs, OpRef{
    58  				Key:    swag.ToGoName(strings.ToLower(method) + " " + pth),
    59  				Method: method,
    60  				Path:   pth,
    61  				ID:     vv.ID,
    62  				Op:     &vv,
    63  				Ref:    spec.MustCreateRef("#" + path.Join("/paths", jsonpointer.Escape(pth), method)),
    64  			})
    65  		}
    66  	}
    67  
    68  	sort.Sort(oprefs)
    69  
    70  	operations := make(map[string]OpRef)
    71  	for _, opr := range oprefs {
    72  		nm := opr.ID
    73  		if nm == "" {
    74  			nm = opr.Key
    75  		}
    76  
    77  		oo, found := operations[nm]
    78  		if found && oo.Method != opr.Method && oo.Path != opr.Path {
    79  			nm = opr.Key
    80  		}
    81  
    82  		if len(operationIDs) == 0 || swag.ContainsStrings(operationIDs, opr.ID) || swag.ContainsStrings(operationIDs, nm) {
    83  			opr.ID = nm
    84  			opr.Op.ID = nm
    85  			operations[nm] = opr
    86  		}
    87  	}
    88  
    89  	return operations
    90  }
    91  

View as plain text