-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathparams.go
145 lines (123 loc) · 3.83 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
// 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/speakeasy-sdks/golx_pets/pkg/types"
)
func GenerateURL(ctx context.Context, serverURL, path string, pathParams interface{}, globals map[string]map[string]map[string]interface{}) (string, error) {
uri := strings.TrimSuffix(serverURL, "/") + path
pathParamsStructType := reflect.TypeOf(pathParams)
pathParamsValType := reflect.ValueOf(pathParams)
parsedParameters := map[string]string{}
for i := 0; i < pathParamsStructType.NumField(); i++ {
fieldType := pathParamsStructType.Field(i)
valType := pathParamsValType.Field(i)
requestTag := getRequestTag(fieldType)
if requestTag != nil {
continue
}
ppTag := parseParamTag(pathParamTagKey, fieldType, "simple", false)
if ppTag == nil {
continue
}
valType = populateFromGlobals(fieldType, valType, "pathParam", globals)
if ppTag.Serialization != "" {
vals, err := populateSerializedParams(ppTag, fieldType.Type, valType)
if err != nil {
return "", err
}
for k, v := range vals {
parsedParameters[k] = url.PathEscape(v)
}
} else {
// TODO: support other styles
switch ppTag.Style {
case "simple":
simpleParams := getSimplePathParams(ctx, ppTag.ParamName, fieldType.Type, valType, ppTag.Explode)
for k, v := range simpleParams {
parsedParameters[k] = v
}
}
}
}
// TODO should we handle the case where there are no matching path params?
return ReplaceParameters(uri, parsedParameters), nil
}
func getSimplePathParams(ctx context.Context, 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
}