-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathparameter.go
136 lines (125 loc) · 3.45 KB
/
parameter.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package openapi3
import (
"net/url"
"regexp"
"strings"
oas3 "github.com/getkin/kin-openapi/openapi3"
"github.com/grokify/mogo/encoding/jsonpointer"
"github.com/grokify/mogo/type/maputil"
"github.com/grokify/mogo/type/stringsutil"
)
// ParameterNames covers path template parameters, operation parameter names/referecnes, compoents keys/names
// Parameter path names only. This is useful for viewing and modifying parameter template naems.
type ParameterNames struct {
Components maputil.MapStringSlice
Operations maputil.MapStringSlice
Paths maputil.MapStringSlice
}
func (pn *ParameterNames) Names() []string {
names := map[string]int{}
for name := range pn.Components {
names[name]++
}
for name := range pn.Operations {
names[name]++
}
for name := range pn.Paths {
names[name]++
}
return maputil.StringKeys(names, nil)
}
func NewParameterNames() ParameterNames {
return ParameterNames{
Components: maputil.MapStringSlice{},
Operations: maputil.MapStringSlice{},
Paths: maputil.MapStringSlice{},
}
}
// ParameterPathNames returns a set of parameter names. Parameter names exist in (1) path URLs,
// (2) operation parameters and (3) spec component parameters.
func (sm *SpecMore) ParameterPathNames() ParameterNames {
if sm.Spec == nil {
return NewParameterNames()
}
return ParameterNames{
Components: sm.ParamPathNamesComponents(),
Operations: sm.ParamPathNamesOperations(),
Paths: sm.ParamPathNamesPaths()}
}
func (sm *SpecMore) ParamPathNamesComponents() map[string][]string {
names := url.Values{}
if sm.Spec == nil {
return names
}
for paramKey, paramRef := range sm.Spec.Components.Parameters {
if paramRef.Value == nil {
continue
}
if strings.ToLower(strings.TrimSpace(paramRef.Value.In)) != InPath {
continue
}
if len(paramRef.Value.Name) > 0 {
jpath := jsonpointer.PointerSubEscapeAll(`#/components/parameters/%s/name`, paramKey)
names.Add(paramRef.Value.Name, jpath)
}
}
return names
}
func (sm *SpecMore) ParamPathNamesOperations() map[string][]string {
names := url.Values{}
if sm.Spec == nil {
return names
}
VisitOperations(sm.Spec, func(opPath, opMethod string, op *oas3.Operation) {
if op == nil {
return
}
for i, paramRef := range op.Parameters {
if paramRef.Value == nil {
continue
}
if strings.ToLower(strings.TrimSpace(paramRef.Value.In)) != InPath {
continue
}
jpath := jsonpointer.PointerSubEscapeAll(`#/paths/%s/%s/parameters/%d/name`, opPath, opMethod, i)
names.Add(paramRef.Value.Name, jpath)
}
})
return names
}
func (sm *SpecMore) ParamPathNamesPaths() map[string][]string {
names := url.Values{}
if sm.Spec == nil {
return names
}
pathsMap := sm.Spec.Paths.Map()
for pathURL := range pathsMap {
// for pathURL := range sm.Spec.Paths { // getkin v0.121.0 to v0.122.0
m := PathParams(pathURL)
if len(m) > 0 {
jpath := jsonpointer.PointerSubEscapeAll(`#/paths/%s`, pathURL)
for _, paramName := range m {
names.Add(paramName, jpath)
}
}
}
return names
}
var rxParens = regexp.MustCompile(`{([^}{}]+)}`)
func ParsePathParametersParens(urlPath string) []string {
paramNames := []string{}
m := rxParens.FindAllStringSubmatch(urlPath, -1)
if len(m) == 0 {
return paramNames
}
for _, n := range m {
if len(n) == 2 {
varName := strings.TrimSpace(n[1])
paramNames = append(paramNames, varName)
}
}
if len(paramNames) > 0 {
paramNames = stringsutil.SliceCondenseSpace(paramNames, true, false)
}
return paramNames
}