forked from parvez3019/go-swagger3
/
route.go
47 lines (40 loc) · 1.21 KB
/
route.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package operations
import (
"fmt"
"net/http"
"regexp"
"strings"
oas "github.com/Arlet2/go-swagger3/openApi3Schema"
)
func (p *parser) parseRouteComment(operation *oas.OperationObject, comment string) error {
sourceString := strings.TrimSpace(comment[len("@Router"):])
// /path [method]
re := regexp.MustCompile(`([\w\.\/\-{}]+)[^\[]+\[([^\]]+)`)
matches := re.FindStringSubmatch(sourceString)
if len(matches) != 3 {
return fmt.Errorf("Can not parse router comment \"%s\", skipped", comment)
}
_, ok := p.OpenAPI.Paths[matches[1]]
if !ok {
p.OpenAPI.Paths[matches[1]] = &oas.PathItemObject{}
}
switch strings.ToUpper(matches[2]) {
case http.MethodGet:
p.OpenAPI.Paths[matches[1]].Get = operation
case http.MethodPost:
p.OpenAPI.Paths[matches[1]].Post = operation
case http.MethodPatch:
p.OpenAPI.Paths[matches[1]].Patch = operation
case http.MethodPut:
p.OpenAPI.Paths[matches[1]].Put = operation
case http.MethodDelete:
p.OpenAPI.Paths[matches[1]].Delete = operation
case http.MethodOptions:
p.OpenAPI.Paths[matches[1]].Options = operation
case http.MethodHead:
p.OpenAPI.Paths[matches[1]].Head = operation
case http.MethodTrace:
p.OpenAPI.Paths[matches[1]].Trace = operation
}
return nil
}