-
Notifications
You must be signed in to change notification settings - Fork 28
/
path.go
93 lines (83 loc) · 2.2 KB
/
path.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package openapi3
import (
"net/http"
"regexp"
"sort"
oas3 "github.com/getkin/kin-openapi/openapi3"
)
var RxPathParam = regexp.MustCompile(`\{([^\{\}]+)\}`)
func PathParams(p string) []string {
params := []string{}
m := RxPathParam.FindAllStringSubmatch(p, -1)
for _, mi := range m {
params = append(params, mi[1])
}
return params
}
func PathItemHasEndpoints(pathItem *oas3.PathItem) bool {
if pathItem.Connect != nil || pathItem.Delete != nil ||
pathItem.Get != nil || pathItem.Head != nil ||
pathItem.Options != nil ||
pathItem.Patch != nil || pathItem.Post != nil ||
pathItem.Put != nil || pathItem.Trace != nil {
return true
}
return false
}
func PathMethods(pathItem *oas3.PathItem) []string {
methods := []string{}
if pathItem.Connect != nil {
methods = append(methods, http.MethodConnect)
}
if pathItem.Delete != nil {
methods = append(methods, http.MethodDelete)
}
if pathItem.Get != nil {
methods = append(methods, http.MethodGet)
}
if pathItem.Head != nil {
methods = append(methods, http.MethodHead)
}
if pathItem.Options != nil {
methods = append(methods, http.MethodOptions)
}
if pathItem.Patch != nil {
methods = append(methods, http.MethodPatch)
}
if pathItem.Post != nil {
methods = append(methods, http.MethodPost)
}
if pathItem.Put != nil {
methods = append(methods, http.MethodPut)
}
if pathItem.Trace != nil {
methods = append(methods, http.MethodTrace)
}
return methods
}
var rxPathVarToGeneric = regexp.MustCompile(`{[^}{]*}`)
func PathVarsToGeneric(input string) string {
return rxPathVarToGeneric.ReplaceAllString(input, "{}")
}
func PathMatchGeneric(path1, path2 string) bool {
return PathVarsToGeneric(path1) == PathVarsToGeneric(path2)
}
func (sm *SpecMore) PathMethods(generic bool) []string {
endpoints := []string{}
if sm.Spec == nil {
return endpoints
}
pathsMap := sm.Spec.Paths.Map()
for url, pathItem := range pathsMap {
// for url, pathItem := range sm.Spec.Paths { // getkin v0.121.0 to v0.122.0
if generic {
url = PathVarsToGeneric(url)
}
pathMethods := PathMethods(pathItem)
for _, pathMethod := range pathMethods {
endpoints = append(endpoints, url+" "+pathMethod)
}
}
sort.Strings(endpoints)
return endpoints
}