-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
parameter_utilities.go
335 lines (309 loc) · 10.2 KB
/
parameter_utilities.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package helpers
import (
"fmt"
"github.com/pb33f/libopenapi/datamodel/high/base"
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
"net/http"
"slices"
"strconv"
"strings"
)
// QueryParam is a struct that holds the key, values and property name for a query parameter
// it's used for complex query types that need to be parsed and tracked differently depending
// on the encoding styles used.
type QueryParam struct {
Key string
Values []string
Property string
}
// ExtractParamsForOperation will extract the parameters for the operation based on the request method.
// Both the path level params and the method level params will be returned.
func ExtractParamsForOperation(request *http.Request, item *v3.PathItem) []*v3.Parameter {
params := item.Parameters
switch request.Method {
case http.MethodGet:
if item.Get != nil {
params = append(params, item.Get.Parameters...)
}
case http.MethodPost:
if item.Post != nil {
params = append(params, item.Post.Parameters...)
}
case http.MethodPut:
if item.Put != nil {
params = append(params, item.Put.Parameters...)
}
case http.MethodDelete:
if item.Delete != nil {
params = append(params, item.Delete.Parameters...)
}
case http.MethodOptions:
if item.Options != nil {
params = append(params, item.Options.Parameters...)
}
case http.MethodHead:
if item.Head != nil {
params = append(params, item.Head.Parameters...)
}
case http.MethodPatch:
if item.Patch != nil {
params = append(params, item.Patch.Parameters...)
}
case http.MethodTrace:
if item.Trace != nil {
params = append(params, item.Trace.Parameters...)
}
}
return params
}
// ExtractSecurityForOperation will extract the security requirements for the operation based on the request method.
func ExtractSecurityForOperation(request *http.Request, item *v3.PathItem) []*base.SecurityRequirement {
var schemes []*base.SecurityRequirement
switch request.Method {
case http.MethodGet:
if item.Get != nil {
schemes = append(schemes, item.Get.Security...)
}
case http.MethodPost:
if item.Post != nil {
schemes = append(schemes, item.Post.Security...)
}
case http.MethodPut:
if item.Put != nil {
schemes = append(schemes, item.Put.Security...)
}
case http.MethodDelete:
if item.Delete != nil {
schemes = append(schemes, item.Delete.Security...)
}
case http.MethodOptions:
if item.Options != nil {
schemes = append(schemes, item.Options.Security...)
}
case http.MethodHead:
if item.Head != nil {
schemes = append(schemes, item.Head.Security...)
}
case http.MethodPatch:
if item.Patch != nil {
schemes = append(schemes, item.Patch.Security...)
}
case http.MethodTrace:
if item.Trace != nil {
schemes = append(schemes, item.Trace.Security...)
}
}
return schemes
}
func cast(v string) any {
if v == "true" || v == "false" {
b, _ := strconv.ParseBool(v)
return b
}
if i, err := strconv.ParseFloat(v, 64); err == nil {
// check if this is an int or not
if !strings.Contains(v, Period) {
iv, _ := strconv.ParseInt(v, 10, 64)
return iv
}
return i
}
return v
}
// ConstructParamMapFromDeepObjectEncoding will construct a map from the query parameters that are encoded as
// deep objects. It's kind of a crazy way to do things, but hey, each to their own.
func ConstructParamMapFromDeepObjectEncoding(values []*QueryParam, sch *base.Schema) map[string]interface{} {
// deepObject encoding is a technique used to encode objects into query parameters. Kinda nuts.
decoded := make(map[string]interface{})
for _, v := range values {
if decoded[v.Key] == nil {
props := make(map[string]interface{})
rawValues := make([]interface{}, len(v.Values))
for i := range v.Values {
rawValues[i] = cast(v.Values[i])
}
// check if the schema for the param is an array
if sch != nil && slices.Contains(sch.Type, Array) {
props[v.Property] = rawValues
}
// check if schema has additional properties defined as an array
if sch != nil && sch.AdditionalProperties != nil &&
sch.AdditionalProperties.IsA() &&
slices.Contains(sch.AdditionalProperties.A.Schema().Type, Array) {
props[v.Property] = rawValues
}
if len(props) == 0 {
props[v.Property] = cast(v.Values[0])
}
decoded[v.Key] = props
} else {
added := false
rawValues := make([]interface{}, len(v.Values))
for i := range v.Values {
rawValues[i] = cast(v.Values[i])
}
// check if the schema for the param is an array
if sch != nil && slices.Contains(sch.Type, Array) {
decoded[v.Key].(map[string]interface{})[v.Property] = rawValues
added = true
}
// check if schema has additional properties defined as an array
if sch != nil && sch.AdditionalProperties != nil &&
sch.AdditionalProperties.IsA() &&
slices.Contains(sch.AdditionalProperties.A.Schema().Type, Array) {
decoded[v.Key].(map[string]interface{})[v.Property] = rawValues
added = true
}
if !added {
decoded[v.Key].(map[string]interface{})[v.Property] = cast(v.Values[0])
}
}
}
return decoded
}
// ConstructParamMapFromQueryParamInput will construct a param map from an existing map of *QueryParam slices.
func ConstructParamMapFromQueryParamInput(values map[string][]*QueryParam) map[string]interface{} {
decoded := make(map[string]interface{})
for _, q := range values {
for _, v := range q {
decoded[v.Key] = cast(v.Values[0])
}
}
return decoded
}
// ConstructParamMapFromPipeEncoding will construct a map from the query parameters that are encoded as
// pipe separated values. Perhaps the most sane way to delimit/encode properties.
func ConstructParamMapFromPipeEncoding(values []*QueryParam) map[string]interface{} {
// Pipes are always a good alternative to commas, personally I think they're better, if I were encoding, I would
// use pipes instead of commas, so much can go wrong with a comma, but a pipe? hardly ever.
decoded := make(map[string]interface{})
for _, v := range values {
props := make(map[string]interface{})
// explode PSV into array
exploded := strings.Split(v.Values[0], Pipe)
for i := range exploded {
if i%2 == 0 {
props[exploded[i]] = cast(exploded[i+1])
}
}
decoded[v.Key] = props
}
return decoded
}
// ConstructParamMapFromSpaceEncoding will construct a map from the query parameters that are encoded as
// space delimited values. This is perhaps the worst way to delimit anything other than a paragraph of text.
func ConstructParamMapFromSpaceEncoding(values []*QueryParam) map[string]interface{} {
// Don't use spaces to delimit anything unless you really know what the hell you're doing. Perhaps the
// easiest way to blow something up, unless you're tokenizing strings... don't do this.
decoded := make(map[string]interface{})
for _, v := range values {
props := make(map[string]interface{})
// explode SSV into array
exploded := strings.Split(v.Values[0], Space)
for i := range exploded {
if i%2 == 0 {
props[exploded[i]] = cast(exploded[i+1])
}
}
decoded[v.Key] = props
}
return decoded
}
// ConstructMapFromCSV will construct a map from a comma separated value string.
func ConstructMapFromCSV(csv string) map[string]interface{} {
decoded := make(map[string]interface{})
// explode SSV into array
exploded := strings.Split(csv, Comma)
for i := range exploded {
if i%2 == 0 {
if len(exploded) == i+1 {
break
}
decoded[exploded[i]] = cast(exploded[i+1])
}
}
return decoded
}
// ConstructKVFromCSV will construct a map from a comma separated value string that denotes key value pairs.
func ConstructKVFromCSV(values string) map[string]interface{} {
props := make(map[string]interface{})
exploded := strings.Split(values, Comma)
for i := range exploded {
obK := strings.Split(exploded[i], Equals)
if len(obK) == 2 {
props[obK[0]] = cast(obK[1])
}
}
return props
}
// ConstructKVFromLabelEncoding will construct a map from a comma separated value string that denotes key value pairs.
func ConstructKVFromLabelEncoding(values string) map[string]interface{} {
props := make(map[string]interface{})
exploded := strings.Split(values, Period)
for i := range exploded {
obK := strings.Split(exploded[i], Equals)
if len(obK) == 2 {
props[obK[0]] = cast(obK[1])
}
}
return props
}
// ConstructKVFromMatrixCSV will construct a map from a comma separated value string that denotes key value pairs.
func ConstructKVFromMatrixCSV(values string) map[string]interface{} {
props := make(map[string]interface{})
exploded := strings.Split(values, SemiColon)
for i := range exploded {
obK := strings.Split(exploded[i], Equals)
if len(obK) == 2 {
props[obK[0]] = cast(obK[1])
}
}
return props
}
// ConstructParamMapFromFormEncodingArray will construct a map from the query parameters that are encoded as
// form encoded values.
func ConstructParamMapFromFormEncodingArray(values []*QueryParam) map[string]interface{} {
decoded := make(map[string]interface{})
for _, v := range values {
props := make(map[string]interface{})
// explode SSV into array
exploded := strings.Split(v.Values[0], Comma)
for i := range exploded {
if i%2 == 0 {
props[exploded[i]] = cast(exploded[i+1])
}
}
decoded[v.Key] = props
}
return decoded
}
// DoesFormParamContainDelimiter will determine if a form parameter contains a delimiter.
func DoesFormParamContainDelimiter(value, style string) bool {
if strings.Contains(value, Comma) && (style == "" || style == Form) {
return true
}
return false
}
// ExplodeQueryValue will explode a query value based on the style (space, pipe, or form/default).
func ExplodeQueryValue(value, style string) []string {
switch style {
case SpaceDelimited:
return strings.Split(value, Space)
case PipeDelimited:
return strings.Split(value, Pipe)
default:
return strings.Split(value, Comma)
}
}
func CollapseCSVIntoFormStyle(key string, value string) string {
return fmt.Sprintf("&%s=%s", key,
strings.Join(strings.Split(value, ","), fmt.Sprintf("&%s=", key)))
}
func CollapseCSVIntoSpaceDelimitedStyle(key string, values []string) string {
return fmt.Sprintf("%s=%s", key, strings.Join(values, "%20"))
}
func CollapseCSVIntoPipeDelimitedStyle(key string, values []string) string {
return fmt.Sprintf("%s=%s", key, strings.Join(values, Pipe))
}