-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfastval.go
1125 lines (1020 loc) · 27.8 KB
/
fastval.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
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2018 Couchbase, Inc. All rights reserved.
package gojsonsm
import (
"errors"
"fmt"
"math"
"regexp"
"strconv"
"strings"
"time"
"unsafe"
)
type ValueType int
// This must be in comparison precedence order!
// Mirrors as closely as possible with N1QL Collate order
const (
InvalidValue ValueType = iota
MissingValue
NullValue
FalseValue
TrueValue
// Numerics:
UintValue
JsonUintValue
IntValue
JsonIntValue
FloatValue
JsonFloatValue
// String types
StringValue
BinStringValue
JsonStringValue
// Time can only be implicitly converted from a specific string form
TimeValue
// Array Obj
ArrayValue
ObjectValue
// Binary types
BinaryValue
RegexValue
PcreValue
)
// Implicit Conversion Table
// Keyed by type, and then a list of potentially convertible target datatype with
// "true" for definitely convertible and "false" for potentially convertible
// If a ValueType does not exist in this table, that means there is no possible target conversion
var ImplicitConvTable = map[ValueType]map[ValueType]bool{
// Numerics
IntValue: map[ValueType]bool{UintValue: false, JsonUintValue: false, FloatValue: true, JsonFloatValue: true,
StringValue: true, BinStringValue: true, JsonStringValue: true, NullValue: true, TrueValue: true, FalseValue: true},
JsonIntValue: map[ValueType]bool{UintValue: false, JsonUintValue: false, FloatValue: true, JsonFloatValue: true,
StringValue: true, BinStringValue: true, JsonStringValue: true, NullValue: true, TrueValue: true, FalseValue: true},
UintValue: map[ValueType]bool{IntValue: true, JsonIntValue: true, FloatValue: true, JsonFloatValue: true, StringValue: true,
BinStringValue: true, JsonStringValue: true, NullValue: true, TrueValue: true, FalseValue: true},
JsonUintValue: map[ValueType]bool{IntValue: true, JsonIntValue: true, FloatValue: true, JsonFloatValue: true, StringValue: true,
BinStringValue: true, JsonStringValue: true, NullValue: true, TrueValue: true, FalseValue: true},
FloatValue: map[ValueType]bool{StringValue: true, BinStringValue: true, JsonStringValue: true, NullValue: true, TrueValue: true,
FalseValue: true},
JsonFloatValue: map[ValueType]bool{StringValue: true, BinStringValue: true, JsonStringValue: true, NullValue: true, TrueValue: true,
FalseValue: true},
// Non-Numerics
TrueValue: map[ValueType]bool{IntValue: true, JsonIntValue: true, UintValue: true, JsonUintValue: true, FloatValue: true,
JsonFloatValue: true, StringValue: true, BinStringValue: true, JsonStringValue: true, NullValue: true, FalseValue: true},
FalseValue: map[ValueType]bool{IntValue: true, JsonIntValue: true, UintValue: true, JsonUintValue: true, FloatValue: true,
JsonFloatValue: true, StringValue: true, BinStringValue: true, JsonStringValue: true, NullValue: true, TrueValue: true},
StringValue: map[ValueType]bool{IntValue: false, JsonIntValue: false, UintValue: false, JsonUintValue: false, FloatValue: false,
JsonFloatValue: false, NullValue: true, TrueValue: false, FalseValue: false, TimeValue: false},
BinStringValue: map[ValueType]bool{IntValue: false, JsonIntValue: false, UintValue: false, JsonUintValue: false, FloatValue: false,
JsonFloatValue: false, NullValue: true, TrueValue: false, FalseValue: false, TimeValue: false},
JsonStringValue: map[ValueType]bool{IntValue: false, JsonIntValue: false, UintValue: false, JsonUintValue: false, FloatValue: false,
JsonFloatValue: false, NullValue: true, TrueValue: false, FalseValue: false, TimeValue: false},
}
// When users try to match a string to a bool, the bool is converted to a JSON string
// These are constants
const TrueString = "true"
const FalseString = "false"
var TrueValueBytes = []byte(TrueString)
var FalseValueBytes = []byte(FalseString)
var TrueStringRegex *regexp.Regexp = regexp.MustCompile("^[T|t][R|r][U|u][E|e]$")
var FalseStringRegex *regexp.Regexp = regexp.MustCompile("^[F|f][A|a][L|l][S|s][E|e]$")
var toJsonStringBuffer []byte
type FastVal struct {
dataType ValueType
data interface{}
sliceData []byte
rawData [8]byte
userDefined bool
}
func (val FastVal) String() string {
switch val.dataType {
case InvalidValue:
return "invalid"
case MissingValue:
return "missing"
case IntValue:
return "(int)" + fmt.Sprintf("%d", val.GetInt())
case UintValue:
return "(uint)" + fmt.Sprintf("%d", val.GetUint())
case FloatValue:
return "(float)" + fmt.Sprintf("%f", val.GetFloat())
case JsonIntValue:
return "(jsonInt)" + string(val.sliceData)
case JsonUintValue:
return "(jsonUint)" + string(val.sliceData)
case JsonFloatValue:
return "(jsonFloat)" + string(val.sliceData)
case StringValue:
return "(string)" + val.data.(string)
case BinStringValue:
tmpVal, _ := val.ToBinString()
return "(binString)" + fmt.Sprintf(`"%s"`, tmpVal.sliceData)
case JsonStringValue:
tmpVal, _ := val.ToBinString()
return "(jsonString)" + fmt.Sprintf(`"%s"`, tmpVal.sliceData)
case BinaryValue:
return "(bin)" + fmt.Sprintf(`"%s"`, val.sliceData)
case NullValue:
return "null"
case TrueValue:
return "true"
case FalseValue:
return "false"
case ArrayValue:
return "(array)" + string(val.sliceData)
case ObjectValue:
return "(object)" + string(val.sliceData)
case TimeValue:
return val.GetTime().String()
case RegexValue:
return "(regexp)" + val.data.(*regexp.Regexp).String()
}
panic(fmt.Sprintf("unexpected data type %v", val.dataType))
}
func (val FastVal) Type() ValueType {
return val.dataType
}
func (val FastVal) IsMissing() bool {
return val.dataType == MissingValue
}
func (val FastVal) IsNull() bool {
return val.dataType == NullValue
}
func (val FastVal) IsBinary() bool {
return val.dataType == BinaryValue
}
func (val FastVal) IsBoolean() bool {
return val.dataType == TrueValue ||
val.dataType == FalseValue
}
func (val FastVal) IsIntegral() bool {
return val.IsInt() ||
val.IsUInt()
}
func (val FastVal) IsInt() bool {
return val.dataType == IntValue ||
val.dataType == JsonIntValue
}
func (val FastVal) IsUInt() bool {
return val.dataType == UintValue ||
val.dataType == JsonUintValue
}
func (val FastVal) IsFloat() bool {
return val.dataType == FloatValue ||
val.dataType == JsonFloatValue
}
func (val FastVal) IsNumeric() bool {
return val.IsInt() ||
val.IsUInt() ||
val.IsFloat()
}
func (val FastVal) IsString() bool {
return val.dataType == StringValue ||
val.dataType == BinStringValue ||
val.dataType == JsonStringValue
}
func (val FastVal) IsTime() bool {
return val.dataType == TimeValue
}
func (val FastVal) GetInt() int64 {
return *(*int64)(unsafe.Pointer(&val.rawData))
}
func (val FastVal) GetUint() uint64 {
return *(*uint64)(unsafe.Pointer(&val.rawData))
}
func (val FastVal) GetFloat() float64 {
return *(*float64)(unsafe.Pointer(&val.rawData))
}
func (val FastVal) GetTime() *time.Time {
return val.data.(*time.Time)
}
func (val FastVal) AsInt() (int64, bool) {
switch val.dataType {
case IntValue:
return val.GetInt(), true
case UintValue:
uintVal := val.GetUint()
return int64(uintVal), uintVal <= math.MaxInt64
case FloatValue:
return int64(val.GetFloat()), false
case BinStringValue:
fallthrough
case JsonStringValue:
fallthrough
case JsonIntValue:
parsedVal, err := strconv.ParseInt(string(val.sliceData), 10, 64)
return parsedVal, err == nil
case JsonUintValue:
parsedVal, err := strconv.ParseUint(string(val.sliceData), 10, 64)
if err == nil && parsedVal > math.MaxInt64 {
return 0, false
} else {
return int64(parsedVal), err == nil
}
case JsonFloatValue:
parsedVal, _ := strconv.ParseFloat(string(val.sliceData), 64)
return int64(parsedVal), false
case TrueValue:
return 1, true
case FalseValue:
return 0, true
}
return 0, false
}
func (val FastVal) AsUint() (uint64, bool) {
switch val.dataType {
case IntValue:
intVal := val.GetInt()
if intVal > 0 {
return uint64(intVal), true
} else {
return 0, false
}
case UintValue:
return val.GetUint(), true
case FloatValue:
return uint64(val.GetFloat()), false
case JsonIntValue:
parsedVal, err := strconv.ParseInt(string(val.sliceData), 10, 64)
if err == nil && parsedVal > 0 {
return uint64(parsedVal), true
} else {
return 0, false
}
case BinStringValue:
fallthrough
case JsonStringValue:
tmpVal, _ := val.ToBinString()
parsedVal, err := strconv.ParseUint(string(tmpVal.sliceData), 10, 64)
return parsedVal, err == nil
case JsonUintValue:
parsedVal, err := strconv.ParseUint(string(val.sliceData), 10, 64)
return parsedVal, err == nil
case JsonFloatValue:
parsedVal, err := strconv.ParseFloat(string(val.sliceData), 64)
return uint64(parsedVal), err == nil
case TrueValue:
return 1, true
case FalseValue:
return 0, true
}
return 0, false
}
func (val FastVal) AsFloat() (float64, bool) {
switch val.dataType {
case IntValue:
return float64(val.GetInt()), true
case UintValue:
return float64(val.GetUint()), true
case FloatValue:
return val.GetFloat(), true
case JsonIntValue:
parsedVal, err := strconv.ParseInt(string(val.sliceData), 10, 64)
return float64(parsedVal), err == nil
case JsonUintValue:
parsedVal, err := strconv.ParseUint(string(val.sliceData), 10, 64)
return float64(parsedVal), err == nil
case JsonStringValue:
fallthrough
case JsonFloatValue:
parsedVal, err := strconv.ParseFloat(string(val.sliceData), 64)
return parsedVal, err == nil
case TrueValue:
return 1.0, true
case FalseValue:
return 0.0, true
}
return 0.0, false
}
func (val FastVal) AsBoolean() (bool, bool) {
switch val.dataType {
case JsonStringValue:
if TrueStringRegex.Match(val.sliceData) {
return true, true
} else if FalseStringRegex.Match(val.sliceData) {
return false, true
} else {
return false, false
}
case IntValue:
return val.GetInt() != 0, true
case UintValue:
return val.GetUint() != 0, true
case FloatValue:
return val.GetFloat() != 0.0, true
case JsonIntValue:
parsedVal, err := strconv.ParseInt(string(val.sliceData), 10, 64)
return parsedVal != 0, err == nil
case JsonUintValue:
parsedVal, err := strconv.ParseUint(string(val.sliceData), 10, 64)
return parsedVal != 0, err == nil
case JsonFloatValue:
parsedVal, err := strconv.ParseFloat(string(val.sliceData), 64)
return parsedVal != 0.0, err == nil
case TrueValue:
return true, true
case FalseValue:
return false, true
default:
// Undefined
return true, false
}
}
func (val FastVal) AsString() (string, bool) {
switch val.dataType {
case StringValue:
return val.data.(string), true
case JsonStringValue:
fallthrough
case BinStringValue:
tmpVal, _ := val.ToBinString()
return string(tmpVal.sliceData), true
case IntValue:
return fmt.Sprintf("%d", val.GetInt()), true
case UintValue:
return fmt.Sprintf("%d", val.GetUint()), true
case FloatValue:
return fmt.Sprintf("%f", val.GetFloat()), true
case JsonIntValue:
return string(val.sliceData), true
case JsonUintValue:
return string(val.sliceData), true
case JsonFloatValue:
return string(val.sliceData), true
case TrueValue:
return TrueString, true
case FalseValue:
return FalseString, true
}
return "", false
}
func (val FastVal) AsRegex() (FastValRegexIface, bool) {
switch val.dataType {
case RegexValue:
return val.data.(*regexp.Regexp), true
case PcreValue:
return val.data.(PcreWrapperInterface), true
}
return nil, false
}
func (val FastVal) AsTime() (*time.Time, bool) {
switch val.dataType {
case TimeValue:
return val.data.(*time.Time), true
case StringValue:
fallthrough
case JsonStringValue:
fallthrough
case BinStringValue:
timeFastVal, err := GetNewTimeFastVal(string(val.sliceData))
if err == nil {
return timeFastVal.data.(*time.Time), true
}
}
return nil, false
}
func (val FastVal) ToBinString() (FastVal, error) {
switch val.dataType {
case StringValue:
return NewBinStringFastVal([]byte(val.data.(string))), nil
case BinStringValue:
return val, nil
case JsonStringValue:
// TODO: MUST DO - Unescape!
return val, nil
}
return val, errors.New("invalid type coercion")
}
// The following reuse an internal buffer so this should be hidden from outside callers
// Internally, this must be called only once per comparison, and should be used for implicit comversion
// (i.e. no double implicit conversion to string from the following 3 types)
func (val FastVal) toJsonStringInternal() (FastVal, error) {
val, err := val.ToJsonString()
if err != nil {
switch val.dataType {
case UintValue:
toJsonStringBuffer = toJsonStringBuffer[:0]
toJsonStringBuffer = strconv.AppendUint(toJsonStringBuffer, val.GetUint(), 10)
return NewJsonStringFastVal(toJsonStringBuffer), nil
case IntValue:
toJsonStringBuffer = toJsonStringBuffer[:0]
toJsonStringBuffer = strconv.AppendInt(toJsonStringBuffer, val.GetInt(), 10)
return NewJsonStringFastVal(toJsonStringBuffer), nil
case FloatValue:
toJsonStringBuffer = toJsonStringBuffer[:0]
toJsonStringBuffer = strconv.AppendFloat(toJsonStringBuffer, val.GetFloat(), 'E', -1, 64)
return NewJsonStringFastVal(toJsonStringBuffer), nil
}
}
return val, err
}
func (val FastVal) ToJsonString() (FastVal, error) {
invalidErr := errors.New("invalid type coercion")
switch val.dataType {
case StringValue:
// TODO: Improve AsJsonString allocations
quotedBytes := strconv.AppendQuote(nil, val.data.(string))
return NewJsonStringFastVal(quotedBytes[1 : len(quotedBytes)-1]), nil
case BinStringValue:
// TODO: Improve AsJsonString allocaitons
quotedBytes := strconv.AppendQuote(nil, string(val.sliceData))
return NewJsonStringFastVal(quotedBytes[1 : len(quotedBytes)-1]), nil
case JsonStringValue:
return val, nil
case TrueValue:
return NewJsonStringFastVal(TrueValueBytes), nil
case FalseValue:
return NewJsonStringFastVal(FalseValueBytes), nil
case NullValue:
return NewInvalidFastVal(), invalidErr
}
return val, invalidErr
}
func (val FastVal) floatToIntOverflows() bool {
floatVal := val.GetFloat()
// Instead of using math constants that could potentially lead to rounding errors,
// force a float-to-float comparison here
if !(floatVal >= math.MinInt64 && floatVal <= math.MaxInt64) {
return true
} else {
return false
}
}
func (val FastVal) compareNull(other FastVal) (int, bool) {
if val.IsNull() && other.IsNull() {
return 0, true
} else if val.IsNull() && !other.IsNull() {
return -1, true
} else if !val.IsNull() && other.IsNull() {
return 1, true
} else {
// Shouldn't be possible
return 0, false
}
}
func (val FastVal) compareInt(other FastVal) (int, bool) {
if other.dataType == FloatValue && other.floatToIntOverflows() {
return val.compareFloat(other)
}
intVal, valid := val.AsInt()
intOval, valid2 := other.AsInt()
if intVal < intOval {
return -1, valid && valid2
} else if intVal > intOval {
return 1, valid && valid2
} else {
return 0, valid && valid2
}
}
func (val FastVal) compareUint(other FastVal) (int, bool) {
uintVal, valid := val.AsUint()
uintOval, valid2 := other.AsUint()
if uintVal < uintOval {
return -1, valid && valid2
} else if uintVal > uintOval {
return 1, valid && valid2
} else {
return 0, valid && valid2
}
}
func (val FastVal) compareFloat(other FastVal) (int, bool) {
// TODO(brett19): EPISLON probably should be defined better than this
// possibly even 0 if we want to force exact matching for floats...
EPSILON := 0.0000001
floatVal, valid := val.AsFloat()
floatOval, valid2 := other.AsFloat()
if math.IsNaN(floatVal) || math.IsNaN(floatOval) {
// Comparing Not-A-Number
// Documentation wise - they should be aware that NaN ops are undefined
// In the meantime - because we have to return something, just let imaginary numbers be < real numbers
if math.IsNaN(floatVal) && math.IsNaN(floatOval) {
return 0, false
} else if math.IsNaN(floatVal) && !math.IsNaN(floatOval) {
return -1, false
} else if !math.IsNaN(floatVal) && math.IsNaN(floatOval) {
return 1, false
}
}
// Perform epsilon comparison first
if math.Abs(floatVal-floatOval) < EPSILON {
return 0, valid && valid2
}
// Traditional comparison
if floatVal < floatOval {
return -1, valid && valid2
} else if floatVal > floatOval {
return 1, valid && valid2
} else {
return 0, valid && valid2
}
}
func (val FastVal) compareBoolean(other FastVal) (int, bool) {
valBool, valid := val.AsBoolean()
otherBool, valid2 := other.AsBoolean()
if !valid || !valid2 {
return 0, false
}
if valBool == otherBool {
return 0, true
} else if valBool && !otherBool {
return 1, true
} else {
return -1, true
}
}
func (val FastVal) compareStrings(other FastVal) (int, bool) {
if other.IsString() || other.IsNumeric() {
escVal, err := val.toJsonStringInternal()
escOval, err1 := other.toJsonStringInternal()
result := strings.Compare(string(escVal.sliceData), string(escOval.sliceData))
return result, err == nil && err1 == nil
} else if other.userDefined {
// User defined means that this val should try to implicit convert to the other type
switch other.dataType {
case TrueValue:
fallthrough
case FalseValue:
if TrueStringRegex.Match(val.sliceData) || FalseStringRegex.Match(val.sliceData) {
return val.compareBoolean(other)
}
case TimeValue:
_, err := GetNewTimeFastVal(string(val.sliceData))
if err == nil {
return val.compareTime(other)
}
}
}
return 0, false
}
func (val FastVal) compareTime(other FastVal) (int, bool) {
thisTime, valid := val.AsTime()
otherTime, valid2 := other.AsTime()
if thisTime == nil || otherTime == nil {
return 0, false
}
if thisTime.Equal(*otherTime) {
return 0, valid && valid2
} else if thisTime.After(*otherTime) {
return 1, valid && valid2
} else {
return -1, valid && valid2
}
}
func (val FastVal) compareArray(other FastVal) (int, bool) {
// TODO - need a better way but for now treat them the same
return val.compareObjArrData(other)
}
func (val FastVal) compareObject(other FastVal) (int, bool) {
// TODO - need a better way but for now treat them the same
return val.compareObjArrData(other)
}
func (val FastVal) compareObjArrData(other FastVal) (int, bool) {
// Do not use reflect
switch val.dataType {
case ArrayValue:
fallthrough
case ObjectValue:
if len(val.sliceData) > len(other.sliceData) {
return 1, true
} else if len(val.sliceData) < len(other.sliceData) {
return -1, true
} else {
for i := range val.sliceData {
if val.sliceData[i] > other.sliceData[i] {
return 1, true
} else if val.sliceData[i] < other.sliceData[i] {
return -1, true
}
}
return 0, true
}
default:
return -1, false
}
}
// This is really using other as the baseline for calling compare,
// and then reversing the result
// This is so that comparisons between different data types are bidirectionally consistent
func (val FastVal) reverseCompare(other FastVal) (int, bool) {
result, valid := other.compareInternal(val)
return result * -1, valid
}
// Collate is used when valid comparisons cannot be done
func (val FastVal) Collate(other FastVal) (int, bool) {
if val.dataType == other.dataType {
return 0, false
} else if val.dataType < other.dataType {
return -1, false
} else {
return 1, false
}
}
func (val FastVal) Compare(other FastVal) (int, bool) {
if val.userDefined || other.userDefined {
return val.compareUserDefined(other)
} else if val.isSameDataTypeAs(other) {
return val.compareInternal(other)
} else {
// Two pass compare - try to cast to the more restrictive type first
if val.dataType < other.dataType {
compatible := val.checkCompatibility(other)
if !compatible {
reverseCompatible := other.checkCompatibility(val)
if !reverseCompatible {
return val.Collate(other)
} else {
return val.reverseCompare(other)
}
} else {
return val.compareInternal(other)
}
} else {
reverseCompatible := other.checkCompatibility(val)
if !reverseCompatible {
compatible := val.checkCompatibility(other)
if !compatible {
return val.Collate(other)
} else {
return val.compareInternal(other)
}
} else {
return val.reverseCompare(other)
}
}
}
}
// Shouldn't allow users to have both defined
func (val FastVal) compareUserDefined(other FastVal) (int, bool) {
bothAreNumeric := val.IsNumeric() && other.IsNumeric()
if val.userDefined {
compatible := val.checkCompatibility(other)
if !compatible {
if bothAreNumeric {
return val.reverseCompare(other)
}
return val.Collate(other)
}
// Force the other to implicitly cast to val if needed
return val.compareInternal(other)
} else {
compatible := other.checkCompatibility(val)
if !compatible {
if bothAreNumeric {
return val.reverseCompare(other)
}
return val.Collate(other)
}
// Force val to implicitly cast to other if needed
return val.reverseCompare(other)
}
}
// Prereq: This call should only be called when "other" is a compatible type
func (val FastVal) compareNumerics(other FastVal) (int, bool) {
switch val.dataType {
case JsonIntValue:
fallthrough
case IntValue:
switch other.dataType {
case FloatValue:
fallthrough
case JsonFloatValue:
return val.compareFloat(other)
case UintValue:
fallthrough
case JsonUintValue:
intVal, _ := val.AsInt()
if intVal >= 0 {
return val.compareUint(other)
} else {
return val.compareInt(other)
}
default:
return val.compareInt(other)
}
case JsonUintValue:
fallthrough
case UintValue:
switch other.dataType {
case IntValue:
fallthrough
case JsonIntValue:
intVal, _ := val.AsInt()
if intVal >= 0 {
return val.compareUint(other)
} else {
return val.compareInt(other)
}
case FloatValue:
fallthrough
case JsonFloatValue:
return val.compareFloat(other)
default:
return val.compareUint(other)
}
case FloatValue:
fallthrough
case JsonFloatValue:
return val.compareFloat(other)
default:
panic("Invalid call into compareNumerics")
}
}
func (val FastVal) isSameDataTypeAs(other FastVal) bool {
if val.dataType == other.dataType {
return true
} else if val.IsString() && other.IsString() {
return true
} else {
return false
}
}
// Check if other can be implicitly converted to val type
func (val FastVal) checkCompatibility(other FastVal) bool {
if val.isSameDataTypeAs(other) {
return true
} else if val.IsNull() {
// Anything can be compared with null
return true
}
compatibleTypes, ok := ImplicitConvTable[other.dataType]
if !ok {
// other's datatype cannot be casted to anything else
return false
}
fullyCompatible, ok := compatibleTypes[val.dataType]
if !ok {
// Val's type is not something that is convertible from other
return false
}
if fullyCompatible {
return true
} else {
// Need to check to see if it is really convertible
var compatible bool
switch val.dataType {
case TimeValue:
_, compatible = other.AsTime()
case UintValue:
fallthrough
case JsonUintValue:
_, compatible = other.AsUint()
case IntValue:
fallthrough
case JsonIntValue:
_, compatible = other.AsInt()
case FloatValue:
fallthrough
case JsonFloatValue:
_, compatible = other.AsFloat()
case TrueValue:
fallthrough
case FalseValue:
_, compatible = other.AsBoolean()
case StringValue:
case BinStringValue:
case JsonStringValue:
_, compatible = other.AsString()
}
return compatible
}
}
// Returns compared val and boolean indicating if the comparison is valid
func (val FastVal) compareInternal(other FastVal) (int, bool) {
switch val.dataType {
case IntValue:
fallthrough
case UintValue:
fallthrough
case FloatValue:
fallthrough
case JsonIntValue:
fallthrough
case JsonUintValue:
fallthrough
case JsonFloatValue:
return val.compareNumerics(other)
case StringValue:
fallthrough
case BinStringValue:
fallthrough
case JsonStringValue:
return val.compareStrings(other)
case TrueValue:
fallthrough
case FalseValue:
return val.compareBoolean(other)
case TimeValue:
return val.compareTime(other)
case ArrayValue:
return val.compareArray(other)
case ObjectValue:
return val.compareObject(other)
case NullValue:
return val.compareNull(other)
}
return val.Collate(other)
}
func (val FastVal) Equals(other FastVal) (bool, bool) {
result, valid := val.Compare(other)
equals := result == 0
if !valid {
equals = false
}
return equals, valid
}
func (val FastVal) matchStrings(other FastVal) (bool, bool) {
escVal, err := val.toJsonStringInternal()
if err != nil {
return false, false
}
regex, valid := other.AsRegex()
if !valid {
return false, valid
} else {
return regex.Match(escVal.sliceData), true
}
}
func (val FastVal) Matches(other FastVal) (bool, bool) {
switch val.dataType {
case StringValue:
return val.matchStrings(other)
case BinStringValue:
return val.matchStrings(other)
case JsonStringValue:
return val.matchStrings(other)
default:
return false, false
}
}
func NewFastVal(val interface{}) FastVal {
switch val := val.(type) {
case int:
return NewIntFastVal(int64(val))
case int8:
return NewIntFastVal(int64(val))
case int16:
return NewIntFastVal(int64(val))
case int32:
return NewIntFastVal(int64(val))
case int64:
return NewIntFastVal(int64(val))
case uint:
return NewUintFastVal(uint64(val))
case uint8:
return NewUintFastVal(uint64(val))
case uint16:
return NewUintFastVal(uint64(val))
case uint32:
return NewUintFastVal(uint64(val))
case uint64:
return NewUintFastVal(val)
case float32:
return NewFloatFastVal(float64(val))
case float64:
return NewFloatFastVal(val)
case bool:
return NewBoolFastVal(val)
case string:
return NewStringFastVal(val)
case []byte:
return NewBinaryFastVal(val)
case *regexp.Regexp:
return NewRegexpFastVal(val)
case PcreWrapperInterface:
return NewPcreFastVal(val)
case *time.Time:
return NewTimeFastVal(val)
case nil:
return NewNullFastVal()
}
return FastVal{
dataType: InvalidValue,
}
}
func NewInvalidFastVal() FastVal {
return FastVal{
dataType: InvalidValue,
}
}
func NewMissingFastVal() FastVal {
return FastVal{
dataType: MissingValue,
}
}
func NewNullFastVal() FastVal {
return FastVal{
dataType: NullValue,
}
}
func NewBoolFastVal(value bool) FastVal {
if value {
return FastVal{