/
pathparams.go
173 lines (146 loc) · 4.53 KB
/
pathparams.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
// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
package utils
import (
"context"
"fmt"
"math/big"
"net/url"
"reflect"
"strings"
"time"
"github.com/ericlagergren/decimal"
"github.com/inkeep/ai-api-go/types"
)
func GenerateURL(_ context.Context, serverURL, path string, pathParams interface{}, globals interface{}) (string, error) {
uri := strings.TrimSuffix(serverURL, "/") + path
parsedParameters := map[string]string{}
globalsAlreadyPopulated, err := populateParsedParameters(pathParams, globals, parsedParameters, []string{})
if err != nil {
return "", err
}
if globals != nil {
_, err = populateParsedParameters(globals, nil, parsedParameters, globalsAlreadyPopulated)
if err != nil {
return "", err
}
}
// TODO should we handle the case where there are no matching path params?
return ReplaceParameters(uri, parsedParameters), nil
}
func populateParsedParameters(pathParams interface{}, globals interface{}, parsedParameters map[string]string, skipFields []string) ([]string, error) {
pathParamsStructType := reflect.TypeOf(pathParams)
pathParamsValType := reflect.ValueOf(pathParams)
globalsAlreadyPopulated := []string{}
for i := 0; i < pathParamsStructType.NumField(); i++ {
fieldType := pathParamsStructType.Field(i)
valType := pathParamsValType.Field(i)
if contains(skipFields, fieldType.Name) {
continue
}
requestTag := getRequestTag(fieldType)
if requestTag != nil {
continue
}
ppTag := parseParamTag(pathParamTagKey, fieldType, "simple", false)
if ppTag == nil {
continue
}
if globals != nil {
var globalFound bool
fieldType, valType, globalFound = populateFromGlobals(fieldType, valType, pathParamTagKey, globals)
if globalFound {
globalsAlreadyPopulated = append(globalsAlreadyPopulated, fieldType.Name)
}
}
if ppTag.Serialization != "" {
vals, err := populateSerializedParams(ppTag, fieldType.Type, valType)
if err != nil {
return nil, err
}
for k, v := range vals {
parsedParameters[k] = url.PathEscape(v)
}
} else {
// TODO: support other styles
switch ppTag.Style {
case "simple":
simpleParams := getSimplePathParams(ppTag.ParamName, fieldType.Type, valType, ppTag.Explode)
for k, v := range simpleParams {
parsedParameters[k] = v
}
}
}
}
return globalsAlreadyPopulated, nil
}
func getSimplePathParams(parentName string, objType reflect.Type, objValue reflect.Value, explode bool) map[string]string {
pathParams := make(map[string]string)
if isNil(objType, objValue) {
return nil
}
if objType.Kind() == reflect.Ptr {
objType = objType.Elem()
objValue = objValue.Elem()
}
switch objType.Kind() {
case reflect.Array, reflect.Slice:
if objValue.Len() == 0 {
return nil
}
var ppVals []string
for i := 0; i < objValue.Len(); i++ {
ppVals = append(ppVals, valToString(objValue.Index(i).Interface()))
}
pathParams[parentName] = strings.Join(ppVals, ",")
case reflect.Map:
if objValue.Len() == 0 {
return nil
}
var ppVals []string
objMap := objValue.MapRange()
for objMap.Next() {
if explode {
ppVals = append(ppVals, fmt.Sprintf("%s=%s", objMap.Key().String(), valToString(objMap.Value().Interface())))
} else {
ppVals = append(ppVals, fmt.Sprintf("%s,%s", objMap.Key().String(), valToString(objMap.Value().Interface())))
}
}
pathParams[parentName] = strings.Join(ppVals, ",")
case reflect.Struct:
switch objValue.Interface().(type) {
case time.Time:
pathParams[parentName] = valToString(objValue.Interface())
case types.Date:
pathParams[parentName] = valToString(objValue.Interface())
case big.Int:
pathParams[parentName] = valToString(objValue.Interface())
case decimal.Big:
pathParams[parentName] = valToString(objValue.Interface())
default:
var ppVals []string
for i := 0; i < objType.NumField(); i++ {
fieldType := objType.Field(i)
valType := objValue.Field(i)
ppTag := parseParamTag(pathParamTagKey, fieldType, "simple", explode)
if ppTag == nil {
continue
}
if isNil(fieldType.Type, valType) {
continue
}
if fieldType.Type.Kind() == reflect.Pointer {
valType = valType.Elem()
}
if explode {
ppVals = append(ppVals, fmt.Sprintf("%s=%s", ppTag.ParamName, valToString(valType.Interface())))
} else {
ppVals = append(ppVals, fmt.Sprintf("%s,%s", ppTag.ParamName, valToString(valType.Interface())))
}
}
pathParams[parentName] = strings.Join(ppVals, ",")
}
default:
pathParams[parentName] = valToString(objValue.Interface())
}
return pathParams
}