-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconverter.go
More file actions
372 lines (329 loc) · 9.94 KB
/
converter.go
File metadata and controls
372 lines (329 loc) · 9.94 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package peony
import (
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"os"
"reflect"
"sort"
"strconv"
"strings"
)
type Convert func(*Params, string, reflect.Type) reflect.Value
type ReverseConvert func(map[string]string, string, interface{})
type Convertor struct {
Convert Convert
ReverseConvert ReverseConvert
}
type Convertors struct {
KindConvertors map[reflect.Kind]*Convertor
TypeConvertors map[reflect.Type]*Convertor
}
func ValueConvertor(convert func(v string, typ reflect.Type) reflect.Value, reverseConvert ReverseConvert) *Convertor {
return &Convertor{func(p *Params, name string, typ reflect.Type) reflect.Value {
vals, ok := p.Values[name]
if !ok || len(vals) == 0 {
return reflect.Zero(typ)
}
return convert(vals[0], typ)
}, reverseConvert}
}
func StringConvert(v string, typ reflect.Type) reflect.Value {
return reflect.ValueOf(v)
}
func IntConvert(value string, typ reflect.Type) reflect.Value {
iValue, err := strconv.ParseInt(value, 10, 64)
if err != nil {
WARN.Printf("can't convert \"%s\" to int\n", value)
return reflect.Zero(typ)
}
val := reflect.New(typ)
val.Elem().SetInt(iValue)
return val.Elem()
}
func UintConvert(value string, typ reflect.Type) reflect.Value {
iValue, err := strconv.ParseUint(value, 10, 64)
if err != nil {
WARN.Printf("can't convert \"%s\" to uint\n", value)
return reflect.Zero(typ)
}
val := reflect.New(typ)
val.Elem().SetUint(iValue)
return val.Elem()
}
func FloatConvert(value string, typ reflect.Type) reflect.Value {
fValue, err := strconv.ParseFloat(value, 64)
if err != nil {
WARN.Printf("can't convert \"%s\" to float\n", value)
return reflect.Zero(typ)
}
val := reflect.New(typ)
val.Elem().SetFloat(fValue)
return val.Elem()
}
func BoolConvert(value string, typ reflect.Type) reflect.Value {
fValue, err := strconv.ParseBool(value)
if err != nil {
WARN.Printf("can't convert \"%s\" to bool\n", value)
return reflect.Zero(typ)
}
val := reflect.New(typ)
val.Elem().SetBool(fValue)
return val.Elem()
}
func nextKey(key string) string {
fieldLen := strings.IndexAny(key, ".[")
if fieldLen == -1 {
return key
}
return key[:fieldLen]
}
func getMultipartFile(p *Params, n string) multipart.File {
for _, fileH := range p.Files[n] {
file, err := fileH.Open()
if err != nil {
WARN.Println("open file header error,", err)
return nil
}
return file
}
return nil
}
func ReaderConvert(p *Params, n string, typ reflect.Type) reflect.Value {
file := getMultipartFile(p, n)
if file == nil {
return reflect.Zero(typ)
}
return reflect.ValueOf(file)
}
func ByteSliceConvert(p *Params, n string, typ reflect.Type) reflect.Value {
file := getMultipartFile(p, n)
if file == nil {
return reflect.Zero(typ)
}
bs, err := ioutil.ReadAll(file)
if err != nil {
WARN.Println("read all multipart file error,", err)
return reflect.Zero(typ)
}
return reflect.ValueOf(bs)
}
func FileConvert(p *Params, n string, typ reflect.Type) reflect.Value {
file := getMultipartFile(p, n)
if file == nil {
return reflect.Zero(typ)
}
if osFile, ok := file.(*os.File); ok {
return reflect.ValueOf(osFile)
}
//store temp file
osFile, err := ioutil.TempFile("", "peony-upload-file")
if err != nil {
WARN.Println("create temp file error,", err)
return reflect.Zero(typ)
}
p.tmpFiles = append(p.tmpFiles, osFile)
_, err = io.Copy(osFile, file)
if err != nil {
WARN.Println("save data to temp file error,", err)
return reflect.Zero(typ)
}
_, err = osFile.Seek(0, 0)
if err != nil {
WARN.Println("seek to begin of temp file error,", err)
return reflect.Zero(typ)
}
return reflect.ValueOf(osFile)
}
type item struct {
index int
value reflect.Value
}
type ItemByIndex []*item
func (a ItemByIndex) Len() int {
return len(a)
}
func (a ItemByIndex) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a ItemByIndex) Less(i, j int) bool {
return a[i].index < a[j].index
}
func GetSliceConvert(c *Convertors) func(*Params, string, reflect.Type) reflect.Value {
return func(p *Params, name string, typ reflect.Type) reflect.Value {
var values ItemByIndex
var maxIdx = 0
var noindex = 0
processItem := func(key string, vals []string) {
var idx int
var err error
if !strings.HasPrefix(key, name+"[") {
return
}
lIdx, rIdx := len(name), strings.IndexByte(key[len(name):], ']')+len(name)
if rIdx == -1 {
//not have ] char in key
return
}
//process e.g. name[]
if lIdx == rIdx-1 {
noindex++
goto END
}
idx, err = strconv.Atoi(key[lIdx+1 : rIdx])
if err != nil {
return
}
if idx > maxIdx {
maxIdx = idx
}
END:
value := c.Convert(p, key[:rIdx+1], typ.Elem())
values = append(values, &item{idx, value})
}
for k, vals := range p.Values {
processItem(k, vals)
}
//if array len small than 10000, keep index
if maxIdx < 10000 {
slice := reflect.MakeSlice(typ, maxIdx+1, maxIdx+noindex+1)
for _, val := range values {
if val.index > -1 {
slice.Index(val.index).Set(val.value)
} else {
slice = reflect.Append(slice, val.value)
}
}
return slice
}
sort.Sort(values)
slice := reflect.MakeSlice(typ, 0, len(values))
for _, val := range values {
slice = reflect.Append(slice, val.value)
}
return slice
}
}
//covert struct
func GetStructConvert(c *Convertors) Convert {
return func(p *Params, n string, typ reflect.Type) reflect.Value {
result := reflect.New(typ).Elem()
fieldValues := make(map[string]reflect.Value)
for key, _ := range p.Values {
if !strings.HasPrefix(key, n+".") {
continue
}
suffix := key[len(n)+1:]
fieldName := nextKey(suffix)
fieldLen := len(fieldName)
//convert the field
if _, ok := fieldValues[fieldName]; !ok {
fieldValue := result.FieldByName(fieldName)
if !fieldValue.IsValid() {
WARN.Println("W: bindStruct: Field not found:", fieldName)
continue
}
if !fieldValue.CanSet() {
WARN.Println("W: bindStruct: Field not settable:", fieldName)
continue
}
convertVal := c.Convert(p, key[:len(n)+1+fieldLen], fieldValue.Type())
fieldValue.Set(convertVal)
fieldValues[fieldName] = convertVal
}
}
return result
}
}
func GetSliceReverseConvert(c *Convertors) func(p map[string]string, name string, v interface{}) {
return func(p map[string]string, name string, v interface{}) {
slice := reflect.ValueOf(v)
for i := 0; i < slice.Len(); i++ {
c.ReverseConvert(p, fmt.Sprintf("%s[%d]", name, i), slice.Index(i).Interface())
}
}
}
func GetStructReverseConvert(c *Convertors) ReverseConvert {
return func(p map[string]string, name string, v interface{}) {
val := reflect.ValueOf(v)
typ := val.Type()
for i := 0; i < val.NumField(); i++ {
structField := typ.Field(i)
fieldValue := val.Field(i)
if structField.PkgPath == "" {
c.ReverseConvert(p, fmt.Sprintf("%s.%s", name, structField.Name), fieldValue.Interface())
}
}
}
}
func IntReverseConvert(p map[string]string, name string, v interface{}) {
p[name] = fmt.Sprintf("%d", v)
}
func StringReverseConvert(p map[string]string, name string, v interface{}) {
p[name] = fmt.Sprintf("%s", v)
}
func FloatReverseConvert(p map[string]string, name string, v interface{}) {
p[name] = fmt.Sprintf("%f", v)
}
func BoolReverseConvert(p map[string]string, name string, v interface{}) {
p[name] = fmt.Sprintf("%b", v)
}
func NewConvertors() *Convertors {
c := &Convertors{
KindConvertors: map[reflect.Kind]*Convertor{},
TypeConvertors: map[reflect.Type]*Convertor{},
}
c.KindConvertors[reflect.Int] = ValueConvertor(IntConvert, IntReverseConvert)
c.KindConvertors[reflect.Int8] = ValueConvertor(IntConvert, IntReverseConvert)
c.KindConvertors[reflect.Int16] = ValueConvertor(IntConvert, IntReverseConvert)
c.KindConvertors[reflect.Int32] = ValueConvertor(IntConvert, IntReverseConvert)
c.KindConvertors[reflect.Int64] = ValueConvertor(IntConvert, IntReverseConvert)
c.KindConvertors[reflect.Uint] = ValueConvertor(UintConvert, IntReverseConvert)
c.KindConvertors[reflect.Uint8] = ValueConvertor(UintConvert, IntReverseConvert)
c.KindConvertors[reflect.Uint16] = ValueConvertor(UintConvert, IntReverseConvert)
c.KindConvertors[reflect.Uint32] = ValueConvertor(UintConvert, IntReverseConvert)
c.KindConvertors[reflect.Uint64] = ValueConvertor(UintConvert, IntReverseConvert)
c.KindConvertors[reflect.Float32] = ValueConvertor(FloatConvert, FloatReverseConvert)
c.KindConvertors[reflect.Float64] = ValueConvertor(FloatConvert, FloatReverseConvert)
c.KindConvertors[reflect.Bool] = ValueConvertor(BoolConvert, BoolReverseConvert)
c.KindConvertors[reflect.String] = ValueConvertor(StringConvert, StringReverseConvert)
c.KindConvertors[reflect.Slice] = &Convertor{GetSliceConvert(c), GetSliceReverseConvert(c)}
c.KindConvertors[reflect.Struct] = &Convertor{
GetStructConvert(c),
GetStructReverseConvert(c),
}
c.KindConvertors[reflect.Ptr] = &Convertor{
func(p *Params, name string, typ reflect.Type) reflect.Value {
return c.Convert(p, name, typ.Elem()).Addr()
},
func(p map[string]string, name string, v interface{}) {
c.ReverseConvert(p, name, reflect.ValueOf(v).Elem().Interface())
},
}
c.TypeConvertors[reflect.TypeOf((*io.Reader)(nil)).Elem()] = &Convertor{ReaderConvert, nil}
c.TypeConvertors[reflect.TypeOf((*io.ReadWriter)(nil)).Elem()] = &Convertor{ReaderConvert, nil}
c.TypeConvertors[reflect.TypeOf((*os.File)(nil))] = &Convertor{FileConvert, nil}
c.TypeConvertors[reflect.TypeOf((*[]byte)(nil)).Elem()] = &Convertor{ByteSliceConvert, nil}
return c
}
func (c *Convertors) Convert(p *Params, name string, typ reflect.Type) reflect.Value {
converter := c.TypeConvertors[typ]
if converter == nil {
converter = c.KindConvertors[typ.Kind()]
}
if converter != nil {
return converter.Convert(p, name, typ)
}
return reflect.Zero(typ)
}
func (c *Convertors) ReverseConvert(p map[string]string, name string, val interface{}) {
typ := reflect.TypeOf(val)
converter := c.TypeConvertors[typ]
if converter == nil {
converter = c.KindConvertors[typ.Kind()]
}
if converter != nil {
converter.ReverseConvert(p, name, val)
}
}