github.com/emreu/go-swagger@v0.22.1/scan/routes.go (about) 1 // +build !go1.11 2 3 // Copyright 2015 go-swagger maintainers 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 package scan 18 19 import ( 20 "fmt" 21 "go/ast" 22 23 "github.com/go-openapi/spec" 24 25 "golang.org/x/tools/go/loader" 26 ) 27 28 func opConsumesSetter(op *spec.Operation) func([]string) { 29 return func(consumes []string) { op.Consumes = consumes } 30 } 31 32 func opProducesSetter(op *spec.Operation) func([]string) { 33 return func(produces []string) { op.Produces = produces } 34 } 35 36 func opSchemeSetter(op *spec.Operation) func([]string) { 37 return func(schemes []string) { op.Schemes = schemes } 38 } 39 40 func opSecurityDefsSetter(op *spec.Operation) func([]map[string][]string) { 41 return func(securityDefs []map[string][]string) { op.Security = securityDefs } 42 } 43 44 func opResponsesSetter(op *spec.Operation) func(*spec.Response, map[int]spec.Response) { 45 return func(def *spec.Response, scr map[int]spec.Response) { 46 if op.Responses == nil { 47 op.Responses = new(spec.Responses) 48 } 49 op.Responses.Default = def 50 op.Responses.StatusCodeResponses = scr 51 } 52 } 53 54 func opParamSetter(op *spec.Operation) func([]*spec.Parameter) { 55 return func(params []*spec.Parameter) { 56 for _, v := range params { 57 op.AddParam(v) 58 } 59 } 60 } 61 62 func newRoutesParser(prog *loader.Program) *routesParser { 63 return &routesParser{ 64 program: prog, 65 } 66 } 67 68 type routesParser struct { 69 program *loader.Program 70 definitions map[string]spec.Schema 71 operations map[string]*spec.Operation 72 responses map[string]spec.Response 73 parameters []*spec.Parameter 74 } 75 76 func (rp *routesParser) Parse(gofile *ast.File, target interface{}, includeTags map[string]bool, excludeTags map[string]bool) error { 77 tgt := target.(*spec.Paths) 78 for _, comsec := range gofile.Comments { 79 content := parsePathAnnotation(rxRoute, comsec.List) 80 81 if content.Method == "" { 82 continue // it's not, next! 83 } 84 85 if !shouldAcceptTag(content.Tags, includeTags, excludeTags) { 86 if Debug { 87 fmt.Printf("route %s %s is ignored due to tag rules\n", content.Method, content.Path) 88 } 89 continue 90 } 91 92 pthObj := tgt.Paths[content.Path] 93 op := setPathOperation( 94 content.Method, content.ID, 95 &pthObj, rp.operations[content.ID]) 96 97 op.Tags = content.Tags 98 99 sp := new(sectionedParser) 100 sp.setTitle = func(lines []string) { op.Summary = joinDropLast(lines) } 101 sp.setDescription = func(lines []string) { op.Description = joinDropLast(lines) } 102 sr := newSetResponses(rp.definitions, rp.responses, opResponsesSetter(op)) 103 spa := newSetParams(rp.parameters, opParamSetter(op)) 104 sp.taggers = []tagParser{ 105 newMultiLineTagParser("Consumes", newMultilineDropEmptyParser(rxConsumes, opConsumesSetter(op)), false), 106 newMultiLineTagParser("Produces", newMultilineDropEmptyParser(rxProduces, opProducesSetter(op)), false), 107 newSingleLineTagParser("Schemes", newSetSchemes(opSchemeSetter(op))), 108 newMultiLineTagParser("Security", newSetSecurity(rxSecuritySchemes, opSecurityDefsSetter(op)), false), 109 newMultiLineTagParser("Parameters", spa, false), 110 newMultiLineTagParser("Responses", sr, false), 111 } 112 if err := sp.Parse(content.Remaining); err != nil { 113 return fmt.Errorf("operation (%s): %v", op.ID, err) 114 } 115 116 if tgt.Paths == nil { 117 tgt.Paths = make(map[string]spec.PathItem) 118 } 119 tgt.Paths[content.Path] = pthObj 120 } 121 122 return nil 123 } 124 125 func shouldAcceptTag(tags []string, includeTags map[string]bool, excludeTags map[string]bool) bool { 126 for _, tag := range tags { 127 if len(includeTags) > 0 { 128 if includeTags[tag] { 129 return true 130 } 131 } else if len(excludeTags) > 0 { 132 if excludeTags[tag] { 133 return false 134 } 135 } 136 } 137 return len(includeTags) <= 0 138 }