-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
230 lines (192 loc) · 4.89 KB
/
utils.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
// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
package utils
import (
"context"
"fmt"
"io"
"math/big"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"github.com/ericlagergren/decimal"
)
const (
queryParamTagKey = "queryParam"
headerParamTagKey = "header"
pathParamTagKey = "pathParam"
)
var (
paramRegex = regexp.MustCompile(`({.*?})`)
SerializationMethodToContentType = map[string]string{
"json": "application/json",
"form": "application/x-www-form-urlencoded",
"multipart": "multipart/form-data",
"raw": "application/octet-stream",
"string": "text/plain",
}
)
func UnmarshalJsonFromResponseBody(body io.Reader, out interface{}, tag string) error {
data, err := io.ReadAll(body)
if err != nil {
return fmt.Errorf("error reading response body: %w", err)
}
if err := UnmarshalJSON(data, out, reflect.StructTag(tag), true, false); err != nil {
return fmt.Errorf("error unmarshalling json response body: %w", err)
}
return nil
}
func ReplaceParameters(stringWithParams string, params map[string]string) string {
if len(params) == 0 {
return stringWithParams
}
return paramRegex.ReplaceAllStringFunc(stringWithParams, func(match string) string {
match = match[1 : len(match)-1]
return params[match]
})
}
func Contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}
func MatchStatusCodes(expectedCodes []string, statusCode int) bool {
for _, codeStr := range expectedCodes {
code, err := strconv.Atoi(codeStr)
if err == nil {
if code == statusCode {
return true
}
continue
}
codeRange, err := strconv.Atoi(string(codeStr[0]))
if err != nil {
continue
}
if statusCode >= (codeRange*100) && statusCode < ((codeRange+1)*100) {
return true
}
}
return false
}
func AsSecuritySource(security interface{}) func(context.Context) (interface{}, error) {
return func(context.Context) (interface{}, error) {
return security, nil
}
}
func parseStructTag(tagKey string, field reflect.StructField) map[string]string {
tag := field.Tag.Get(tagKey)
if tag == "" {
return nil
}
values := map[string]string{}
options := strings.Split(tag, ",")
for _, optionConf := range options {
parts := strings.Split(optionConf, "=")
switch len(parts) {
case 1:
// flag option
parts = append(parts, "true")
case 2:
// key=value option
default:
// invalid option
continue
}
values[parts[0]] = parts[1]
}
return values
}
func parseParamTag(tagKey string, field reflect.StructField, defaultStyle string, defaultExplode bool) *paramTag {
// example `{tagKey}:"style=simple,explode=false,name=apiID"`
values := parseStructTag(tagKey, field)
if values == nil {
return nil
}
tag := ¶mTag{
Style: defaultStyle,
Explode: defaultExplode,
ParamName: strings.ToLower(field.Name),
}
for k, v := range values {
switch k {
case "style":
tag.Style = v
case "explode":
tag.Explode = v == "true"
case "name":
tag.ParamName = v
case "serialization":
tag.Serialization = v
}
}
return tag
}
func valToString(val interface{}) string {
switch v := val.(type) {
case time.Time:
return v.Format(time.RFC3339Nano)
case big.Int:
return v.String()
case decimal.Big:
return v.String()
default:
return fmt.Sprintf("%v", v)
}
}
func populateFromGlobals(fieldType reflect.StructField, valType reflect.Value, paramType string, globals interface{}) (reflect.StructField, reflect.Value, bool) {
if globals == nil {
return fieldType, valType, false
}
globalsStruct := reflect.TypeOf(globals)
globalsStructVal := reflect.ValueOf(globals)
globalsField, found := globalsStruct.FieldByName(fieldType.Name)
if !found {
return fieldType, valType, false
}
if fieldType.Type.Kind() != reflect.Ptr || !valType.IsNil() {
return fieldType, valType, true
}
globalsVal := globalsStructVal.FieldByName(fieldType.Name)
if !globalsVal.IsValid() {
return fieldType, valType, false
}
switch paramType {
case queryParamTagKey:
qpTag := parseQueryParamTag(globalsField)
if qpTag == nil {
return fieldType, valType, false
}
default:
tag := parseParamTag(paramType, fieldType, "simple", false)
if tag == nil {
return fieldType, valType, false
}
}
return globalsField, globalsVal, true
}
func isNil(typ reflect.Type, val reflect.Value) bool {
// `reflect.TypeOf(nil) == nil` so calling typ.Kind() will cause a nil pointer
// dereference panic. Catch it and return early.
// https://github.com/golang/go/issues/51649
// https://github.com/golang/go/issues/54208
if typ == nil {
return true
}
if typ.Kind() == reflect.Ptr || typ.Kind() == reflect.Map || typ.Kind() == reflect.Slice || typ.Kind() == reflect.Interface {
return val.IsNil()
}
return false
}
func contains(arr []string, str string) bool {
for _, a := range arr {
if a == str {
return true
}
}
return false
}