-
Notifications
You must be signed in to change notification settings - Fork 0
/
structs_data_conv.go
453 lines (431 loc) · 8.2 KB
/
structs_data_conv.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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package structs
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
)
var (
emptyStrMap = map[string]struct{}{
"": struct{}{},
"0": struct{}{},
"off": struct{}{},
"false": struct{}{},
"False": struct{}{},
}
)
type iString interface {
String() string
}
func anyToString(v interface{}, def ...string) string {
var defValue = ""
if len(def) > 0 {
defValue = def[0]
}
if v == nil {
return defValue
}
switch value := v.(type) {
case int:
return strconv.FormatInt(int64(value), 10)
case int8:
return strconv.Itoa(int(value))
case int16:
return strconv.Itoa(int(value))
case int32:
return strconv.Itoa(int(value))
case int64:
return strconv.FormatInt(int64(value), 10)
case uint:
return strconv.FormatUint(uint64(value), 10)
case uint8:
return strconv.FormatUint(uint64(value), 10)
case uint16:
return strconv.FormatUint(uint64(value), 10)
case uint32:
return strconv.FormatUint(uint64(value), 10)
case uint64:
return strconv.FormatUint(uint64(value), 10)
case float32:
return strconv.FormatFloat(float64(value), 'f', -1, 32)
case float64:
return strconv.FormatFloat(value, 'f', -1, 64)
case bool:
return strconv.FormatBool(value)
case string:
return value
case []byte:
return string(value)
case []rune:
return string(value)
default:
if f, ok := value.(iString); ok {
return f.String()
} else if f, ok := value.(error); ok {
return f.Error()
} else if f, ok := value.(error); ok {
return fmt.Sprintf("error:%s", f.Error())
} else {
if jsonContent, err := json.Marshal(value); err != nil {
return fmt.Sprint(value)
} else {
return string(jsonContent)
}
}
}
}
// Int converts <i> to int.
func anyToInt(i interface{}, def ...int) int {
var defValue = 0
if len(def) > 0 {
defValue = def[0]
}
if i == nil {
return defValue
}
if v, ok := i.(int); ok {
return v
}
return int(anyToInt64(i, anyToInt64(defValue)))
}
// Int8 converts <i> to int8.
func anyToInt8(i interface{}, def ...int8) int8 {
var defV int8 = 0
if len(def) > 0 {
defV = def[0]
}
if i == nil {
return defV
}
if v, ok := i.(int8); ok {
return v
}
return int8(anyToInt64(i, anyToInt64(defV)))
}
// Int16 converts <i> to int16.
func anyToInt16(i interface{}, def ...int16) int16 {
var defV int16 = 0
if len(def) > 0 {
defV = def[0]
}
if i == nil {
return defV
}
if v, ok := i.(int16); ok {
return v
}
return int16(anyToInt64(i, anyToInt64(defV)))
}
// Int32 converts <i> to int32.
func anyToInt32(i interface{}, def ...int32) int32 {
var defV int32 = 0
if len(def) > 0 {
defV = def[0]
}
if i == nil {
return defV
}
if v, ok := i.(int32); ok {
return v
}
return int32(anyToInt64(i, anyToInt64(defV)))
}
func anyToInt64(v interface{}, def ...int64) int64 {
var defVaue int64 = 0
if len(def) > 0 {
defVaue = def[0]
}
if v == nil {
return defVaue
}
switch value := v.(type) {
case int:
return int64(value)
case int8:
return int64(value)
case int16:
return int64(value)
case int32:
return int64(value)
case int64:
return value
case uint:
return int64(value)
case uint8:
return int64(value)
case uint16:
return int64(value)
case uint32:
return int64(value)
case uint64:
return int64(value)
case float32:
return int64(value)
case float64:
return int64(value)
case bool:
if value {
return 1
}
return 0
case []byte:
return decodeToInt64(value)
default:
s := anyToString(value)
// Hexadecimal
if len(s) > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X') {
if v, e := strconv.ParseInt(s[2:], 16, 64); e == nil {
return v
}
}
// Octal
if len(s) > 1 && s[0] == '0' {
if v, e := strconv.ParseInt(s[1:], 8, 64); e == nil {
return v
}
}
// Decimal
if v, e := strconv.ParseInt(s, 10, 64); e == nil {
return v
}
// Float64
return int64(anyToFloat64(value))
}
}
// Uint converts <i> to uint.
func anyToUint(i interface{}, def ...uint) uint {
var defV uint = 0
if len(def) > 0 {
defV = def[0]
}
if i == nil {
return defV
}
if v, ok := i.(uint); ok {
return v
}
return uint(anyToUint64(i, anyToUint64(defV)))
}
// Uint8 converts <i> to uint8.
func anyToUint8(i interface{}, def ...uint8) uint8 {
var defV uint8 = 0
if len(def) > 0 {
defV = def[0]
}
if i == nil {
return defV
}
if v, ok := i.(uint8); ok {
return v
}
return uint8(anyToUint64(i, anyToUint64(defV)))
}
// Uint16 converts <i> to uint16.
func anyToUint16(i interface{}, def ...uint16) uint16 {
var defV uint16 = 0
if len(def) > 0 {
defV = def[0]
}
if i == nil {
return defV
}
if v, ok := i.(uint16); ok {
return v
}
return uint16(anyToUint64(i, uint64(defV)))
}
// Uint32 converts <i> to uint32.
func anyToUint32(i interface{}, def ...uint32) uint32 {
var defV uint32 = 0
if len(def) > 0 {
defV = def[0]
}
if i == nil {
return defV
}
if v, ok := i.(uint32); ok {
return v
}
return uint32(anyToUint64(i, uint64(defV)))
}
// Uint64 converts <i> to uint64.
func anyToUint64(i interface{}, def ...uint64) uint64 {
var defV uint64 = 0
if len(def) > 0 {
defV = def[0]
}
if i == nil {
return defV
}
switch value := i.(type) {
case int:
return uint64(value)
case int8:
return uint64(value)
case int16:
return uint64(value)
case int32:
return uint64(value)
case int64:
return uint64(value)
case uint:
return uint64(value)
case uint8:
return uint64(value)
case uint16:
return uint64(value)
case uint32:
return uint64(value)
case uint64:
return value
case float32:
return uint64(value)
case float64:
return uint64(value)
case bool:
if value {
return 1
}
return 0
case []byte:
return decodeToUint64(value)
default:
s := anyToString(value)
// Hexadecimal
if len(s) > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X') {
if v, e := strconv.ParseUint(s[2:], 16, 64); e == nil {
return v
}
}
// Octal
if len(s) > 1 && s[0] == '0' {
if v, e := strconv.ParseUint(s[1:], 8, 64); e == nil {
return v
}
}
// Decimal
if v, e := strconv.ParseUint(s, 10, 64); e == nil {
return v
}
// Float64
return uint64(anyToFloat64(value))
}
}
func anyToFloat(i interface{}, def ...float32) float32 {
return anyToFloat32(i, def...)
}
// Float32 converts <i> to float32.
func anyToFloat32(i interface{}, def ...float32) float32 {
var defV float32 = 0
if len(def) > 0 {
defV = def[0]
}
if i == nil {
return defV
}
switch value := i.(type) {
case float32:
return value
case float64:
return float32(value)
case []byte:
return decodeToFloat32(value)
default:
v, _ := strconv.ParseFloat(anyToString(i), 64)
return float32(v)
}
}
// Float64 converts <i> to float64.
func anyToFloat64(i interface{}, def ...float64) float64 {
var defValue float64 = 0
if len(def) > 0 {
defValue = def[0]
}
if i == nil {
return defValue
}
switch value := i.(type) {
case float32:
return float64(value)
case float64:
return value
case []byte:
return decodeToFloat64(value)
default:
v, _ := strconv.ParseFloat(anyToString(i), 64)
return v
}
}
// Byte converts <i> to byte.
func anyToByte(i interface{}) byte {
if v, ok := i.(byte); ok {
return v
}
return byte(anyToUint8(i))
}
// Bytes converts <i> to []byte.
func anyToBytes(i interface{}) []byte {
if i == nil {
return nil
}
switch value := i.(type) {
case string:
return []byte(value)
case []byte:
return value
default:
return encode(i)
}
}
// Rune converts <i> to rune.
func anyToRune(i interface{}) rune {
if v, ok := i.(rune); ok {
return v
}
return rune(anyToInt32(i))
}
// Runes converts <i> to []rune.
func anyToRunes(i interface{}) []rune {
if v, ok := i.([]rune); ok {
return v
}
return []rune(anyToString(i))
}
func anyToBool(i interface{}, def ...bool) bool {
bDef := false
if len(def) > 0 {
bDef = def[0]
}
if i == nil {
return bDef
}
switch value := i.(type) {
case bool:
return value
case string:
if _, ok := emptyStrMap[value]; ok {
return bDef
}
return true
default:
rv := reflect.ValueOf(i)
switch rv.Kind() {
case reflect.Ptr:
return !rv.IsNil()
case reflect.Map:
fallthrough
case reflect.Array:
fallthrough
case reflect.Slice:
return rv.Len() != 0
case reflect.Struct:
return true
default:
s := anyToString(i)
if _, ok := emptyStrMap[s]; ok {
return bDef
}
return true
}
}
}