github.com/emreu/go-swagger@v0.22.1/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 type routesBuilder struct { 44 ctx *scanCtx 45 route parsedPathContent 46 definitions map[string]spec.Schema 47 operations map[string]*spec.Operation 48 responses map[string]spec.Response 49 parameters []*spec.Parameter 50 } 51 52 func (r *routesBuilder) Build(tgt *spec.Paths) error { 53 54 pthObj := tgt.Paths[r.route.Path] 55 op := setPathOperation( 56 r.route.Method, r.route.ID, 57 &pthObj, r.operations[r.route.ID]) 58 59 op.Tags = r.route.Tags 60 61 sp := new(sectionedParser) 62 sp.setTitle = func(lines []string) { op.Summary = joinDropLast(lines) } 63 sp.setDescription = func(lines []string) { op.Description = joinDropLast(lines) } 64 sr := newSetResponses(r.definitions, r.responses, opResponsesSetter(op)) 65 spa := newSetParams(r.parameters, opParamSetter(op)) 66 sp.taggers = []tagParser{ 67 newMultiLineTagParser("Consumes", newMultilineDropEmptyParser(rxConsumes, opConsumesSetter(op)), false), 68 newMultiLineTagParser("Produces", newMultilineDropEmptyParser(rxProduces, opProducesSetter(op)), false), 69 newSingleLineTagParser("Schemes", newSetSchemes(opSchemeSetter(op))), 70 newMultiLineTagParser("Security", newSetSecurity(rxSecuritySchemes, opSecurityDefsSetter(op)), false), 71 newMultiLineTagParser("Parameters", spa, false), 72 newMultiLineTagParser("Responses", sr, false), 73 newSingleLineTagParser("Deprecated", &setDeprecatedOp{op}), 74 } 75 if err := sp.Parse(r.route.Remaining); err != nil { 76 return fmt.Errorf("operation (%s): %v", op.ID, err) 77 } 78 79 if tgt.Paths == nil { 80 tgt.Paths = make(map[string]spec.PathItem) 81 } 82 tgt.Paths[r.route.Path] = pthObj 83 return nil 84 }