-
Notifications
You must be signed in to change notification settings - Fork 0
/
a_basic_func.go
531 lines (498 loc) · 11.5 KB
/
a_basic_func.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
package cdt
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"time"
)
// isPtr is_prt
func isPtr(refVal reflect.Value) bool {
return refVal.Kind() == reflect.Ptr
}
// isInterface is_interface
func isInterface(refVal reflect.Value) bool {
return refVal.Kind() == reflect.Interface
}
// isString is_string
func isString(refVal reflect.Value) bool {
if isPtr(refVal) {
return isString(refVal.Elem())
}
return refVal.Kind() == reflect.String
}
// isArray is_array
func isArray(refVal reflect.Value) bool {
if isPtr(refVal) {
return isArray(refVal.Elem())
}
return refVal.Kind() == reflect.Slice || refVal.Kind() == reflect.Array
}
// isBytes is_bytes
func isBytes(refVal reflect.Value) bool {
if isPtr(refVal) {
return isBytes(refVal.Elem())
}
return refVal.Kind() == reflect.Slice && refVal.Type().Elem().Kind() == reflect.Uint8
}
// isMap is_map
func isMap(refVal reflect.Value) bool {
if isPtr(refVal) {
return isMap(refVal.Elem())
}
return refVal.Kind() == reflect.Map
}
// isStruct is_struct
func isStruct(refVal reflect.Value) bool {
if isPtr(refVal) {
return isStruct(refVal.Elem())
}
return refVal.Kind() == reflect.Struct
}
// isBoolean is_bool
func isBoolean(refVal reflect.Value) bool {
if isPtr(refVal) {
return isBoolean(refVal.Elem())
}
return refVal.Kind() == reflect.Bool
}
// isInt is_int
func isInt(refVal reflect.Value) bool {
if isPtr(refVal) {
return isInt(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return true
}
return false
}
// isInt8 is_int8
func isInt8(refVal reflect.Value) bool {
if isPtr(refVal) {
return isInt8(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Int8:
return true
}
return false
}
// isInt16 is_int16
func isInt16(refVal reflect.Value) bool {
if isPtr(refVal) {
return isInt16(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Int16:
return true
}
return false
}
// isInt32 is_int32
func isInt32(refVal reflect.Value) bool {
if isPtr(refVal) {
return isInt32(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Int32:
return true
}
return false
}
// isInt64 is_int64
func isInt64(refVal reflect.Value) bool {
if isPtr(refVal) {
return isInt64(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Int64:
return true
}
return false
}
// isUint is_uint
func isUint(refVal reflect.Value) bool {
if isPtr(refVal) {
return isUint(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return true
}
return false
}
func isUint8(refVal reflect.Value) bool {
if isPtr(refVal) {
return isUint8(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Uint8:
return true
}
return false
}
// isUint16 is_uint16
func isUint16(refVal reflect.Value) bool {
if isPtr(refVal) {
return isUint16(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Uint16:
return true
}
return false
}
// isUint32 is_uint32
func isUint32(refVal reflect.Value) bool {
if isPtr(refVal) {
return isUint32(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Uint32:
return true
}
return false
}
// isUint64 is_uint64
func isUint64(refVal reflect.Value) bool {
if isPtr(refVal) {
return isUint64(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Uint64:
return true
}
return false
}
// isUintptr is_uintptr
func isUintptr(refVal reflect.Value) bool {
if isPtr(refVal) {
return isUintptr(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Uintptr:
return true
}
return false
}
// isFloat is_float
func isFloat(refVal reflect.Value) bool {
if isPtr(refVal) {
return isFloat(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Float32, reflect.Float64:
return true
}
return false
}
// isFloat32 is_float32
func isFloat32(refVal reflect.Value) bool {
if isPtr(refVal) {
return isFloat(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Float32:
return true
}
return false
}
// isFloat64 is_float64
func isFloat64(refVal reflect.Value) bool {
if isPtr(refVal) {
return isFloat(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Float64:
return true
}
return false
}
// isNil is_nil
func isNil(refVal reflect.Value) bool {
if isPtr(refVal) {
return isNil(refVal.Elem())
}
return refVal.Kind() == reflect.Invalid
}
// isTime is_time
func isTime(refVal reflect.Value) bool {
if isPtr(refVal) {
return isTime(refVal.Elem())
}
if refVal.Kind() == reflect.Struct {
timeType := reflect.TypeOf(time.Time{})
return refVal.Type().AssignableTo(timeType)
}
return false
}
// IsNumeric is_numeric()
// Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential part.
// Thus +0123.45e6 is a valid numeric value.
// In PHP hexadecimal (e.g. 0xf4c3b00c) is not supported, but IsNumeric is supported.
func isNumeric(refVal reflect.Value) bool {
switch refVal.Kind() {
case reflect.Ptr:
return isNumeric(refVal.Elem())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Float32, reflect.Float64:
return true
case reflect.Slice, reflect.String:
var str string
switch refVal.Kind() {
case reflect.Slice:
if isBytes(refVal) {
str = string(refVal.Bytes())
}
break
case reflect.String:
str = refVal.String()
break
}
if str == "" {
return false
}
// Trim any whitespace
str = strings.TrimSpace(str)
if str[0] == '-' || str[0] == '+' {
if len(str) == 1 {
return false
}
str = str[1:]
}
// hex
if len(str) > 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X') {
for _, h := range str[2:] {
if !((h >= '0' && h <= '9') || (h >= 'a' && h <= 'f') || (h >= 'A' && h <= 'F')) {
return false
}
}
return true
}
// 0-9, Point, Scientific
p, s, l := 0, 0, len(str)
for i, v := range str {
if v == '.' { // Point
if p > 0 || s > 0 || i+1 == l {
return false
}
p = i
} else if v == 'e' || v == 'E' { // Scientific
if i == 0 || s > 0 || i+1 == l {
return false
}
s = i
} else if v < '0' || v > '9' {
return false
}
}
return true
}
return false
}
// toFloat64 function to convert other type data to float64
func toFloat64(refVal reflect.Value) (float64, error) {
switch refVal.Kind() {
case reflect.Invalid:
return 0, nil
case reflect.Ptr:
return toFloat64(refVal.Elem())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return float64(refVal.Uint()), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return float64(refVal.Int()), nil
case reflect.Float32, reflect.Float64:
return refVal.Float(), nil
case reflect.String:
if isNumeric(refVal) {
return json.Number(strings.TrimSpace(refVal.String())).Float64()
} else {
return 0, newError(refVal)
}
case reflect.Slice:
if isBytes(refVal) && isNumeric(refVal) {
return json.Number(refVal.Bytes()).Float64()
} else {
return 0, newError(refVal)
}
case reflect.Bool:
if refVal.Bool() {
return 1, nil
} else {
return 0, nil
}
default:
return 0, newError(refVal)
}
}
// toBoolean function to convert other type data to boolean
func toBoolean(refVal reflect.Value) (bool, error) {
var val any
switch refVal.Kind() {
case reflect.Invalid:
return false, nil
case reflect.Ptr:
return toBoolean(refVal.Elem())
case reflect.String:
// val to lower
val = strings.ToLower(refVal.String())
break
case reflect.Slice:
if isBytes(refVal) {
val = strings.ToLower(string(refVal.Bytes()))
break
}
return false, newError(refVal)
default:
val = refVal.Interface()
if isInt(refVal) {
valInt, _ := toFloat64(refVal)
val = int(valInt)
}
}
switch val {
case nil:
return false, nil
case "1", "t", "true", 1, true:
return true, nil
case "0", "f", "false", 0, false:
return false, nil
default:
return false, newError(refVal)
}
}
// toString function to convert other type data to string
func toString(refVal reflect.Value) (string, error) {
switch refVal.Kind() {
case reflect.Invalid:
return "", nil
case reflect.Ptr:
return toString(refVal.Elem())
case reflect.String:
return refVal.String(), nil
case reflect.Slice:
if isBytes(refVal) {
return string(refVal.Bytes()), nil
}
b, err := json.Marshal(refVal.Interface())
if err != nil {
return "", err
}
return string(b), nil
case reflect.Bool:
return fmt.Sprintf("%v", refVal.Interface()), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Float32, reflect.Float64:
return fmt.Sprintf("%v", refVal.Interface()), nil
case reflect.Map, reflect.Struct, reflect.Interface, reflect.Array:
b, err := json.Marshal(refVal.Interface())
if err != nil {
return "", err
}
return string(b), nil
default:
return "", newError(refVal)
}
}
// toTime function to convert other type data to time
func toTime(refVal reflect.Value) (time.Time, error) {
if !isTime(refVal) {
return time.Time{}, newError(refVal)
}
if isPtr(refVal) {
return toTime(refVal.Elem())
}
var val any
val = refVal.Interface()
switch val.(type) {
case time.Time:
return val.(time.Time), nil
default:
return time.Time{}, newError(refVal)
}
}
// toArray function to convert other type data to array
func toArray(refVal reflect.Value) ([]any, error) {
switch refVal.Kind() {
case reflect.Invalid:
return nil, nil
case reflect.Ptr:
return toArray(refVal.Elem())
case reflect.Slice:
if isBytes(refVal) {
return toArray(reflect.ValueOf(string(refVal.Bytes())))
}
return nil, newError(refVal)
case reflect.Array:
var arr []any
for i := 0; i < refVal.Len(); i++ {
currentIndex := refVal.Index(i)
if currentIndex.CanInterface() {
arr = append(arr, currentIndex.Interface())
} else {
arr = append(arr, nil)
}
}
return arr, nil
case reflect.String:
var arr []any
if err := json.Unmarshal([]byte(refVal.String()), &arr); err != nil {
return nil, err
}
return arr, nil
}
return nil, newError(refVal)
}
// toMap function to convert other type data to map
func toMap(refVal reflect.Value) (map[string]any, error) {
switch refVal.Kind() {
case reflect.Invalid:
return nil, nil
case reflect.Ptr:
return toMap(refVal.Elem())
case reflect.Slice:
if isBytes(refVal) {
return toMap(reflect.ValueOf(string(refVal.Bytes())))
}
return nil, newError(refVal)
case reflect.Struct:
var m = make(map[string]any)
numFieldLen := refVal.NumField()
for i := 0; i < numFieldLen; i++ {
currentFieldIndex := refVal.Field(i)
if currentFieldIndex.CanInterface() {
m[refVal.Type().Field(i).Name] = currentFieldIndex.Interface()
} else {
m[refVal.Type().Field(i).Name] = nil
}
}
return m, nil
case reflect.Map:
var m = make(map[string]any)
mapKeys := refVal.MapKeys()
for _, key := range mapKeys {
currentMapIndex := refVal.MapIndex(key)
if currentMapIndex.CanInterface() {
m[key.String()] = currentMapIndex.Interface()
} else {
m[key.String()] = nil
}
}
return m, nil
case reflect.String:
var m = make(map[string]any)
if err := json.Unmarshal([]byte(refVal.String()), &m); err != nil {
return nil, err
}
return m, nil
}
return nil, newError(refVal)
}
// newError function to create new error
func newError(refVal reflect.Value) error {
return fmt.Errorf("unable to casting type %T", refVal.Interface())
}