-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.go
986 lines (912 loc) · 27.5 KB
/
value.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
// Copyright 2024 The FIT SDK for Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package proto
import (
"math"
"reflect"
"strconv"
"unsafe"
"github.com/muktihari/fit/profile/basetype"
)
// stringptr is an identifier to distinguish a string pointer from a []byte pointer since both are *byte.
type stringptr *byte
// Type is Value's type
type Type byte
const (
TypeInvalid Type = iota
TypeBool
TypeInt8
TypeUint8
TypeInt16
TypeUint16
TypeInt32
TypeUint32
TypeInt64
TypeUint64
TypeFloat32
TypeFloat64
TypeString
TypeSliceBool
TypeSliceInt8
TypeSliceUint8
TypeSliceInt16
TypeSliceUint16
TypeSliceInt32
TypeSliceUint32
TypeSliceInt64
TypeSliceUint64
TypeSliceFloat32
TypeSliceFloat64
TypeSliceString
)
var typeStrings = [...]string{
"Invalid",
"Bool",
"Int8",
"Uint8",
"Int16",
"Uint16",
"Int32",
"Uint32",
"Int64",
"Uint64",
"Float32",
"Float64",
"String",
"SliceBool",
"SliceInt8",
"SliceUint8",
"SliceInt16",
"SliceUint16",
"SliceInt32",
"SliceUint32",
"SliceInt64",
"SliceUint64",
"SliceFloat32",
"SliceFloat64",
"SliceString",
}
func (t Type) String() string {
if t < Type(len(typeStrings)) {
return typeStrings[t]
}
return "proto.TypeInvalid(" + strconv.Itoa(int(t)) + ")"
}
// Value is a zero alloc implementation value that hold any FIT protocol value
// (value of primitive-types or slice of primitive-types).
//
// To compare two Values of not known type, compare the results of the Any method.
// Using == on two Values is disallowed.
type Value struct {
_ [0]func() // disallow ==
// num holds a numeric value when it's single value, and hold slice's len when it's a slice value.
num uint64
// any holds a Type when it's a single value, and hold a pointer to the slice when it's slice value.
//
// This implementation takes advantage of compiler interface optimization:
// - Compiler adds global [256]byte array called 'staticbytes' to every binary.
// - So putting single-byte value into an interface{} do not allocate.
//
// ref: https://commaok.xyz/post/interface-allocs
any any
}
// Return the underlying type the Value holds.
func (v Value) Type() Type {
switch typ := v.any.(type) {
case Type:
return typ
case stringptr:
return TypeString
case *bool:
return TypeSliceBool
case *int8:
return TypeSliceInt8
case *uint8:
return TypeSliceUint8
case *int16:
return TypeSliceInt16
case *uint16:
return TypeSliceUint16
case *int32:
return TypeSliceInt32
case *uint32:
return TypeSliceUint32
case *int64:
return TypeSliceInt64
case *uint64:
return TypeSliceUint64
case *float32:
return TypeSliceFloat32
case *float64:
return TypeSliceFloat64
case *string:
return TypeSliceString
}
return TypeInvalid
}
// Int8 returns Value as int8, if it's not a valid int8 value, it returns basetype.Sint8Invalid (0x7F).
func (v Value) Int8() int8 {
if v.any != TypeInt8 {
return basetype.Sint8Invalid
}
return int8(v.num)
}
// Bool returns Value as bool, if it's not a valid bool value, it returns false.
func (v Value) Bool() bool {
if v.any != TypeBool || v.num != 1 {
return false
}
return true
}
// Uint8 returns Value as uint8, if it's not a valid uint8 value, it returns basetype.Uint8Invalid (0xFF).
func (v Value) Uint8() uint8 {
if v.any != TypeUint8 {
return basetype.Uint8Invalid
}
return uint8(v.num)
}
// Uint8z returns Value as uint8, if it's not a valid uint8 value, it returns basetype.Uint8zInvalid (0).
func (v Value) Uint8z() uint8 {
if v.any != TypeUint8 {
return basetype.Uint8zInvalid
}
return uint8(v.num)
}
// Int16 returns Value as int16, if it's not a valid int16 value, it returns basetype.Sint16Invalid (0x7FFF).
func (v Value) Int16() int16 {
if v.any != TypeInt16 {
return basetype.Sint16Invalid
}
return int16(v.num)
}
// Uint16 returns Value as uint16, if it's not a valid uint16 value, it returns basetype.Uint16Invalid (0xFFFF).
func (v Value) Uint16() uint16 {
if v.any != TypeUint16 {
return basetype.Uint16Invalid
}
return uint16(v.num)
}
// Uint16z returns Value as uint16, if it's not a valid uint16 value, it returns basetype.Uint16zInvalid (0).
func (v Value) Uint16z() uint16 {
if v.any != TypeUint16 {
return basetype.Uint16zInvalid
}
return uint16(v.num)
}
// Int32 returns Value as int32, if it's not a valid int32 value, it returns basetype.Sint32Invalid (0x7FFFFFFF).
func (v Value) Int32() int32 {
if v.any != TypeInt32 {
return basetype.Sint32Invalid
}
return int32(v.num)
}
// Uint32 returns Value as uint32, if it's not a valid uint32 value, it returns basetype.Uint32Invalid (0xFFFFFFFF).
func (v Value) Uint32() uint32 {
if v.any != TypeUint32 {
return basetype.Uint32Invalid
}
return uint32(v.num)
}
// Uint32z returns Value as uint32, if it's not a valid uint32 value, it returns basetype.Uint32zInvalid (0).
func (v Value) Uint32z() uint32 {
if v.any != TypeUint32 {
return basetype.Uint32zInvalid
}
return uint32(v.num)
}
// Int64 returns Value as int64, if it's not a valid int64 value, it returns basetype.Sint64Invalid (0x7FFFFFFFFFFFFFFF).
func (v Value) Int64() int64 {
if v.any != TypeInt64 {
return basetype.Sint64Invalid
}
return int64(v.num)
}
// Uint64 returns Value as uint64, if it's not a valid uint64 value, it returns basetype.Uint64Invalid (0xFFFFFFFFFFFFFFFF).
func (v Value) Uint64() uint64 {
if v.any != TypeUint64 {
return basetype.Uint64Invalid
}
return v.num
}
// Uint64z returns Value as uint64, if it's not a valid uint64 value, it returns basetype.Uint64Invalid (0).
func (v Value) Uint64z() uint64 {
if v.any != TypeUint64 {
return basetype.Uint64zInvalid
}
return uint64(v.num)
}
// Float32 returns Value as float32, if it's not a valid float32 value, it returns basetype.Float32Invalid (0xFFFFFFFF) in float32 value.
func (v Value) Float32() float32 {
if v.any != TypeFloat32 {
return math.Float32frombits(basetype.Float32Invalid)
}
return math.Float32frombits(uint32(v.num))
}
// Float64 returns Value as float64, if it's not a valid float64 value, it returns basetype.Float64Invalid (0xFFFFFFFFFFFFFFFF) in float64 value.
func (v Value) Float64() float64 {
if v.any != TypeFloat64 {
return math.Float64frombits(basetype.Float64Invalid)
}
return math.Float64frombits(v.num)
}
// String returns Value as string, if it's not a valid string value, it returns basetype.StringInvalid.
// This should not be treated as a Go's String method, use Any() if you want to print the underlying value.
func (v Value) String() string {
ptr, ok := v.any.(stringptr)
if !ok {
return basetype.StringInvalid
}
return unsafe.String(ptr, v.num)
}
// SliceBool returns Value as []bool, if it's not a valid []bool value, it returns nil.
// The caller takes ownership of the returned value, so Value should no longer be used after this call,
// except the returned value is copied and the copied value is used instead.
func (v Value) SliceBool() []bool {
ptr, ok := v.any.(*bool)
if !ok {
return nil
}
return unsafe.Slice(ptr, v.num)
}
// SliceInt8 returns Value as []int8, if it's not a valid []int8 value, it returns nil.
// The caller takes ownership of the returned value, so Value should no longer be used after this call,
// except the returned value is copied and the copied value is used instead.
func (v Value) SliceInt8() []int8 {
ptr, ok := v.any.(*int8)
if !ok {
return nil
}
return unsafe.Slice(ptr, v.num)
}
// SliceUint8 returns Value as []uint8, if it's not a valid []uint8 value, it returns nil.
// The caller takes ownership of the returned value, so Value should no longer be used after this call,
// except the returned value is copied and the copied value is used instead.
func (v Value) SliceUint8() []uint8 {
ptr, ok := v.any.(*uint8)
if !ok {
return nil
}
return unsafe.Slice(ptr, v.num)
}
// SliceInt16 returns Value as []int16, if it's not a valid []int16 value, it returns nil.
// The caller takes ownership of the returned value, so Value should no longer be used after this call,
// except the returned value is copied and the copied value is used instead.
func (v Value) SliceInt16() []int16 {
ptr, ok := v.any.(*int16)
if !ok {
return nil
}
return unsafe.Slice(ptr, v.num)
}
// SliceUint16 returns Value as []uint16, if it's not a valid []uint16 value, it returns nil.
// The caller takes ownership of the returned value, so Value should no longer be used after this call,
// except the returned value is copied and the copied value is used instead.
func (v Value) SliceUint16() []uint16 {
ptr, ok := v.any.(*uint16)
if !ok {
return nil
}
return unsafe.Slice(ptr, v.num)
}
// SliceInt32 returns Value as []int32, if it's not a valid []int32 value, it returns nil.
// The caller takes ownership of the returned value, so Value should no longer be used after this call,
// except the returned value is copied and the copied value is used instead.
func (v Value) SliceInt32() []int32 {
ptr, ok := v.any.(*int32)
if !ok {
return nil
}
return unsafe.Slice(ptr, v.num)
}
// SliceUint32 returns Value as []uint32, if it's not a valid []uint32 value, it returns nil.
// The caller takes ownership of the returned value, so Value should no longer be used after this call,
// except the returned value is copied and the copied value is used instead.
func (v Value) SliceUint32() []uint32 {
ptr, ok := v.any.(*uint32)
if !ok {
return nil
}
return unsafe.Slice(ptr, v.num)
}
// SliceInt64 returns Value as []int64, if it's not a valid []int64 value, it returns nil.
// The caller takes ownership of the returned value, so Value should no longer be used after this call,
// except the returned value is copied and the copied value is used instead.
func (v Value) SliceInt64() []int64 {
ptr, ok := v.any.(*int64)
if !ok {
return nil
}
return unsafe.Slice(ptr, v.num)
}
// SliceUint64 returns Value as []uint64, if it's not a valid []uint64 value, it returns nil.
// The caller takes ownership of the returned value, so Value should no longer be used after this call,
// except the returned value is copied and the copied value is used instead.
func (v Value) SliceUint64() []uint64 {
ptr, ok := v.any.(*uint64)
if !ok {
return nil
}
return unsafe.Slice(ptr, v.num)
}
// SliceFloat32 returns Value as []float32, if it's not a valid []float32 value, it returns nil.
// The caller takes ownership of the returned value, so Value should no longer be used after this call,
// except the returned value is copied and the copied value is used instead.
func (v Value) SliceFloat32() []float32 {
ptr, ok := v.any.(*float32)
if !ok {
return nil
}
return unsafe.Slice(ptr, v.num)
}
// SliceFloat64 returns Value as []float64, if it's not a valid []float64 value, it returns nil.
// The caller takes ownership of the returned value, so Value should no longer be used after this call,
// except the returned value is copied and the copied value is used instead.
func (v Value) SliceFloat64() []float64 {
ptr, ok := v.any.(*float64)
if !ok {
return nil
}
return unsafe.Slice(ptr, v.num)
}
// SliceString returns Value as []string, if it's not a valid []string value, it returns nil.
// The caller takes ownership of the returned value, so Value should no longer be used after this call,
// except the returned value is copied and the copied value is used instead.
func (v Value) SliceString() []string {
ptr, ok := v.any.(*string)
if !ok {
return nil
}
return unsafe.Slice(ptr, v.num)
}
// Any returns Value's underlying value. If the underlying value is a slice, the caller takes ownership of that slice value,
// so Value should no longer be used after this call, except the returned value is copied and the copied value is used instead.
func (v Value) Any() any {
switch v.Type() {
case TypeBool:
return v.Bool()
case TypeInt8:
return v.Int8()
case TypeUint8:
return v.Uint8()
case TypeInt16:
return v.Int16()
case TypeUint16:
return v.Uint16()
case TypeInt32:
return v.Int32()
case TypeUint32:
return v.Uint32()
case TypeInt64:
return v.Int64()
case TypeUint64:
return v.Uint64()
case TypeFloat32:
return v.Float32()
case TypeFloat64:
return v.Float64()
case TypeString:
return v.String()
case TypeSliceBool:
return v.SliceBool()
case TypeSliceInt8:
return v.SliceInt8()
case TypeSliceUint8:
return v.SliceUint8()
case TypeSliceInt16:
return v.SliceInt16()
case TypeSliceUint16:
return v.SliceUint16()
case TypeSliceInt32:
return v.SliceInt32()
case TypeSliceUint32:
return v.SliceUint32()
case TypeSliceInt64:
return v.SliceInt64()
case TypeSliceUint64:
return v.SliceUint64()
case TypeSliceFloat32:
return v.SliceFloat32()
case TypeSliceFloat64:
return v.SliceFloat64()
case TypeSliceString:
return v.SliceString()
}
return nil
}
func (v Value) Align(t basetype.BaseType) bool {
switch v.Type() {
case TypeBool, TypeSliceBool:
return t == basetype.Enum
case TypeInt8, TypeSliceInt8:
return t == basetype.Sint8
case TypeUint8, TypeSliceUint8:
return t == basetype.Enum ||
t == basetype.Byte ||
t == basetype.Uint8 ||
t == basetype.Uint8z
case TypeInt16, TypeSliceInt16:
return t == basetype.Sint16
case TypeUint16, TypeSliceUint16:
return t == basetype.Uint16 || t == basetype.Uint16z
case TypeInt32, TypeSliceInt32:
return t == basetype.Sint32
case TypeUint32, TypeSliceUint32:
return t == basetype.Uint32 || t == basetype.Uint32z
case TypeInt64, TypeSliceInt64:
return t == basetype.Sint64
case TypeUint64, TypeSliceUint64:
return t == basetype.Uint64 || t == basetype.Uint64z
case TypeFloat32, TypeSliceFloat32:
return t == basetype.Float32
case TypeFloat64, TypeSliceFloat64:
return t == basetype.Float64
case TypeString, TypeSliceString:
return t == basetype.String
}
return false
}
// Valid checks whether the Value is valid based on given basetype. This does not verify whether the Type of
// the Value aligns with the provided BaseType. For slices, even though only one element is valid, the Value will be counted a valid value.
//
// Special case: bool or slice of bool will always be valid since bool type is often used as a flag and
// there are only two possibility (true/false).
func (v Value) Valid(t basetype.BaseType) bool {
var invalidCount int
switch v.Type() {
case TypeBool, TypeSliceBool:
return true // Mark as valid
case TypeInt8:
return int8(v.num) != basetype.Sint8Invalid
case TypeUint8:
val := uint8(v.num)
switch t {
case basetype.Enum:
return val != basetype.EnumInvalid
case basetype.Byte:
return val != basetype.ByteInvalid
case basetype.Uint8:
return val != basetype.Uint8Invalid
case basetype.Uint8z:
return val != basetype.Uint8zInvalid
}
return false
case TypeInt16:
return int16(v.num) != basetype.Sint16Invalid
case TypeUint16:
if t == basetype.Uint16z {
return uint16(v.num) != basetype.Uint16zInvalid
}
return uint16(v.num) != basetype.Uint16Invalid
case TypeInt32:
return int32(v.num) != basetype.Sint32Invalid
case TypeUint32:
if t == basetype.Uint32z {
return uint32(v.num) != basetype.Uint32zInvalid
}
return uint32(v.num) != basetype.Uint32Invalid
case TypeFloat32:
return uint32(v.num) != basetype.Float32Invalid
case TypeFloat64:
return v.num != basetype.Float64Invalid
case TypeInt64:
return int64(v.num) != basetype.Sint64Invalid
case TypeUint64:
if t == basetype.Uint64z {
return v.num != basetype.Uint64zInvalid
}
return v.num != basetype.Uint64Invalid
case TypeString:
s := unsafe.String(v.any.(stringptr), v.num)
return s != basetype.StringInvalid && s != "\x00"
case TypeSliceInt8:
vals := v.SliceInt8()
for i := range vals {
if vals[i] == basetype.Sint8Invalid {
invalidCount++
}
}
return invalidCount != len(vals)
case TypeSliceUint8:
vals := v.SliceUint8()
for i := range vals {
if vals[i] == basetype.Uint8Invalid {
invalidCount++
}
}
return invalidCount != len(vals)
case TypeSliceInt16:
vals := v.SliceInt16()
for i := range vals {
if vals[i] == basetype.Sint16Invalid {
invalidCount++
}
}
return invalidCount != len(vals)
case TypeSliceUint16:
vals := v.SliceUint16()
for i := range vals {
if vals[i] == basetype.Uint16Invalid {
invalidCount++
}
}
return invalidCount != len(vals)
case TypeSliceInt32:
vals := v.SliceInt32()
for i := range vals {
if vals[i] == basetype.Sint32Invalid {
invalidCount++
}
}
return invalidCount != len(vals)
case TypeSliceUint32:
vals := v.SliceUint32()
for i := range vals {
if vals[i] == basetype.Uint32Invalid {
invalidCount++
}
}
return invalidCount != len(vals)
case TypeSliceFloat32:
vals := v.SliceFloat32()
for i := range vals {
if math.Float32bits(vals[i]) == basetype.Float32Invalid {
invalidCount++
}
}
return invalidCount != len(vals)
case TypeSliceFloat64:
vals := v.SliceFloat64()
for i := range vals {
if math.Float64bits(vals[i]) == basetype.Float64Invalid {
invalidCount++
}
}
return invalidCount != len(vals)
case TypeSliceInt64:
vals := v.SliceInt64()
for i := range vals {
if vals[i] == basetype.Sint64Invalid {
invalidCount++
}
}
return invalidCount != len(vals)
case TypeSliceUint64:
vals := v.SliceUint64()
for i := range vals {
if vals[i] == basetype.Uint64Invalid {
invalidCount++
}
}
return invalidCount != len(vals)
case TypeSliceString:
vals := v.SliceString()
for i := range vals {
if vals[i] == basetype.StringInvalid || vals[i] == "\x00" {
invalidCount++
}
}
return invalidCount != len(vals)
}
return false
}
// Bool converts bool as Value.
func Bool(v bool) Value {
var num uint64
if v {
num = 1
}
return Value{num: num, any: TypeBool}
}
// Int8 converts int8 as Value.
func Int8(v int8) Value {
return Value{num: uint64(v), any: TypeInt8}
}
// Uint8 converts uint8 as Value.
func Uint8(v uint8) Value {
return Value{num: uint64(v), any: TypeUint8}
}
// Int16 converts int16 as Value.
func Int16(v int16) Value {
return Value{num: uint64(v), any: TypeInt16}
}
// Uint16 converts uint16 as Value.
func Uint16(v uint16) Value {
return Value{num: uint64(v), any: TypeUint16}
}
// Int32 converts int32 as Value.
func Int32(v int32) Value {
return Value{num: uint64(v), any: TypeInt32}
}
// Uint32 converts uint32 as Value.
func Uint32(v uint32) Value {
return Value{num: uint64(v), any: TypeUint32}
}
// Int64 converts int64 as Value.
func Int64(v int64) Value {
return Value{num: uint64(v), any: TypeInt64}
}
// Uint64 converts uint64 as Value.
func Uint64(v uint64) Value {
return Value{num: v, any: TypeUint64}
}
// Float32 converts float32 as Value.
func Float32(v float32) Value {
return Value{num: uint64(math.Float32bits(v)), any: TypeFloat32}
}
// Float64 converts float64 as Value.
func Float64(v float64) Value {
return Value{num: math.Float64bits(v), any: TypeFloat64}
}
// String converts string as Value.
func String(v string) Value {
return Value{num: uint64(len(v)), any: stringptr(unsafe.StringData(v))}
}
// HACK: The use of *(*[]ArbitraryType)(unsafe.Pointer(&slice) below should be safe (in unsafe world) since we only use it to
// temporarily cast the type to make unsafe.SliceData return the pointer as *ArbitraryType. The actual slice is handled by
// unsafe.SliceData so we don't lose the data.
// SliceBool converts []bool as Value. This takes ownership of s, and the caller should not use s after this call.
func SliceBool[S []E, E ~bool](s S) Value {
return Value{num: uint64(len(s)), any: unsafe.SliceData(*(*[]bool)(unsafe.Pointer(&s)))}
}
// SliceInt8 converts []int8 as Value. This takes ownership of s, and the caller should not use s after this call.
func SliceInt8[S []E, E ~int8](s S) Value {
return Value{num: uint64(len(s)), any: unsafe.SliceData(*(*[]int8)(unsafe.Pointer(&s)))}
}
// SliceUint8 converts []uint8 as Value. This takes ownership of s, and the caller should not use s after this call.
func SliceUint8[S []E, E ~uint8](s S) Value {
return Value{num: uint64(len(s)), any: unsafe.SliceData(*(*[]uint8)(unsafe.Pointer(&s)))}
}
// SliceInt16 converts []int16 as Value. This takes ownership of s, and the caller should not use s after this call.
func SliceInt16[S []E, E ~int16](s S) Value {
return Value{num: uint64(len(s)), any: unsafe.SliceData(*(*[]int16)(unsafe.Pointer(&s)))}
}
// SliceUint16 converts []uint16 as Value. This takes ownership of s, and the caller should not use s after this call.
func SliceUint16[S []E, E ~uint16](s S) Value {
return Value{num: uint64(len(s)), any: unsafe.SliceData(*(*[]uint16)(unsafe.Pointer(&s)))}
}
// SliceInt32 converts []int32 as Value. This takes ownership of s, and the caller should not use s after this call.
func SliceInt32[S []E, E ~int32](s S) Value {
return Value{num: uint64(len(s)), any: unsafe.SliceData(*(*[]int32)(unsafe.Pointer(&s)))}
}
// SliceUint32 converts []uint32 as Value. This takes ownership of s, and the caller should not use s after this call.
func SliceUint32[S []E, E ~uint32](s S) Value {
return Value{num: uint64(len(s)), any: unsafe.SliceData(*(*[]uint32)(unsafe.Pointer(&s)))}
}
// SliceInt64 converts []int64 as Value. This takes ownership of s, and the caller should not use s after this call.
func SliceInt64[S []E, E ~int64](s S) Value {
return Value{num: uint64(len(s)), any: unsafe.SliceData(*(*[]int64)(unsafe.Pointer(&s)))}
}
// SliceUint64 converts []uint64 as Value. This takes ownership of s, and the caller should not use s after this call.
func SliceUint64[S []E, E ~uint64](s S) Value {
return Value{num: uint64(len(s)), any: unsafe.SliceData(*(*[]uint64)(unsafe.Pointer(&s)))}
}
// SliceFloat32 converts []float32 as Value. This takes ownership of s, and the caller should not use s after this call.
func SliceFloat32[S []E, E ~float32](s S) Value {
return Value{num: uint64(len(s)), any: unsafe.SliceData(*(*[]float32)(unsafe.Pointer(&s)))}
}
// SliceFloat64 converts []float64 as Value. This takes ownership of s, and the caller should not use s after this call.
func SliceFloat64[S []E, E ~float64](s S) Value {
return Value{num: uint64(len(s)), any: unsafe.SliceData(*(*[]float64)(unsafe.Pointer(&s)))}
}
// SliceString converts []string as Value. This takes ownership of s, and the caller should not use s after this call.
func SliceString[S []E, E ~string](s S) Value {
return Value{num: uint64(len(s)), any: unsafe.SliceData(*(*[]string)(unsafe.Pointer(&s)))}
}
// Any converts any value into Value. If the given v is not a primitive-type value (or a slice of primitive-type)
// it will determine it using reflection, and if it's a non-primitive-type slice it will make 1 alloc.
// - If v is not supported such as int, uint, []int, []uint, []any, slice with zero len, etc. Value with TypeInvalid is returned.
// - If v is a primitive-type slice, this will take ownership of v, and the caller should not use v after this call.
func Any(v any) Value {
switch val := v.(type) { // Fast path
case int, uint, []int, []uint, []any: // Fast return on invalid value
return Value{any: TypeInvalid}
case Value:
return val
case bool:
return Bool(val)
case int8:
return Int8(val)
case uint8:
return Uint8(val)
case int16:
return Int16(val)
case uint16:
return Uint16(val)
case int32:
return Int32(val)
case uint32:
return Uint32(val)
case int64:
return Int64(val)
case uint64:
return Uint64(val)
case float32:
return Float32(val)
case float64:
return Float64(val)
case string:
return String(val)
case []bool:
return SliceBool(val)
case []int8:
return SliceInt8(val)
case []uint8:
return SliceUint8(val)
case []int16:
return SliceInt16(val)
case []uint16:
return SliceUint16(val)
case []int32:
return SliceInt32(val)
case []uint32:
return SliceUint32(val)
case []int64:
return SliceInt64(val)
case []uint64:
return SliceUint64(val)
case []float32:
return SliceFloat32(val)
case []float64:
return SliceFloat64(val)
case []string:
return SliceString(val)
}
// Fallback to reflection.
rv := reflect.Indirect(reflect.ValueOf(v))
switch rv.Kind() {
case reflect.Bool:
return Bool(rv.Bool())
case reflect.Int8:
return Int8(int8(rv.Int()))
case reflect.Uint8:
return Uint8(uint8(rv.Uint()))
case reflect.Int16:
return Int16(int16(rv.Int()))
case reflect.Uint16:
return Uint16(uint16(rv.Uint()))
case reflect.Int32:
return Int32(int32(rv.Int()))
case reflect.Uint32:
return Uint32(uint32(rv.Uint()))
case reflect.Int64:
return Int64(int64(rv.Int()))
case reflect.Uint64:
return Uint64(uint64(rv.Uint()))
case reflect.Float32:
return Float32(float32(rv.Float()))
case reflect.Float64:
return Float64(float64(rv.Float()))
case reflect.String:
return String(rv.String())
case reflect.Slice: // Always alloc since it makes new slice.
if rv.Len() == 0 {
return Value{any: TypeInvalid}
}
switch rv.Index(0).Kind() {
case reflect.Bool:
var vals = make([]bool, rv.Len())
for i := 0; i < rv.Len(); i++ {
vals[i] = rv.Index(i).Bool()
}
return SliceBool(vals)
case reflect.Int8:
var vals = make([]int8, rv.Len())
for i := 0; i < rv.Len(); i++ {
vals[i] = int8(rv.Index(i).Int())
}
return SliceInt8(vals)
case reflect.Uint8:
var vals = make([]uint8, rv.Len())
for i := 0; i < rv.Len(); i++ {
vals[i] = uint8(rv.Index(i).Uint())
}
return SliceUint8(vals)
case reflect.Int16:
var vals = make([]int16, rv.Len())
for i := 0; i < rv.Len(); i++ {
vals[i] = int16(rv.Index(i).Int())
}
return SliceInt16(vals)
case reflect.Uint16:
var vals = make([]uint16, rv.Len())
for i := 0; i < rv.Len(); i++ {
vals[i] = uint16(rv.Index(i).Uint())
}
return SliceUint16(vals)
case reflect.Int32:
var vals = make([]int32, rv.Len())
for i := 0; i < rv.Len(); i++ {
vals[i] = int32(rv.Index(i).Int())
}
return SliceInt32(vals)
case reflect.Uint32:
var vals = make([]uint32, rv.Len())
for i := 0; i < rv.Len(); i++ {
vals[i] = uint32(rv.Index(i).Uint())
}
return SliceUint32(vals)
case reflect.Int64:
var vals = make([]int64, rv.Len())
for i := 0; i < rv.Len(); i++ {
vals[i] = int64(rv.Index(i).Int())
}
return SliceInt64(vals)
case reflect.Uint64:
var vals = make([]uint64, rv.Len())
for i := 0; i < rv.Len(); i++ {
vals[i] = uint64(rv.Index(i).Uint())
}
return SliceUint64(vals)
case reflect.Float32:
var vals = make([]float32, rv.Len())
for i := 0; i < rv.Len(); i++ {
vals[i] = float32(rv.Index(i).Float())
}
return SliceFloat32(vals)
case reflect.Float64:
var vals = make([]float64, rv.Len())
for i := 0; i < rv.Len(); i++ {
vals[i] = float64(rv.Index(i).Float())
}
return SliceFloat64(vals)
case reflect.String:
var vals = make([]string, rv.Len())
for i := 0; i < rv.Len(); i++ {
vals[i] = string(rv.Index(i).String())
}
return SliceString(vals)
}
}
return Value{any: TypeInvalid}
}
// Sizeof returns the size of val in bytes. For every string in Value, if the last index of the string is not '\x00', size += 1.
func Sizeof(val Value, baseType basetype.BaseType) int {
return lenof(val) * int(baseType.Size())
}
func lenof(val Value) int {
switch val.Type() {
case TypeInvalid:
return 0
case TypeBool,
TypeInt8,
TypeUint8,
TypeInt16,
TypeUint16,
TypeInt32,
TypeUint32,
TypeFloat32,
TypeFloat64,
TypeInt64,
TypeUint64:
return 1
case TypeString:
s := val.String()
if len(s) == 0 {
return 1 // utf-8 null terminated string
}
if l := len(s); l > 0 && s[l-1] == '\x00' {
return l
}
return len(s) + 1
case TypeSliceString:
vs := val.SliceString()
var size int
for i := range vs {
if len(vs[i]) == 0 {
size += 1 // utf-8 null terminated string
continue
}
if l := len(vs[i]); l > 0 && vs[i][l-1] == '\x00' {
size += l
continue
}
size += len(vs[i]) + 1
}
if size == 0 {
return 1 // utf-8 null terminated string
}
return size
}
return int(val.num) // other slices
}