github.com/go-swagger/go-swagger@v0.31.0/codescan/routes.go (about)

     1  package codescan
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/go-openapi/spec"
     7  )
     8  
     9  func opConsumesSetter(op *spec.Operation) func([]string) {
    10  	return func(consumes []string) { op.Consumes = consumes }
    11  }
    12  
    13  func opProducesSetter(op *spec.Operation) func([]string) {
    14  	return func(produces []string) { op.Produces = produces }
    15  }
    16  
    17  func opSchemeSetter(op *spec.Operation) func([]string) {
    18  	return func(schemes []string) { op.Schemes = schemes }
    19  }
    20  
    21  func opSecurityDefsSetter(op *spec.Operation) func([]map[string][]string) {
    22  	return func(securityDefs []map[string][]string) { op.Security = securityDefs }
    23  }
    24  
    25  func opResponsesSetter(op *spec.Operation) func(*spec.Response, map[int]spec.Response) {
    26  	return func(def *spec.Response, scr map[int]spec.Response) {
    27  		if op.Responses == nil {
    28  			op.Responses = new(spec.Responses)
    29  		}
    30  		op.Responses.Default = def
    31  		op.Responses.StatusCodeResponses = scr
    32  	}
    33  }
    34  
    35  func opParamSetter(op *spec.Operation) func([]*spec.Parameter) {
    36  	return func(params []*spec.Parameter) {
    37  		for _, v := range params {
    38  			op.AddParam(v)
    39  		}
    40  	}
    41  }
    42  
    43  func opExtensionsSetter(op *spec.Operation) func(*spec.Extensions) {
    44  	return func(exts *spec.Extensions) {
    45  		for name, value := range *exts {
    46  			op.AddExtension(name, value)
    47  		}
    48  	}
    49  }
    50  
    51  type routesBuilder struct {
    52  	ctx         *scanCtx
    53  	route       parsedPathContent
    54  	definitions map[string]spec.Schema
    55  	operations  map[string]*spec.Operation
    56  	responses   map[string]spec.Response
    57  	parameters  []*spec.Parameter
    58  }
    59  
    60  func (r *routesBuilder) Build(tgt *spec.Paths) error {
    61  	pthObj := tgt.Paths[r.route.Path]
    62  	op := setPathOperation(
    63  		r.route.Method, r.route.ID,
    64  		&pthObj, r.operations[r.route.ID])
    65  
    66  	op.Tags = r.route.Tags
    67  
    68  	sp := new(sectionedParser)
    69  	sp.setTitle = func(lines []string) { op.Summary = joinDropLast(lines) }
    70  	sp.setDescription = func(lines []string) { op.Description = joinDropLast(lines) }
    71  	sr := newSetResponses(r.definitions, r.responses, opResponsesSetter(op))
    72  	spa := newSetParams(r.parameters, opParamSetter(op))
    73  	sp.taggers = []tagParser{
    74  		newMultiLineTagParser("Consumes", newMultilineDropEmptyParser(rxConsumes, opConsumesSetter(op)), false),
    75  		newMultiLineTagParser("Produces", newMultilineDropEmptyParser(rxProduces, opProducesSetter(op)), false),
    76  		newSingleLineTagParser("Schemes", newSetSchemes(opSchemeSetter(op))),
    77  		newMultiLineTagParser("Security", newSetSecurity(rxSecuritySchemes, opSecurityDefsSetter(op)), false),
    78  		newMultiLineTagParser("Parameters", spa, false),
    79  		newMultiLineTagParser("Responses", sr, false),
    80  		newSingleLineTagParser("Deprecated", &setDeprecatedOp{op}),
    81  		newMultiLineTagParser("Extensions", newSetExtensions(opExtensionsSetter(op)), true),
    82  	}
    83  	if err := sp.Parse(r.route.Remaining); err != nil {
    84  		return fmt.Errorf("operation (%s): %w", op.ID, err)
    85  	}
    86  
    87  	if tgt.Paths == nil {
    88  		tgt.Paths = make(map[string]spec.PathItem)
    89  	}
    90  	tgt.Paths[r.route.Path] = pthObj
    91  	return nil
    92  }