-
Notifications
You must be signed in to change notification settings - Fork 0
/
form.go
117 lines (95 loc) · 2.76 KB
/
form.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
// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
package utils
import (
"fmt"
"math/big"
"net/url"
"reflect"
"strings"
"time"
"github.com/ericlagergren/decimal"
"github.com/speakeasy-sdks/oneof-v1/pkg/types"
)
func populateForm(paramName string, explode bool, objType reflect.Type, objValue reflect.Value, delimiter string, getFieldName func(reflect.StructField) string) url.Values {
formValues := url.Values{}
if isNil(objType, objValue) {
return formValues
}
if objType.Kind() == reflect.Pointer {
objType = objType.Elem()
objValue = objValue.Elem()
}
switch objType.Kind() {
case reflect.Struct:
switch objValue.Interface().(type) {
case time.Time:
formValues.Add(paramName, valToString(objValue.Interface()))
case types.Date:
formValues.Add(paramName, valToString(objValue.Interface()))
case big.Int:
formValues.Add(paramName, valToString(objValue.Interface()))
case decimal.Big:
formValues.Add(paramName, valToString(objValue.Interface()))
default:
var items []string
for i := 0; i < objType.NumField(); i++ {
fieldType := objType.Field(i)
valType := objValue.Field(i)
if isNil(fieldType.Type, valType) {
continue
}
if valType.Kind() == reflect.Pointer {
valType = valType.Elem()
}
fieldName := getFieldName(fieldType)
if fieldName == "" {
continue
}
if explode {
formValues.Add(fieldName, valToString(valType.Interface()))
} else {
items = append(items, fmt.Sprintf("%s%s%s", fieldName, delimiter, valToString(valType.Interface())))
}
}
if len(items) > 0 {
formValues.Add(paramName, strings.Join(items, delimiter))
}
}
case reflect.Map:
items := []string{}
iter := objValue.MapRange()
for iter.Next() {
if explode {
formValues.Add(iter.Key().String(), valToString(iter.Value().Interface()))
} else {
items = append(items, fmt.Sprintf("%s%s%s", iter.Key().String(), delimiter, valToString(iter.Value().Interface())))
}
}
if len(items) > 0 {
formValues.Add(paramName, strings.Join(items, delimiter))
}
case reflect.Slice, reflect.Array:
values := parseDelimitedArray(explode, objValue, delimiter)
for _, v := range values {
formValues.Add(paramName, v)
}
default:
formValues.Add(paramName, valToString(objValue.Interface()))
}
return formValues
}
func parseDelimitedArray(explode bool, objValue reflect.Value, delimiter string) []string {
values := []string{}
items := []string{}
for i := 0; i < objValue.Len(); i++ {
if explode {
values = append(values, valToString(objValue.Index(i).Interface()))
} else {
items = append(items, valToString(objValue.Index(i).Interface()))
}
}
if len(items) > 0 {
values = append(values, strings.Join(items, delimiter))
}
return values
}