-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdt.go
829 lines (785 loc) · 23.5 KB
/
cdt.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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
package cdt
import (
"fmt"
"reflect"
"strings"
"time"
)
type convert struct {
ctdStructTagKV map[string]string
OriginVal any
}
type DataRaw struct {
convert
}
// NewConvert function to create new convert instance
func NewConvert(originVal any) *DataRaw {
t := new(DataRaw)
t.OriginVal = originVal
return t
}
// Set function to set origin data
func (i *convert) Set(val any) {
i.OriginVal = val
}
// SetCtdStructTagKV function to set ctd struct tag kv
func (i *convert) SetCtdStructTagKV(kv map[string]string) {
i.ctdStructTagKV = kv
}
// GetOriginVal return origin value
func (i *convert) GetOriginVal() any {
return i.OriginVal
}
// GetOriginValRef return origin value reference
func (i *convert) GetOriginValRef() reflect.Value {
return reflect.ValueOf(i.OriginVal)
}
// 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 (i *convert) IsNumeric() bool {
return isNumeric(i.GetOriginValRef())
}
// IsPtr is_prt
func (i *convert) IsPtr() bool {
return isPtr(i.GetOriginValRef())
}
// IsString is_string
func (i *convert) IsString() bool {
return isString(i.GetOriginValRef())
}
// IsArray is_array
func (i *convert) IsArray() bool {
return isArray(i.GetOriginValRef())
}
// IsBytes is_bytes
func (i *convert) IsBytes() bool {
return isBytes(i.GetOriginValRef())
}
// IsMap is_map
func (i *convert) IsMap() bool {
return isMap(i.GetOriginValRef())
}
// IsStruct is_struct
func (i *convert) IsStruct() bool {
return isStruct(i.GetOriginValRef())
}
// IsBoolean is_bool
func (i *convert) IsBoolean() bool {
return isBoolean(i.GetOriginValRef())
}
// IsInt is_int
func (i *convert) IsInt() bool {
return isInt(i.GetOriginValRef())
}
// IsInt8 is_int8
func (i *convert) IsInt8() bool {
return isInt8(i.GetOriginValRef())
}
// IsInt16 is_int16
func (i *convert) IsInt16() bool {
return isInt16(i.GetOriginValRef())
}
// IsInt32 is_int32
func (i *convert) IsInt32() bool {
return isInt32(i.GetOriginValRef())
}
// IsInt64 is_int64
func (i *convert) IsInt64() bool {
return isInt64(i.GetOriginValRef())
}
// IsUint is_uint
func (i *convert) IsUint() bool {
return isUint(i.GetOriginValRef())
}
// IsUint8 is_uint8
func (i *convert) IsUint8() bool {
return isUint8(i.GetOriginValRef())
}
// IsUint16 is_uint16
func (i *convert) IsUint16() bool {
return isUint16(i.GetOriginValRef())
}
// IsUint32 is_uint32
func (i *convert) IsUint32() bool {
return isUint32(i.GetOriginValRef())
}
// IsUint64 is_uint64
func (i *convert) IsUint64() bool {
return isUint64(i.GetOriginValRef())
}
// IsUintptr is_uintptr
func (i *convert) IsUintptr() bool {
return isUintptr(i.GetOriginValRef())
}
// IsFloat is_float
func (i *convert) IsFloat() bool {
return isFloat(i.GetOriginValRef())
}
// IsFloat32 is_float32
func (i *convert) IsFloat32() bool {
return isFloat32(i.GetOriginValRef())
}
// IsFloat64 is_float64
func (i *convert) IsFloat64() bool {
return isFloat64(i.GetOriginValRef())
}
// IsNil is_null
func (i *convert) IsNil() bool {
return isNil(i.GetOriginValRef())
}
// IsTime is_time
func (i *convert) IsTime() bool {
return isTime(i.GetOriginValRef())
}
// CopyVariable copy variable
func CopyVariable(data any) (newData any) {
defer func() {
if err := recover(); err != nil {
newData = data
}
}()
refVal := reflect.ValueOf(data)
if refVal.Kind() == reflect.Invalid {
newData = data
} else {
// copy data to newData
copyRef := reflect.New(reflect.TypeOf(data).Elem())
copyRef.Elem().Set(reflect.ValueOf(data).Elem())
newData = copyRef.Interface()
}
return
}
// ctdStructTagKV struct tag option
type ctdStructTagKV map[string]string
// =====================================================
const ctdStructTag = "cdt"
const ctdStructTagParamSeparator = "="
const ctdStructTagParamInterval = ","
// example: `cdt:"type=string,time_format='2006-01-02 15:04:05'"`
// example: `cdt:"type=string,time_format=2006-01-02 15:04:05"`
// example: `cdt:"type=string,time_format=\"2006-01-02 15:04:05\""`
// example: `cdt:"type=string,time_format=` if input `time_format` tag ,but not set time_format value or `time_format` check is empty ,than set time_format `2006-01-02 15:04:05`
// example: `cdt:"type=string"`
// example: `cdt:"type=int"`
// example: `cdt:"type=float"`
// =====================================================
const ctdStructTagParamNameType = "type"
const ctdStructTagParamNameTimeFormat = "time_format"
// =====================================================
const ctdStructTagParamNameTypeString = "string"
const ctdStructTagParamNameTypeUInt = "uint"
const ctdStructTagParamNameTypeUInt8 = "uint8"
const ctdStructTagParamNameTypeUInt16 = "uint16"
const ctdStructTagParamNameTypeUInt32 = "uint32"
const ctdStructTagParamNameTypeUInt64 = "uint64"
const ctdStructTagParamNameTypeUIntPtr = "uintptr"
const ctdStructTagParamNameTypeInt = "int"
const ctdStructTagParamNameTypeInt8 = "int8"
const ctdStructTagParamNameTypeInt16 = "int16"
const ctdStructTagParamNameTypeInt32 = "int32"
const ctdStructTagParamNameTypeInt64 = "int64"
const ctdStructTagParamNameTypeFloat = "float"
const ctdStructTagParamNameTypeFloat32 = "float32"
const ctdStructTagParamNameTypeFloat64 = "float64"
const ctdStructTagParamNameTypeBoolean = "boolean"
const ctdStructTagParamNameTypeNull = "null"
const ctdStructTagParamNameTypeArray = "array"
const ctdStructTagParamNameTypeObject = "object"
const ctdStructTagParamNameTypeMap = "map"
const ctdStructTagParamNameTypeTime = "time"
// =====================================================
const ctdStructTagParamValueDefaultFormat = "2006-01-02 15:04:05"
// EncodeDataForTags encode struct data type value, with struct tags `cdt`
func EncodeDataForTags(v any) {
defer func() {
if err := recover(); err != nil {
fmt.Println("EncodeDataForTags:", err)
}
}()
if v == nil {
return
}
var value reflect.Value
var typ reflect.Type
switch v.(type) {
case reflect.Value:
value = v.(reflect.Value)
typ = value.Type()
break
default:
value = reflect.ValueOf(v)
typ = value.Type()
break
}
if typ.Kind() == reflect.Ptr || typ.Kind() == reflect.Interface {
EncodeDataForTags(value.Elem())
return
}
if typ.Kind() != reflect.Struct || value.Interface() == nil {
return
}
switch value.Interface().(type) {
case DataRaw, *DataRaw:
if !value.CanAddr() {
return
}
sourceDataRawPtr := value.Addr().Interface().(*DataRaw)
if len(sourceDataRawPtr.ctdStructTagKV) <= 0 {
return
}
encodeDataForCdtTagKV(value, sourceDataRawPtr.ctdStructTagKV)
return
}
//
for i := 0; i < typ.NumField(); i++ {
currentFieldValueRef := value.Field(i)
if isPtr(currentFieldValueRef) || isInterface(currentFieldValueRef) {
for isPtr(currentFieldValueRef) || isInterface(currentFieldValueRef) {
currentFieldValueRef = currentFieldValueRef.Elem()
}
}
currentFieldValue := currentFieldValueRef.Interface()
sourceDataRawPtr := new(DataRaw)
switch currentFieldValue.(type) {
case DataRaw:
if !currentFieldValueRef.CanAddr() {
continue
}
sourceDataRawPtr = currentFieldValueRef.Addr().Interface().(*DataRaw)
break
default:
if currentFieldValueRef.Kind() == reflect.Struct {
// other struct
EncodeDataForTags(currentFieldValueRef)
return
}
continue
}
var configTagKV ctdStructTagKV
// check has set custom tag
if len(sourceDataRawPtr.ctdStructTagKV) > 0 {
// with custom tag
configTagKV = sourceDataRawPtr.ctdStructTagKV
} else {
// no custom tag, that parse struct tag
configTagKV = parseConfigCdtTagToKV(typ.Field(i))
}
//
if len(configTagKV) == 0 {
// no tag options
continue
}
// each tag kv
encodeDataForCdtTagKV(currentFieldValueRef, configTagKV)
}
}
// DecodeDataForTags decode struct data type value, with struct tags `cdt`
func DecodeDataForTags(v any) {
defer func() {
if err := recover(); err != nil {
fmt.Println("DecodeDataForTags:", err)
}
}()
if v == nil {
return
}
var value reflect.Value
var typ reflect.Type
switch v.(type) {
case reflect.Value:
value = v.(reflect.Value)
typ = value.Type()
break
default:
value = reflect.ValueOf(v)
typ = value.Type()
break
}
if typ.Kind() == reflect.Ptr || typ.Kind() == reflect.Interface {
DecodeDataForTags(value.Elem())
return
}
if typ.Kind() != reflect.Struct || value.Interface() == nil {
return
}
switch value.Interface().(type) {
case DataRaw, *DataRaw:
if !value.CanAddr() {
return
}
sourceDataRawPtr := value.Addr().Interface().(*DataRaw)
if len(sourceDataRawPtr.ctdStructTagKV) <= 0 {
return
}
decodeDataForCdtTagKV(value, sourceDataRawPtr.ctdStructTagKV)
return
}
for i := 0; i < typ.NumField(); i++ {
currentFieldValueRef := value.Field(i)
if isPtr(currentFieldValueRef) || isInterface(currentFieldValueRef) {
for isPtr(currentFieldValueRef) || isInterface(currentFieldValueRef) {
currentFieldValueRef = currentFieldValueRef.Elem()
}
}
currentFieldValue := currentFieldValueRef.Interface()
sourceDataRawPtr := new(DataRaw)
switch currentFieldValue.(type) {
case DataRaw:
if !currentFieldValueRef.CanAddr() {
return
}
sourceDataRawPtr = currentFieldValueRef.Addr().Interface().(*DataRaw)
break
default:
if currentFieldValueRef.Kind() == reflect.Struct {
// other struct
DecodeDataForTags(currentFieldValueRef)
return
}
continue
}
var configTagKV ctdStructTagKV
// check has set custom tag
if len(sourceDataRawPtr.ctdStructTagKV) > 0 {
// with custom tag
configTagKV = sourceDataRawPtr.ctdStructTagKV
} else {
// no custom tag, that parse struct tag
configTagKV = parseConfigCdtTagToKV(typ.Field(i))
}
//
if len(configTagKV) == 0 {
// no tag options
continue
}
// each tag kv
decodeDataForCdtTagKV(currentFieldValueRef, configTagKV)
}
}
// encodeDataForCdtTagKV encode data for tag kv
func encodeDataForCdtTagKV(currentFieldValueRef reflect.Value, configTagKV ctdStructTagKV) {
if !currentFieldValueRef.CanAddr() {
return
}
sourceDataRawPtr := currentFieldValueRef.Addr().Interface().(*DataRaw)
for configTagKeyStr, configTagValueStr := range configTagKV {
switch strings.ToLower(configTagKeyStr) {
case ctdStructTagParamNameType:
// has set type tag
switch strings.ToLower(configTagValueStr) {
case ctdStructTagParamNameTypeString:
// data type is string
if sourceDataRawPtr.IsNil() || sourceDataRawPtr.IsString() {
continue
}
convertStrValue, convertStrValueErr := sourceDataRawPtr.ToStringE()
if convertStrValueErr != nil {
continue
}
sourceDataRawPtr.Set(convertStrValue)
break
case ctdStructTagParamNameTypeUInt, ctdStructTagParamNameTypeUInt8, ctdStructTagParamNameTypeUInt16, ctdStructTagParamNameTypeUInt32, ctdStructTagParamNameTypeUInt64, ctdStructTagParamNameTypeUIntPtr,
ctdStructTagParamNameTypeInt, ctdStructTagParamNameTypeInt8, ctdStructTagParamNameTypeInt16, ctdStructTagParamNameTypeInt32, ctdStructTagParamNameTypeInt64:
// data type is uint || int
if sourceDataRawPtr.IsNil() || sourceDataRawPtr.IsInt() {
continue
}
convertInt64Value, convertInt64ValueErr := sourceDataRawPtr.ToInt64E()
if convertInt64ValueErr != nil {
continue
}
sourceDataRawPtr.Set(convertInt64Value)
break
case ctdStructTagParamNameTypeFloat, ctdStructTagParamNameTypeFloat32, ctdStructTagParamNameTypeFloat64:
// data type is float
if sourceDataRawPtr.IsNil() || sourceDataRawPtr.IsFloat() {
continue
}
convertFloat64Value, convertFloat64ValueErr := sourceDataRawPtr.ToFloat64E()
if convertFloat64ValueErr != nil {
continue
}
sourceDataRawPtr.Set(convertFloat64Value)
break
case ctdStructTagParamNameTypeBoolean:
// data type is boolean
if sourceDataRawPtr.IsNil() || sourceDataRawPtr.IsBoolean() {
continue
}
convertBooleanValue, convertBooleanValueErr := sourceDataRawPtr.ToBooleanE()
if convertBooleanValueErr != nil {
continue
}
sourceDataRawPtr.Set(convertBooleanValue)
break
case ctdStructTagParamNameTypeNull:
// data type is null
if sourceDataRawPtr.IsNil() {
continue
}
sourceDataRawPtr.Set(nil)
break
case ctdStructTagParamNameTypeTime:
// data type is time
// check sourceDataRawPtr data value is time.Time
if sourceDataRawPtr.IsNil() {
continue
}
if sourceDataRawPtr.IsString() || sourceDataRawPtr.IsBytes() {
timeLayoutFormatStr, timeLayoutFormatStrHasExists := configTagKV[ctdStructTagParamNameTimeFormat]
if !timeLayoutFormatStrHasExists {
continue
}
if len(timeLayoutFormatStr) == 0 {
timeLayoutFormatStr = ctdStructTagParamValueDefaultFormat
}
convertStrValue, convertStrValueErr := sourceDataRawPtr.ToStringE()
if convertStrValueErr != nil {
continue
}
parsedTimeFormat, parsedTimeFormatErr := time.Parse(timeLayoutFormatStr, convertStrValue)
if parsedTimeFormatErr != nil {
// parse time with default time format
parsedTimeFormat, parsedTimeFormatErr = time.Parse(time.RFC3339Nano, convertStrValue)
}
if parsedTimeFormatErr != nil {
continue
}
sourceDataRawPtr.Set(parsedTimeFormat.Format(timeLayoutFormatStr))
}
if !sourceDataRawPtr.IsTime() {
continue
}
convertTimeValue, convertTimeValueErr := sourceDataRawPtr.ToTimeE()
if convertTimeValueErr != nil {
continue
}
timeLayoutFormatStr, timeLayoutFormatStrHasExists := configTagKV[ctdStructTagParamNameTimeFormat]
if !timeLayoutFormatStrHasExists {
continue
}
if len(timeLayoutFormatStr) == 0 {
timeLayoutFormatStr = ctdStructTagParamValueDefaultFormat
}
sourceDataRawPtr.Set(convertTimeValue.Format(timeLayoutFormatStr))
break
case ctdStructTagParamNameTypeArray, ctdStructTagParamNameTypeObject, ctdStructTagParamNameTypeMap:
// data type is array || object
if sourceDataRawPtr.IsNil() {
continue
}
if (sourceDataRawPtr.IsArray() || sourceDataRawPtr.IsStruct() || sourceDataRawPtr.IsMap()) && !sourceDataRawPtr.IsBytes() {
continue
}
convertStrValue, convertStrValueErr := sourceDataRawPtr.ToStringE()
if convertStrValueErr != nil {
continue
}
sourceDataRawPtr.Set(convertStrValue)
break
}
break
}
}
}
// decodeDataForCdtTagKV decode data for tag kv
func decodeDataForCdtTagKV(currentFieldValueRef reflect.Value, configTagKV ctdStructTagKV) {
if !currentFieldValueRef.CanAddr() {
return
}
sourceDataRawPtr := currentFieldValueRef.Addr().Interface().(*DataRaw)
// each tag kv
for configTagKeyStr, configTagValueStr := range configTagKV {
switch strings.ToLower(configTagKeyStr) {
case ctdStructTagParamNameType:
// has set type tag
switch strings.ToLower(configTagValueStr) {
case ctdStructTagParamNameTypeString:
// tag type is string, need convert data value to string
if sourceDataRawPtr.IsNil() || sourceDataRawPtr.IsString() {
continue
}
convertStrValue, convertStrValueErr := sourceDataRawPtr.ToStringE()
if convertStrValueErr != nil {
continue
}
sourceDataRawPtr.Set(convertStrValue)
break
case ctdStructTagParamNameTypeUInt:
// tag type is uint, need convert data value to uint
if sourceDataRawPtr.IsNil() {
continue
}
convertUIntValue, convertUIntValueErr := sourceDataRawPtr.ToUintE()
if convertUIntValueErr != nil {
continue
}
sourceDataRawPtr.Set(convertUIntValue)
break
case ctdStructTagParamNameTypeUInt8:
// tag type is uint8, need convert data value to uint8
if sourceDataRawPtr.IsNil() {
continue
}
convertUint8Value, convertUint8ValueErr := sourceDataRawPtr.ToUint8E()
if convertUint8ValueErr != nil {
continue
}
sourceDataRawPtr.Set(convertUint8Value)
break
case ctdStructTagParamNameTypeUInt16:
// tag type is uint16, need convert data value to uint16
if sourceDataRawPtr.IsNil() {
continue
}
convertUint16Value, convertUint16ValueErr := sourceDataRawPtr.ToUint16E()
if convertUint16ValueErr != nil {
continue
}
sourceDataRawPtr.Set(convertUint16Value)
break
case ctdStructTagParamNameTypeUInt32:
// tag type is uint32, need convert data value to uint32
if sourceDataRawPtr.IsNil() {
continue
}
convertUint32Value, convertUint32ValueErr := sourceDataRawPtr.ToUint32E()
if convertUint32ValueErr != nil {
continue
}
sourceDataRawPtr.Set(convertUint32Value)
break
case ctdStructTagParamNameTypeUInt64:
// tag type is uint64, need convert data value to uint64
if sourceDataRawPtr.IsNil() {
continue
}
convertUint64Value, convertUint64ValueErr := sourceDataRawPtr.ToUint64E()
if convertUint64ValueErr != nil {
continue
}
sourceDataRawPtr.Set(convertUint64Value)
break
case ctdStructTagParamNameTypeUIntPtr:
// tag type is uintptr, need convert data value to uintptr
if sourceDataRawPtr.IsNil() {
continue
}
convertUintptrValue, convertUintptrValueErr := sourceDataRawPtr.ToUintptrE()
if convertUintptrValueErr != nil {
continue
}
sourceDataRawPtr.Set(convertUintptrValue)
break
case ctdStructTagParamNameTypeInt:
// tag type is int, need convert data value to int
if sourceDataRawPtr.IsNil() {
continue
}
convertIntValue, convertIntValueErr := sourceDataRawPtr.ToIntE()
if convertIntValueErr != nil {
continue
}
sourceDataRawPtr.Set(convertIntValue)
break
case ctdStructTagParamNameTypeInt8:
// tag type is int8, need convert data value to int8
if sourceDataRawPtr.IsNil() {
continue
}
convertInt8Value, convertInt8ValueErr := sourceDataRawPtr.ToInt8E()
if convertInt8ValueErr != nil {
continue
}
sourceDataRawPtr.Set(convertInt8Value)
break
case ctdStructTagParamNameTypeInt16:
// tag type is int16, need convert data value to int16
if sourceDataRawPtr.IsNil() {
continue
}
convertInt16Value, convertInt16ValueErr := sourceDataRawPtr.ToInt16E()
if convertInt16ValueErr != nil {
continue
}
sourceDataRawPtr.Set(convertInt16Value)
break
case ctdStructTagParamNameTypeInt32:
// tag type is int32, need convert data value to int32
if sourceDataRawPtr.IsNil() {
continue
}
convertInt32Value, convertInt32ValueErr := sourceDataRawPtr.ToInt32E()
if convertInt32ValueErr != nil {
continue
}
sourceDataRawPtr.Set(convertInt32Value)
break
case ctdStructTagParamNameTypeInt64:
// tag type is int64, need convert data value to int64
if sourceDataRawPtr.IsNil() {
continue
}
convertInt64Value, convertInt64ValueErr := sourceDataRawPtr.ToInt64E()
if convertInt64ValueErr != nil {
continue
}
sourceDataRawPtr.Set(convertInt64Value)
break
case ctdStructTagParamNameTypeFloat32:
// tag type is float32, need convert data value to float32
if sourceDataRawPtr.IsNil() {
continue
}
convertFloat32Value, convertFloat32ValueErr := sourceDataRawPtr.ToFloat32E()
if convertFloat32ValueErr != nil {
continue
}
sourceDataRawPtr.Set(convertFloat32Value)
break
case ctdStructTagParamNameTypeFloat, ctdStructTagParamNameTypeFloat64:
// tag type is float, need convert data value to float64
if sourceDataRawPtr.IsNil() {
continue
}
convertFloat64Value, convertFloat64ValueErr := sourceDataRawPtr.ToFloat64E()
if convertFloat64ValueErr != nil {
continue
}
sourceDataRawPtr.Set(convertFloat64Value)
break
case ctdStructTagParamNameTypeBoolean:
// tag type is boolean, need convert data value to boolean
if sourceDataRawPtr.IsNil() || sourceDataRawPtr.IsBoolean() {
continue
}
convertBooleanValue, convertBooleanValueErr := sourceDataRawPtr.ToBooleanE()
if convertBooleanValueErr != nil {
continue
}
sourceDataRawPtr.Set(convertBooleanValue)
break
case ctdStructTagParamNameTypeNull:
// tag type is null, need convert data value to nil
if sourceDataRawPtr.IsNil() {
continue
}
sourceDataRawPtr.Set(nil)
break
case ctdStructTagParamNameTypeTime:
// tag type is time, need convert data value to time
// check data type is time.Time
if sourceDataRawPtr.IsTime() {
continue
}
// check data value is nil
if sourceDataRawPtr.IsNil() {
continue
}
convertStrValue, convertStrValueErr := sourceDataRawPtr.ToStringE()
if convertStrValueErr != nil {
continue
}
timeLayoutFormatStr, timeLayoutFormatStrHasExists := configTagKV[ctdStructTagParamNameTimeFormat]
if !timeLayoutFormatStrHasExists {
continue
}
if len(timeLayoutFormatStr) == 0 {
timeLayoutFormatStr = ctdStructTagParamValueDefaultFormat
}
parsedTimeFormat, parsedTimeFormatErr := time.Parse(timeLayoutFormatStr, convertStrValue)
if parsedTimeFormatErr != nil {
// parse time with default time format
parsedTimeFormat, parsedTimeFormatErr = time.Parse(time.RFC3339Nano, convertStrValue)
}
if parsedTimeFormatErr != nil {
continue
}
sourceDataRawPtr.Set(parsedTimeFormat)
break
case ctdStructTagParamNameTypeArray:
// data type is array
if sourceDataRawPtr.IsNil() {
continue
}
if sourceDataRawPtr.IsArray() && !sourceDataRawPtr.IsBytes() {
continue
}
convertArrayValue, convertArrayValueErr := sourceDataRawPtr.ToArrayE()
if convertArrayValueErr != nil {
continue
}
sourceDataRawPtr.Set(convertArrayValue)
break
case ctdStructTagParamNameTypeObject, ctdStructTagParamNameTypeMap:
// data type is object
if sourceDataRawPtr.IsNil() || sourceDataRawPtr.IsStruct() || sourceDataRawPtr.IsMap() {
continue
}
convertMapValue, convertMapValueErr := sourceDataRawPtr.ToMapE()
if convertMapValueErr != nil {
continue
}
sourceDataRawPtr.Set(convertMapValue)
break
}
break
}
}
}
// =====================================================
// parseConfigCdtTagToKV parse config tags to kv
func parseConfigCdtTagToKV(fieldRef reflect.StructField) ctdStructTagKV {
readConfigTagStr := strings.TrimSpace(fieldRef.Tag.Get(ctdStructTag))
if len(readConfigTagStr) == 0 {
return nil
}
configTagsArray := strings.Split(readConfigTagStr, ctdStructTagParamInterval)
if len(configTagsArray) == 0 {
return nil
}
configTagKV := make(ctdStructTagKV, 0)
for _, configTagStr := range configTagsArray {
configTagStrToLowerStr := strings.ToLower(configTagStr)
if len(configTagStrToLowerStr) == 0 {
continue
}
// read : after str
midStrIndex := strings.Index(configTagStrToLowerStr, ctdStructTagParamSeparator)
if midStrIndex == -1 {
continue
}
tagNameLowerStr := strings.TrimSpace(configTagStrToLowerStr[:midStrIndex])
if len(tagNameLowerStr) == 0 {
continue
}
if midStrIndex+1 >= len(configTagStrToLowerStr) {
continue
}
var tagValueLowerStr string
switch tagNameLowerStr {
case ctdStructTagParamNameTimeFormat:
tagValueLowerStr = strings.TrimSpace(configTagStrToLowerStr[midStrIndex+1:])
// remove ' from string
if strings.Index(tagValueLowerStr, "'") > -1 {
tagValueLowerStr = strings.ReplaceAll(tagValueLowerStr, "'", "")
}
// remove " from string
if strings.Index(tagValueLowerStr, "\"") > -1 {
tagValueLowerStr = strings.ReplaceAll(tagValueLowerStr, "\"", "")
}
if len(tagValueLowerStr) == 0 {
// default time format
tagValueLowerStr = ctdStructTagParamValueDefaultFormat
}
break
default:
tagValueLowerStr = strings.TrimSpace(configTagStrToLowerStr[midStrIndex+1:])
break
}
configTagKV[tagNameLowerStr] = tagValueLowerStr
}
return configTagKV
}