github.com/rzurga/go-swagger@v0.28.1-0.20211109195225-5d1f453ffa3a/scan/path.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 "go/ast" 21 "regexp" 22 "strings" 23 24 "github.com/go-openapi/spec" 25 ) 26 27 type parsedPathContent struct { 28 Method, Path, ID string 29 Tags []string 30 Remaining *ast.CommentGroup 31 } 32 33 func parsePathAnnotation(annotation *regexp.Regexp, lines []*ast.Comment) (cnt parsedPathContent) { 34 var justMatched bool 35 36 for _, cmt := range lines { 37 for _, line := range strings.Split(cmt.Text, "\n") { 38 matches := annotation.FindStringSubmatch(line) 39 if len(matches) > 3 { 40 cnt.Method, cnt.Path, cnt.ID = matches[1], matches[2], matches[len(matches)-1] 41 cnt.Tags = rxSpace.Split(matches[3], -1) 42 if len(matches[3]) == 0 { 43 cnt.Tags = nil 44 } 45 justMatched = true 46 } else if cnt.Method != "" { 47 if cnt.Remaining == nil { 48 cnt.Remaining = new(ast.CommentGroup) 49 } 50 if !justMatched || strings.TrimSpace(rxStripComments.ReplaceAllString(line, "")) != "" { 51 cc := new(ast.Comment) 52 cc.Slash = cmt.Slash 53 cc.Text = line 54 cnt.Remaining.List = append(cnt.Remaining.List, cc) 55 justMatched = false 56 } 57 } 58 } 59 } 60 61 return 62 } 63 64 func setPathOperation(method, id string, pthObj *spec.PathItem, op *spec.Operation) *spec.Operation { 65 if op == nil { 66 op = new(spec.Operation) 67 op.ID = id 68 } 69 70 switch strings.ToUpper(method) { 71 case "GET": 72 if pthObj.Get != nil { 73 if id == pthObj.Get.ID { 74 op = pthObj.Get 75 } else { 76 pthObj.Get = op 77 } 78 } else { 79 pthObj.Get = op 80 } 81 82 case "POST": 83 if pthObj.Post != nil { 84 if id == pthObj.Post.ID { 85 op = pthObj.Post 86 } else { 87 pthObj.Post = op 88 } 89 } else { 90 pthObj.Post = op 91 } 92 93 case "PUT": 94 if pthObj.Put != nil { 95 if id == pthObj.Put.ID { 96 op = pthObj.Put 97 } else { 98 pthObj.Put = op 99 } 100 } else { 101 pthObj.Put = op 102 } 103 104 case "PATCH": 105 if pthObj.Patch != nil { 106 if id == pthObj.Patch.ID { 107 op = pthObj.Patch 108 } else { 109 pthObj.Patch = op 110 } 111 } else { 112 pthObj.Patch = op 113 } 114 115 case "HEAD": 116 if pthObj.Head != nil { 117 if id == pthObj.Head.ID { 118 op = pthObj.Head 119 } else { 120 pthObj.Head = op 121 } 122 } else { 123 pthObj.Head = op 124 } 125 126 case "DELETE": 127 if pthObj.Delete != nil { 128 if id == pthObj.Delete.ID { 129 op = pthObj.Delete 130 } else { 131 pthObj.Delete = op 132 } 133 } else { 134 pthObj.Delete = op 135 } 136 137 case "OPTIONS": 138 if pthObj.Options != nil { 139 if id == pthObj.Options.ID { 140 op = pthObj.Options 141 } else { 142 pthObj.Options = op 143 } 144 } else { 145 pthObj.Options = op 146 } 147 } 148 149 return op 150 }