github.com/djarvur/go-swagger@v0.18.0/scan/path.go (about) 1 // Copyright 2015 go-swagger maintainers 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package scan 16 17 import ( 18 "go/ast" 19 "regexp" 20 "strings" 21 22 "github.com/go-openapi/spec" 23 ) 24 25 type parsedPathContent struct { 26 Method, Path, ID string 27 Tags []string 28 Remaining *ast.CommentGroup 29 } 30 31 func parsePathAnnotation(annotation *regexp.Regexp, lines []*ast.Comment) (cnt parsedPathContent) { 32 var justMatched bool 33 34 for _, cmt := range lines { 35 for _, line := range strings.Split(cmt.Text, "\n") { 36 matches := annotation.FindStringSubmatch(line) 37 if len(matches) > 3 { 38 cnt.Method, cnt.Path, cnt.ID = matches[1], matches[2], matches[len(matches)-1] 39 cnt.Tags = rxSpace.Split(matches[3], -1) 40 if len(matches[3]) == 0 { 41 cnt.Tags = nil 42 } 43 justMatched = true 44 } else if cnt.Method != "" { 45 if cnt.Remaining == nil { 46 cnt.Remaining = new(ast.CommentGroup) 47 } 48 if !justMatched || strings.TrimSpace(rxStripComments.ReplaceAllString(line, "")) != "" { 49 cc := new(ast.Comment) 50 cc.Slash = cmt.Slash 51 cc.Text = line 52 cnt.Remaining.List = append(cnt.Remaining.List, cc) 53 justMatched = false 54 } 55 } 56 } 57 } 58 59 return 60 } 61 62 func setPathOperation(method, id string, pthObj *spec.PathItem, op *spec.Operation) *spec.Operation { 63 if op == nil { 64 op = new(spec.Operation) 65 op.ID = id 66 } 67 68 switch strings.ToUpper(method) { 69 case "GET": 70 if pthObj.Get != nil { 71 if id == pthObj.Get.ID { 72 op = pthObj.Get 73 } else { 74 pthObj.Get = op 75 } 76 } else { 77 pthObj.Get = op 78 } 79 80 case "POST": 81 if pthObj.Post != nil { 82 if id == pthObj.Post.ID { 83 op = pthObj.Post 84 } else { 85 pthObj.Post = op 86 } 87 } else { 88 pthObj.Post = op 89 } 90 91 case "PUT": 92 if pthObj.Put != nil { 93 if id == pthObj.Put.ID { 94 op = pthObj.Put 95 } else { 96 pthObj.Put = op 97 } 98 } else { 99 pthObj.Put = op 100 } 101 102 case "PATCH": 103 if pthObj.Patch != nil { 104 if id == pthObj.Patch.ID { 105 op = pthObj.Patch 106 } else { 107 pthObj.Patch = op 108 } 109 } else { 110 pthObj.Patch = op 111 } 112 113 case "HEAD": 114 if pthObj.Head != nil { 115 if id == pthObj.Head.ID { 116 op = pthObj.Head 117 } else { 118 pthObj.Head = op 119 } 120 } else { 121 pthObj.Head = op 122 } 123 124 case "DELETE": 125 if pthObj.Delete != nil { 126 if id == pthObj.Delete.ID { 127 op = pthObj.Delete 128 } else { 129 pthObj.Delete = op 130 } 131 } else { 132 pthObj.Delete = op 133 } 134 135 case "OPTIONS": 136 if pthObj.Options != nil { 137 if id == pthObj.Options.ID { 138 op = pthObj.Options 139 } else { 140 pthObj.Options = op 141 } 142 } else { 143 pthObj.Options = op 144 } 145 } 146 147 return op 148 }