-
Notifications
You must be signed in to change notification settings - Fork 834
/
type_helpers.go
901 lines (863 loc) · 20.3 KB
/
type_helpers.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
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
package value
import (
"encoding/json"
"errors"
"fmt"
"math"
"strconv"
"time"
"github.com/Jeffail/gabs/v2"
)
// Type represents a discrete value type supported by Bloblang queries.
type Type string
// ValueType variants.
var (
TString Type = "string"
TBytes Type = "bytes"
TNumber Type = "number"
TBool Type = "bool"
TTimestamp Type = "timestamp"
TArray Type = "array"
TObject Type = "object"
TNull Type = "null"
TDelete Type = "delete"
TNothing Type = "nothing"
TQuery Type = "query expression"
TUnknown Type = "unknown"
// Specialised and not generally known over ValueNumber.
TInt Type = "integer"
TFloat Type = "float"
)
// ITypeOf returns the type of a boxed value as a discrete ValueType. If the
// type of the value is unknown then ValueUnknown is returned.
func ITypeOf(i any) Type {
switch i.(type) {
case string:
return TString
case []byte:
return TBytes
case int, int8, int16, int32, int64, uint8, uint16, uint32, uint64, float32, float64, json.Number:
return TNumber
case bool:
return TBool
case time.Time:
return TTimestamp
case []any:
return TArray
case map[string]any:
return TObject
case Delete:
return TDelete
case Nothing:
return TNothing
case nil:
return TNull
}
if _, isDyn := i.(interface {
Annotation() string
}); isDyn {
return TQuery
}
return TUnknown
}
//------------------------------------------------------------------------------
// Delete is a special type that serializes to `null` when forced but indicates
// a target should be deleted.
type Delete *struct{}
// Nothing is a special type that serializes to `null` when forced but indicates
// a query should be disregarded (and not mapped).
type Nothing *struct{}
// IGetNumber takes a boxed value and attempts to extract a number (float64)
// from it.
func IGetNumber(v any) (float64, error) {
switch t := v.(type) {
case int:
return float64(t), nil
case int8:
return float64(t), nil
case int16:
return float64(t), nil
case int32:
return float64(t), nil
case int64:
return float64(t), nil
case uint:
return float64(t), nil
case uint8:
return float64(t), nil
case uint16:
return float64(t), nil
case uint32:
return float64(t), nil
case uint64:
return float64(t), nil
case float32:
return float64(t), nil
case float64:
return t, nil
case json.Number:
return t.Float64()
}
return 0, NewTypeError(v, TNumber)
}
// IGetFloat32 takes a boxed value and attempts to extract a number (float32)
// from it.
func IGetFloat32(v any) (float32, error) {
switch t := v.(type) {
case int:
return float32(t), nil
case int8:
return float32(t), nil
case int16:
return float32(t), nil
case int32:
return float32(t), nil
case int64:
return float32(t), nil
case uint:
return float32(t), nil
case uint8:
return float32(t), nil
case uint16:
return float32(t), nil
case uint32:
return float32(t), nil
case uint64:
return float32(t), nil
case float32:
return t, nil
case float64:
return float32(t), nil
case json.Number:
v, e := t.Float64()
return float32(v), e
}
return 0, NewTypeError(v, TNumber)
}
// IGetInt takes a boxed value and attempts to extract an integer (int64) from
// it.
func IGetInt(v any) (int64, error) {
switch t := v.(type) {
case int:
return int64(t), nil
case int8:
return int64(t), nil
case int16:
return int64(t), nil
case int32:
return int64(t), nil
case int64:
return t, nil
case uint:
return int64(t), nil
case uint8:
return int64(t), nil
case uint16:
return int64(t), nil
case uint32:
return int64(t), nil
case uint64:
return int64(t), nil
case float32:
return int64(t), nil
case float64:
return int64(t), nil
case json.Number:
i, err := t.Int64()
if err == nil {
return i, nil
}
if f, ferr := t.Float64(); ferr == nil {
return int64(f), nil
}
return 0, err
}
return 0, NewTypeError(v, TNumber)
}
// IGetUInt takes a boxed value and attempts to extract an unsigned integer
// (uint64) from it.
func IGetUInt(v any) (uint64, error) {
switch v.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, json.Number:
// We're passing through here because it handles out of bounds issues.
return IToUint(v)
}
return 0, NewTypeError(v, TNumber)
}
// IGetBool takes a boxed value and attempts to extract a boolean from it.
func IGetBool(v any) (bool, error) {
switch t := v.(type) {
case bool:
return t, nil
case int:
return t != 0, nil
case int8:
return t != 0, nil
case int16:
return t != 0, nil
case int32:
return t != 0, nil
case int64:
return t != 0, nil
case uint:
return t != 0, nil
case uint8:
return t != 0, nil
case uint16:
return t != 0, nil
case uint32:
return t != 0, nil
case uint64:
return t != 0, nil
case float32:
return t != 0, nil
case float64:
return t != 0, nil
case json.Number:
return t.String() != "0", nil
}
return false, NewTypeError(v, TBool)
}
// IGetString takes a boxed value and attempts to return a string value. Returns
// an error if the value is not a string or byte slice.
func IGetString(v any) (string, error) {
switch t := v.(type) {
case string:
return t, nil
case []byte:
return string(t), nil
case time.Time:
return t.Format(time.RFC3339Nano), nil
}
return "", NewTypeError(v, TString)
}
// IGetBytes takes a boxed value and attempts to return a byte slice value.
// Returns an error if the value is not a string or byte slice.
func IGetBytes(v any) ([]byte, error) {
switch t := v.(type) {
case string:
return []byte(t), nil
case []byte:
return t, nil
case time.Time:
return t.AppendFormat(nil, time.RFC3339Nano), nil
}
return nil, NewTypeError(v, TBytes)
}
// IGetTimestamp takes a boxed value and attempts to coerce it into a timestamp,
// either by interpretting a numerical value as a unix timestamp, or by parsing
// a string value as RFC3339Nano.
func IGetTimestamp(v any) (time.Time, error) {
if tVal, ok := v.(time.Time); ok {
return tVal, nil
}
switch t := ISanitize(v).(type) {
case int64:
return time.Unix(t, 0), nil
case uint64:
return time.Unix(int64(t), 0), nil
case float64:
fint := math.Trunc(t)
fdec := t - fint
return time.Unix(int64(fint), int64(fdec*1e9)), nil
case json.Number:
if i, err := t.Int64(); err == nil {
return time.Unix(i, 0), nil
} else if f, err := t.Float64(); err == nil {
fint := math.Trunc(f)
fdec := f - fint
return time.Unix(int64(fint), int64(fdec*1e9)), nil
} else {
return time.Time{}, fmt.Errorf("failed to parse value '%v' as number", v)
}
case []byte:
return time.Parse(time.RFC3339Nano, string(t))
case string:
return time.Parse(time.RFC3339Nano, t)
}
return time.Time{}, NewTypeError(v, TNumber, TString)
}
// IIsNull returns whether a bloblang type is null, this includes Delete and
// Nothing types.
func IIsNull(i any) bool {
if i == nil {
return true
}
switch i.(type) {
case Delete, Nothing:
return true
}
return false
}
func RestrictForComparison(v any) any {
v = ISanitize(v)
switch t := v.(type) {
case int64:
return float64(t)
case uint64:
return float64(t)
case json.Number:
if f, err := IGetNumber(t); err == nil {
return f
}
case []byte:
return string(t)
}
return v
}
// ISanitize takes a boxed value of any type and attempts to convert it into one
// of the following types: string, []byte, int64, uint64, float64, bool,
// []interface{}, map[string]interface{}, Delete, Nothing.
func ISanitize(i any) any {
switch t := i.(type) {
case string, []byte, int64, uint64, float64, bool, []any, map[string]any, Delete, Nothing:
return i
case json.RawMessage:
return []byte(t)
case json.Number:
if i, err := t.Int64(); err == nil {
return i
}
if f, err := t.Float64(); err == nil {
return f
}
return t.String()
case time.Time:
return t.Format(time.RFC3339Nano)
case int:
return int64(t)
case int8:
return int64(t)
case int16:
return int64(t)
case int32:
return int64(t)
case uint:
return uint64(t)
case uint8:
return uint64(t)
case uint16:
return uint64(t)
case uint32:
return uint64(t)
case float32:
return float64(t)
}
// Do NOT support unknown types (for now).
return nil
}
// IToBytes takes a boxed value of any type and attempts to convert it into a
// byte slice.
func IToBytes(i any) []byte {
switch t := i.(type) {
case string:
return []byte(t)
case []byte:
return t
case json.Number:
return []byte(t.String())
case int64:
return strconv.AppendInt(nil, t, 10)
case uint64:
return strconv.AppendUint(nil, t, 10)
case float64:
return strconv.AppendFloat(nil, t, 'g', -1, 64)
case bool:
if t {
return []byte("true")
}
return []byte("false")
case time.Time:
return t.AppendFormat(nil, time.RFC3339Nano)
case nil:
return []byte(`null`)
}
// Last resort
return gabs.Wrap(i).Bytes()
}
// IToString takes a boxed value of any type and attempts to convert it into a
// string.
func IToString(i any) string {
switch t := i.(type) {
case string:
return t
case []byte:
return string(t)
case int64:
return strconv.FormatInt(t, 10)
case uint64:
return strconv.FormatUint(t, 10)
case float64:
return strconv.FormatFloat(t, 'g', -1, 64)
case json.Number:
return t.String()
case bool:
if t {
return "true"
}
return "false"
case time.Time:
return t.Format(time.RFC3339Nano)
case nil:
return `null`
}
// Last resort
return gabs.Wrap(i).String()
}
// IToNumber takes a boxed value and attempts to extract a number (float64)
// from it or parse one.
func IToNumber(v any) (float64, error) {
return IToFloat64(v)
}
// IToFloat64 takes a boxed value and attempts to extract a number (float64)
// from it or parse one.
func IToFloat64(v any) (float64, error) {
switch t := v.(type) {
case int:
return float64(t), nil
case int8:
return float64(t), nil
case int16:
return float64(t), nil
case int32:
return float64(t), nil
case int64:
return float64(t), nil
case uint:
return float64(t), nil
case uint8:
return float64(t), nil
case uint16:
return float64(t), nil
case uint32:
return float64(t), nil
case uint64:
return float64(t), nil
case float64:
return t, nil
case float32:
return float64(t), nil
case json.Number:
return t.Float64()
case []byte:
return strconv.ParseFloat(string(t), 64)
case string:
return strconv.ParseFloat(t, 64)
}
return 0, NewTypeError(v, TNumber)
}
// IToFloat32 takes a boxed value and attempts to extract a number (float32)
// from it or parse one.
func IToFloat32(v any) (float32, error) {
switch t := v.(type) {
case int:
return float32(t), nil
case int8:
return float32(t), nil
case int16:
return float32(t), nil
case int32:
return float32(t), nil
case int64:
return float32(t), nil
case uint:
return float32(t), nil
case uint8:
return float32(t), nil
case uint16:
return float32(t), nil
case uint32:
return float32(t), nil
case uint64:
return float32(t), nil
case float64:
return float32(t), nil
case json.Number:
f64, err := strconv.ParseFloat(string(t), 32)
if err != nil {
return 0, err
}
return float32(f64), nil
case []byte:
f64, err := strconv.ParseFloat(string(t), 32)
if err != nil {
return 0, err
}
return float32(f64), nil
case string:
f64, err := strconv.ParseFloat(t, 32)
if err != nil {
return 0, err
}
return float32(f64), nil
}
return 0, NewTypeError(v, TNumber)
}
const (
maxUint = ^uint64(0)
maxUint32 = ^uint32(0)
maxUint16 = ^uint16(0)
maxUint8 = ^uint8(0)
MaxInt = maxUint >> 1
maxInt32 = maxUint32 >> 1
maxInt16 = maxUint16 >> 1
maxInt8 = maxUint8 >> 1
MinInt = ^int64(MaxInt)
minInt32 = ^int32(maxInt32)
minInt16 = ^int16(maxInt16)
minInt8 = ^int8(maxInt8)
)
// IToInt takes a boxed value and attempts to extract a number (int64) from it
// or parse one.
func IToInt(v any) (int64, error) {
switch t := v.(type) {
case int:
return int64(t), nil
case int8:
return int64(t), nil
case int16:
return int64(t), nil
case int32:
return int64(t), nil
case int64:
return t, nil
case uint:
return int64(t), nil
case uint8:
return int64(t), nil
case uint16:
return int64(t), nil
case uint32:
return int64(t), nil
case uint64:
if t > MaxInt {
return 0, errors.New("unsigned integer value is too large to be cast as a signed integer")
}
return int64(t), nil
case float32:
return IToInt(float64(t))
case float64:
if math.IsInf(t, 0) {
return 0, errors.New("cannot convert +/-INF to an integer")
}
if math.IsNaN(t) {
return 0, errors.New("cannot convert NAN to an integer")
}
if t > float64(MaxInt) {
return 0, errors.New("float value is too large to be cast as a signed integer")
}
if t < float64(MinInt) {
return 0, errors.New("float value is too small to be cast as a signed integer")
}
if t-float64(int64(t)) != 0 {
return 0, errors.New("float value contains decimals and therefore cannot be cast as a signed integer, if you intend to round the value then call `.round()` explicitly before this cast")
}
return int64(t), nil
case json.Number:
return t.Int64()
case []byte:
return strconv.ParseInt(string(t), 0, 64)
case string:
return strconv.ParseInt(t, 0, 64)
}
return 0, NewTypeError(v, TNumber)
}
// IToInt32 takes a boxed value and attempts to extract a number (int32) from
// it or parse one.
func IToInt32(v any) (int32, error) {
if v, ok := v.(int32); ok {
return v, nil
}
i64, err := IToInt(v)
if err != nil {
return 0, err
}
if i64 > int64(maxInt32) {
return 0, errors.New("value is too large to be cast as a 32-bit signed integer")
}
if i64 < int64(minInt32) {
return 0, errors.New("value is too small to be cast as a 32-bit signed integer")
}
return int32(i64), nil
}
// IToInt16 takes a boxed value and attempts to extract a number (int64) from
// it or parse one.
func IToInt16(v any) (int16, error) {
if v, ok := v.(int16); ok {
return v, nil
}
i64, err := IToInt(v)
if err != nil {
return 0, err
}
if i64 > int64(maxInt16) {
return 0, errors.New("value is too large to be cast as a 16-bit signed integer")
}
if i64 < int64(minInt16) {
return 0, errors.New("value is too small to be cast as a 16-bit signed integer")
}
return int16(i64), nil
}
// IToInt8 takes a boxed value and attempts to extract a number (int8) from
// it or parse one.
func IToInt8(v any) (int8, error) {
if v, ok := v.(int8); ok {
return v, nil
}
i64, err := IToInt(v)
if err != nil {
return 0, err
}
if i64 > int64(maxInt8) {
return 0, errors.New("value is too large to be cast as an 8-bit signed integer")
}
if i64 < int64(minInt8) {
return 0, errors.New("value is too small to be cast as an 8-bit signed integer")
}
return int8(i64), nil
}
// IToUint takes a boxed value and attempts to extract a number (uint64) from it
// or parse one.
func IToUint(v any) (uint64, error) {
switch t := v.(type) {
case uint:
return uint64(t), nil
case uint8:
return uint64(t), nil
case uint16:
return uint64(t), nil
case uint32:
return uint64(t), nil
case uint64:
return t, nil
case int:
return IToUint(int64(t))
case int8:
return IToUint(int64(t))
case int16:
return IToUint(int64(t))
case int32:
return IToUint(int64(t))
case int64:
if t < 0 {
return 0, errors.New("signed integer value is negative and cannot be cast as an unsigned integer")
}
return uint64(t), nil
case float32:
return IToUint(float64(t))
case float64:
if t < 0 {
return 0, errors.New("float value is negative and cannot be cast as an unsigned integer")
}
if math.IsInf(t, 0) {
return 0, errors.New("cannot convert +/-INF to an unsigned integer")
}
if math.IsNaN(t) {
return 0, errors.New("cannot convert NAN to an unsigned integer")
}
if t > float64(maxUint) {
return 0, errors.New("float value is too large to be cast as an unsigned integer")
}
if t-float64(uint64(t)) != 0 {
return 0, errors.New("float value contains decimals and therefore cannot be cast as an unsigned integer, if you intend to round the value then call `.round()` explicitly before this cast")
}
return uint64(t), nil
case json.Number:
i, err := t.Int64()
if err != nil {
return 0, err
}
if i < 0 {
return 0, errors.New("signed integer value is negative and cannot be cast as an unsigned integer")
}
return uint64(i), nil
case []byte:
return strconv.ParseUint(string(t), 0, 64)
case string:
return strconv.ParseUint(t, 0, 64)
}
return 0, NewTypeError(v, TNumber)
}
// IToUint32 takes a boxed value and attempts to extract a number (uint32) from
// it or parse one.
func IToUint32(v any) (uint32, error) {
if v, ok := v.(uint32); ok {
return v, nil
}
u64, err := IToUint(v)
if err != nil {
return 0, err
}
if u64 > uint64(maxUint32) {
return 0, errors.New("value is too large to be cast as a 32-bit unsigned integer")
}
return uint32(u64), nil
}
// IToUint16 takes a boxed value and attempts to extract a number (uint16) from
// it or parse one.
func IToUint16(v any) (uint16, error) {
if v, ok := v.(uint16); ok {
return v, nil
}
u64, err := IToUint(v)
if err != nil {
return 0, err
}
if u64 > uint64(maxUint16) {
return 0, errors.New("value is too large to be cast as a 16-bit unsigned integer")
}
return uint16(u64), nil
}
// IToUint8 takes a boxed value and attempts to extract a number (uint8) from
// it or parse one.
func IToUint8(v any) (uint8, error) {
if v, ok := v.(uint8); ok {
return v, nil
}
u64, err := IToUint(v)
if err != nil {
return 0, err
}
if u64 > uint64(maxUint8) {
return 0, errors.New("value is too large to be cast as an 8-bit unsigned integer")
}
return uint8(u64), nil
}
// IToBool takes a boxed value and attempts to extract a boolean from it or
// parse it into a bool.
func IToBool(v any) (bool, error) {
switch t := v.(type) {
case bool:
return t, nil
case int:
return t != 0, nil
case int8:
return t != 0, nil
case int16:
return t != 0, nil
case int32:
return t != 0, nil
case int64:
return t != 0, nil
case uint:
return t != 0, nil
case uint8:
return t != 0, nil
case uint16:
return t != 0, nil
case uint32:
return t != 0, nil
case uint64:
return t != 0, nil
case float32:
return t != 0, nil
case float64:
return t != 0, nil
case json.Number:
return t.String() != "0", nil
case []byte:
if v, err := strconv.ParseBool(string(t)); err == nil {
return v, nil
}
case string:
if v, err := strconv.ParseBool(t); err == nil {
return v, nil
}
}
return false, NewTypeError(v, TBool)
}
// IClone performs a deep copy of a generic value.
func IClone(root any) any {
switch t := root.(type) {
case map[string]any:
newMap := make(map[string]any, len(t))
for k, v := range t {
newMap[k] = IClone(v)
}
return newMap
case []any:
newSlice := make([]any, len(t))
for i, v := range t {
newSlice[i] = IClone(v)
}
return newSlice
}
return root
}
// ICompare returns true if both the left and right are equal according to one
// of the following conditions:
//
// - The types exactly match and have the same value
// - The types are both either a string or byte slice and the underlying data is the same
// - The types are both numerical and have the same value
// - Both types are a matching slice or map containing values matching these same conditions.
func ICompare(left, right any) bool {
if left == nil && right == nil {
return true
}
switch lhs := RestrictForComparison(left).(type) {
case string:
rhs, err := IGetString(right)
if err != nil {
return false
}
return lhs == rhs
case float64:
rhs, err := IGetNumber(right)
if err != nil {
return false
}
return lhs == rhs
case bool:
rhs, err := IGetBool(right)
if err != nil {
return false
}
return lhs == rhs
case []any:
rhs, matches := right.([]any)
if !matches {
return false
}
if len(lhs) != len(rhs) {
return false
}
for i, vl := range lhs {
if !ICompare(vl, rhs[i]) {
return false
}
}
return true
case map[string]any:
rhs, matches := right.(map[string]any)
if !matches {
return false
}
if len(lhs) != len(rhs) {
return false
}
for k, vl := range lhs {
if !ICompare(vl, rhs[k]) {
return false
}
}
return true
}
return false
}
func IGetStringMap(v any) (map[string]string, error) {
iMap, ok := v.(map[string]any)
if !ok {
if sMap, ok := v.(map[string]string); ok {
return sMap, nil
}
return nil, NewTypeError(v, TObject)
}
sMap := make(map[string]string, len(iMap))
for k, ev := range iMap {
if sMap[k], ok = ev.(string); !ok {
return nil, NewTypeError(ev, TString)
}
}
return sMap, nil
}