-
Notifications
You must be signed in to change notification settings - Fork 0
/
form-mapping.go
224 lines (213 loc) · 6.14 KB
/
form-mapping.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
package just
import (
"errors"
"mime/multipart"
"net/url"
"reflect"
"strconv"
"time"
)
var (
ErrUnknownType = errors.New("unknown type")
ErrBlankTimeFormat = errors.New("blank time format")
ErrOnlyStructUrlEncode = errors.New("array and slice by root element not supported, only structure")
)
func marshalUrlValues(ptr interface{}) ([]byte, error) {
t, v := reflect.TypeOf(ptr).Elem(), reflect.ValueOf(ptr).Elem()
if t.Kind() == reflect.Array || t.Kind() == reflect.Slice {
return nil, ErrOnlyStructUrlEncode
}
values := make(url.Values)
for i := 0; i < t.NumField(); i++ {
typeField, structField := t.Field(i), v.Field(i)
if !structField.CanSet() {
continue
}
structFieldKind, inputFieldName := structField.Kind(), typeField.Tag.Get("form")
if len(inputFieldName) < 1 {
inputFieldName = typeField.Name
}
if structFieldKind == reflect.Slice {
for i := 0; i < structField.Len(); i++ {
values.Add(inputFieldName, structField.Index(i).String())
}
} else {
values.Set(inputFieldName, structField.String())
}
}
return []byte(values.Encode()), nil
}
func mapForm(values map[string][]string, files map[string][]*multipart.FileHeader, ptr interface{}) error {
return recursiveTreeMapForm(values, files, reflect.TypeOf(ptr).Elem(), reflect.ValueOf(ptr).Elem())
}
func recursiveTreeMapForm(values map[string][]string, files map[string][]*multipart.FileHeader, t reflect.Type, v reflect.Value) error {
if t.Kind() == reflect.Array || t.Kind() == reflect.Slice {
return errors.New("Array and slice by root element not supported, only structure!")
}
for i := 0; i < t.NumField(); i++ {
typeField, structField := t.Field(i), v.Field(i)
if !structField.CanSet() {
if typeField.Type.Kind() == reflect.Struct {
if err := recursiveTreeMapForm(values, files, typeField.Type, structField); err != nil {
return err
}
}
continue
}
structFieldKind, inputFieldName := structField.Kind(), typeField.Tag.Get("form")
if len(inputFieldName) < 1 {
inputFieldName = typeField.Name
}
inputValue, existsValue := values[inputFieldName]
if !existsValue {
// Check files
if files != nil && len(files) > 0 {
if list, ok := files[inputFieldName]; ok {
if numFiles := len(list); numFiles > 0 {
if structFieldKind == reflect.Slice {
elemType := structField.Type().Elem()
if elemType.Kind() == reflect.Ptr {
elemType = elemType.Elem()
}
if elemType.Name() == "FileHeader" {
slice := reflect.MakeSlice(structField.Type(), numFiles, numFiles)
for i := 0; i < numFiles; i++ {
slice.Index(i).Set(reflect.ValueOf(list[i]))
}
v.Field(i).Set(slice)
}
} else if structField.Type().Elem().Name() == "FileHeader" {
structField.Set(reflect.ValueOf(list[0]))
}
}
}
}
continue
}
if numElements := len(inputValue); numElements > 0 {
if structFieldKind == reflect.Slice || structFieldKind == reflect.Array {
sliceOf := structField.Type().Elem().Kind()
slice := reflect.MakeSlice(structField.Type(), numElements, numElements)
for i := 0; i < numElements; i++ {
if err := setWithProperType(sliceOf, inputValue[i], slice.Index(i)); err != nil {
return err
}
}
v.Field(i).Set(slice)
} else {
if _, isTime := structField.Interface().(time.Time); isTime {
if err := setTimeField(inputValue[0], typeField, structField); err != nil {
return err
}
continue
}
kind := typeField.Type.Kind()
if kind == reflect.Ptr {
kind = typeField.Type.Elem().Kind()
if structField.IsNil() {
structField.Set(reflect.New(typeField.Type.Elem()))
}
}
if err := setWithProperType(kind, inputValue[0], reflect.Indirect(structField)); err != nil {
return err
}
}
}
}
return nil
}
func setWithProperType(valueKind reflect.Kind, val string, structField reflect.Value) error {
switch valueKind {
case reflect.Int:
return setIntField(val, 0, structField)
case reflect.Int8:
return setIntField(val, 8, structField)
case reflect.Int16:
return setIntField(val, 16, structField)
case reflect.Int32:
return setIntField(val, 32, structField)
case reflect.Int64:
return setIntField(val, 64, structField)
case reflect.Uint:
return setUintField(val, 0, structField)
case reflect.Uint8:
return setUintField(val, 8, structField)
case reflect.Uint16:
return setUintField(val, 16, structField)
case reflect.Uint32:
return setUintField(val, 32, structField)
case reflect.Uint64:
return setUintField(val, 64, structField)
case reflect.Bool:
return setBoolField(val, structField)
case reflect.Float32:
return setFloatField(val, 32, structField)
case reflect.Float64:
return setFloatField(val, 64, structField)
case reflect.String:
structField.SetString(val)
default:
return ErrUnknownType
}
return nil
}
func setIntField(val string, bitSize int, field reflect.Value) error {
if val == "" {
val = "0"
}
intVal, err := strconv.ParseInt(val, 10, bitSize)
if err == nil {
field.SetInt(intVal)
}
return err
}
func setUintField(val string, bitSize int, field reflect.Value) error {
if val == "" {
val = "0"
}
uintVal, err := strconv.ParseUint(val, 10, bitSize)
if err == nil {
field.SetUint(uintVal)
}
return err
}
func setBoolField(val string, field reflect.Value) error {
if val == "" {
val = "false"
}
boolVal, err := strconv.ParseBool(val)
if err == nil {
field.SetBool(boolVal)
}
return nil
}
func setFloatField(val string, bitSize int, field reflect.Value) error {
if val == "" {
val = "0.0"
}
floatVal, err := strconv.ParseFloat(val, bitSize)
if err == nil {
field.SetFloat(floatVal)
}
return err
}
func setTimeField(val string, structField reflect.StructField, value reflect.Value) error {
timeFormat := structField.Tag.Get("time_format")
if timeFormat == "" {
return ErrBlankTimeFormat
}
if val == "" {
value.Set(reflect.ValueOf(time.Time{}))
return nil
}
l := time.Local
if isUTC, _ := strconv.ParseBool(structField.Tag.Get("time_utc")); isUTC {
l = time.UTC
}
t, err := time.ParseInLocation(timeFormat, val, l)
if err != nil {
return err
}
value.Set(reflect.ValueOf(t))
return nil
}