github.com/josephspurrier/go-swagger@v0.2.1-0.20221129144919-1f672a142a00/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 62 pthObj := tgt.Paths[r.route.Path] 63 op := setPathOperation( 64 r.route.Method, r.route.ID, 65 &pthObj, r.operations[r.route.ID]) 66 67 op.Tags = r.route.Tags 68 69 sp := new(sectionedParser) 70 sp.setTitle = func(lines []string) { op.Summary = joinDropLast(lines) } 71 sp.setDescription = func(lines []string) { op.Description = joinDropLast(lines) } 72 sr := newSetResponses(r.definitions, r.responses, opResponsesSetter(op)) 73 spa := newSetParams(r.parameters, opParamSetter(op)) 74 sp.taggers = []tagParser{ 75 newMultiLineTagParser("Consumes", newMultilineDropEmptyParser(rxConsumes, opConsumesSetter(op)), false), 76 newMultiLineTagParser("Produces", newMultilineDropEmptyParser(rxProduces, opProducesSetter(op)), false), 77 newSingleLineTagParser("Schemes", newSetSchemes(opSchemeSetter(op))), 78 newMultiLineTagParser("Security", newSetSecurity(rxSecuritySchemes, opSecurityDefsSetter(op)), false), 79 newMultiLineTagParser("Parameters", spa, false), 80 newMultiLineTagParser("Responses", sr, false), 81 newSingleLineTagParser("Deprecated", &setDeprecatedOp{op}), 82 newMultiLineTagParser("Extensions", newSetExtensions(opExtensionsSetter(op)), true), 83 } 84 if err := sp.Parse(r.route.Remaining); err != nil { 85 return fmt.Errorf("operation (%s): %v", op.ID, err) 86 } 87 88 if tgt.Paths == nil { 89 tgt.Paths = make(map[string]spec.PathItem) 90 } 91 tgt.Paths[r.route.Path] = pthObj 92 return nil 93 }