-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathflexbuffers.py
More file actions
1592 lines (1284 loc) · 43.4 KB
/
flexbuffers.py
File metadata and controls
1592 lines (1284 loc) · 43.4 KB
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
# Lint as: python3
# Copyright 2020 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Implementation of FlexBuffers binary format.
For more info check https://google.github.io/flatbuffers/flexbuffers.html and
corresponding C++ implementation at
https://github.com/google/flatbuffers/blob/master/include/flatbuffers/flexbuffers.h
"""
# pylint: disable=invalid-name
# TODO(dkovalev): Add type hints everywhere, so tools like pytypes could work.
import array
import contextlib
import enum
import struct
__all__ = ('Type', 'Builder', 'GetRoot', 'Dumps', 'Loads')
class BitWidth(enum.IntEnum):
"""Supported bit widths of value types.
These are used in the lower 2 bits of a type field to determine the size of
the elements (and or size field) of the item pointed to (e.g. vector).
"""
W8 = 0 # 2^0 = 1 byte
W16 = 1 # 2^1 = 2 bytes
W32 = 2 # 2^2 = 4 bytes
W64 = 3 # 2^3 = 8 bytes
@staticmethod
def U(value):
"""Returns the minimum `BitWidth` to encode unsigned integer value."""
assert value >= 0
if value < (1 << 8):
return BitWidth.W8
elif value < (1 << 16):
return BitWidth.W16
elif value < (1 << 32):
return BitWidth.W32
elif value < (1 << 64):
return BitWidth.W64
else:
raise ValueError('value is too big to encode: %s' % value)
@staticmethod
def I(value):
"""Returns the minimum `BitWidth` to encode signed integer value."""
# -2^(n-1) <= value < 2^(n-1)
# -2^n <= 2 * value < 2^n
# 2 * value < 2^n, when value >= 0 or 2 * (-value) <= 2^n, when value < 0
# 2 * value < 2^n, when value >= 0 or 2 * (-value) - 1 < 2^n, when value < 0
#
# if value >= 0:
# return BitWidth.U(2 * value)
# else:
# return BitWidth.U(2 * (-value) - 1) # ~x = -x - 1
value *= 2
return BitWidth.U(value if value >= 0 else ~value)
@staticmethod
def F(value):
"""Returns the `BitWidth` to encode floating point value."""
if struct.unpack('<f', struct.pack('<f', value))[0] == value:
return BitWidth.W32
return BitWidth.W64
@staticmethod
def B(byte_width):
return {1: BitWidth.W8, 2: BitWidth.W16, 4: BitWidth.W32, 8: BitWidth.W64}[
byte_width
]
I = {1: 'b', 2: 'h', 4: 'i', 8: 'q'} # Integer formats
U = {1: 'B', 2: 'H', 4: 'I', 8: 'Q'} # Unsigned integer formats
F = {4: 'f', 8: 'd'} # Floating point formats
def _Unpack(fmt, buf):
return struct.unpack('<%s' % fmt[len(buf)], buf)[0]
def _UnpackVector(fmt, buf, length):
byte_width = len(buf) // length
return struct.unpack('<%d%s' % (length, fmt[byte_width]), buf)
def _Pack(fmt, value, byte_width):
return struct.pack('<%s' % fmt[byte_width], value)
def _PackVector(fmt, values, byte_width):
return struct.pack('<%d%s' % (len(values), fmt[byte_width]), *values)
def _Mutate(fmt, buf, value, byte_width, value_bit_width):
if (1 << value_bit_width) <= byte_width:
buf[:byte_width] = _Pack(fmt, value, byte_width)
return True
return False
# Computes how many bytes you'd have to pad to be able to write an
# "scalar_size" scalar if the buffer had grown to "buf_size",
# "scalar_size" is a power of two.
def _PaddingBytes(buf_size, scalar_size):
# ((buf_size + (scalar_size - 1)) // scalar_size) * scalar_size - buf_size
return -buf_size & (scalar_size - 1)
def _ShiftSlice(s, offset, length):
start = offset + (0 if s.start is None else s.start)
stop = offset + (length if s.stop is None else s.stop)
return slice(start, stop, s.step)
# https://en.cppreference.com/w/cpp/algorithm/lower_bound
def _LowerBound(values, value, pred):
"""Implementation of C++ std::lower_bound() algorithm."""
first, last = 0, len(values)
count = last - first
while count > 0:
i = first
step = count // 2
i += step
if pred(values[i], value):
i += 1
first = i
count -= step + 1
else:
count = step
return first
# https://en.cppreference.com/w/cpp/algorithm/binary_search
def _BinarySearch(values, value, pred=lambda x, y: x < y):
"""Implementation of C++ std::binary_search() algorithm."""
index = _LowerBound(values, value, pred)
if index != len(values) and not pred(value, values[index]):
return index
return -1
class Type(enum.IntEnum):
"""Supported types of encoded data.
These are used as the upper 6 bits of a type field to indicate the actual
type.
"""
NULL = 0
INT = 1
UINT = 2
FLOAT = 3
# Types above stored inline, types below store an offset.
KEY = 4
STRING = 5
INDIRECT_INT = 6
INDIRECT_UINT = 7
INDIRECT_FLOAT = 8
MAP = 9
VECTOR = 10 # Untyped.
VECTOR_INT = 11 # Typed any size (stores no type table).
VECTOR_UINT = 12
VECTOR_FLOAT = 13
VECTOR_KEY = 14
# DEPRECATED, use VECTOR or VECTOR_KEY instead.
# Read test.cpp/FlexBuffersDeprecatedTest() for details on why.
VECTOR_STRING_DEPRECATED = 15
VECTOR_INT2 = 16 # Typed tuple (no type table, no size field).
VECTOR_UINT2 = 17
VECTOR_FLOAT2 = 18
VECTOR_INT3 = 19 # Typed triple (no type table, no size field).
VECTOR_UINT3 = 20
VECTOR_FLOAT3 = 21
VECTOR_INT4 = 22 # Typed quad (no type table, no size field).
VECTOR_UINT4 = 23
VECTOR_FLOAT4 = 24
BLOB = 25
BOOL = 26
VECTOR_BOOL = 36 # To do the same type of conversion of type to vector type
@staticmethod
def Pack(type_, bit_width):
return (int(type_) << 2) | bit_width
@staticmethod
def Unpack(packed_type):
return 1 << (packed_type & 0b11), Type(packed_type >> 2)
@staticmethod
def IsInline(type_):
return type_ <= Type.FLOAT or type_ == Type.BOOL
@staticmethod
def IsTypedVector(type_):
return (
Type.VECTOR_INT <= type_ <= Type.VECTOR_STRING_DEPRECATED
or type_ == Type.VECTOR_BOOL
)
@staticmethod
def IsTypedVectorElementType(type_):
return Type.INT <= type_ <= Type.STRING or type_ == Type.BOOL
@staticmethod
def ToTypedVectorElementType(type_):
if not Type.IsTypedVector(type_):
raise ValueError('must be typed vector type')
return Type(type_ - Type.VECTOR_INT + Type.INT)
@staticmethod
def IsFixedTypedVector(type_):
return Type.VECTOR_INT2 <= type_ <= Type.VECTOR_FLOAT4
@staticmethod
def IsFixedTypedVectorElementType(type_):
return Type.INT <= type_ <= Type.FLOAT
@staticmethod
def ToFixedTypedVectorElementType(type_):
if not Type.IsFixedTypedVector(type_):
raise ValueError('must be fixed typed vector type')
# 3 types each, starting from length 2.
fixed_type = type_ - Type.VECTOR_INT2
return Type(fixed_type % 3 + Type.INT), fixed_type // 3 + 2
@staticmethod
def ToTypedVector(element_type, fixed_len=0):
"""Converts element type to corresponding vector type.
Args:
element_type: vector element type
fixed_len: number of elements: 0 for typed vector; 2, 3, or 4 for fixed
typed vector.
Returns:
Typed vector type or fixed typed vector type.
"""
if fixed_len == 0:
if not Type.IsTypedVectorElementType(element_type):
raise ValueError('must be typed vector element type')
else:
if not Type.IsFixedTypedVectorElementType(element_type):
raise ValueError('must be fixed typed vector element type')
offset = element_type - Type.INT
if fixed_len == 0:
return Type(offset + Type.VECTOR_INT) # TypedVector
elif fixed_len == 2:
return Type(offset + Type.VECTOR_INT2) # FixedTypedVector
elif fixed_len == 3:
return Type(offset + Type.VECTOR_INT3) # FixedTypedVector
elif fixed_len == 4:
return Type(offset + Type.VECTOR_INT4) # FixedTypedVector
else:
raise ValueError('unsupported fixed_len: %s' % fixed_len)
class Buf:
"""Class to access underlying buffer object starting from the given offset."""
def __init__(self, buf, offset):
self._buf = buf
self._offset = offset if offset >= 0 else len(buf) + offset
self._length = len(buf) - self._offset
def __getitem__(self, key):
if isinstance(key, slice):
return self._buf[_ShiftSlice(key, self._offset, self._length)]
elif isinstance(key, int):
return self._buf[self._offset + key]
else:
raise TypeError('invalid key type')
def __setitem__(self, key, value):
if isinstance(key, slice):
self._buf[_ShiftSlice(key, self._offset, self._length)] = value
elif isinstance(key, int):
self._buf[self._offset + key] = key
else:
raise TypeError('invalid key type')
def __repr__(self):
return 'buf[%d:]' % self._offset
def Find(self, sub):
"""Returns the lowest index where the sub subsequence is found."""
return self._buf[self._offset :].find(sub)
def Slice(self, offset):
"""Returns new `Buf` which starts from the given offset."""
return Buf(self._buf, self._offset + offset)
def Indirect(self, offset, byte_width):
"""Return new `Buf` based on the encoded offset (indirect encoding)."""
return self.Slice(offset - _Unpack(U, self[offset : offset + byte_width]))
class Object:
"""Base class for all non-trivial data accessors."""
__slots__ = '_buf', '_byte_width'
def __init__(self, buf, byte_width):
self._buf = buf
self._byte_width = byte_width
@property
def ByteWidth(self):
return self._byte_width
class Sized(Object):
"""Base class for all data accessors which need to read encoded size."""
__slots__ = ('_size',)
def __init__(self, buf, byte_width, size=0):
super().__init__(buf, byte_width)
if size == 0:
self._size = _Unpack(U, self.SizeBytes)
else:
self._size = size
@property
def SizeBytes(self):
return self._buf[-self._byte_width : 0]
def __len__(self):
return self._size
class Blob(Sized):
"""Data accessor for the encoded blob bytes."""
__slots__ = ()
@property
def Bytes(self):
return self._buf[0 : len(self)]
def __repr__(self):
return 'Blob(%s, size=%d)' % (self._buf, len(self))
class String(Sized):
"""Data accessor for the encoded string bytes."""
__slots__ = ()
@property
def Bytes(self):
return self._buf[0 : len(self)]
def Mutate(self, value):
"""Mutates underlying string bytes in place.
Args:
value: New string to replace the existing one. New string must have less
or equal UTF-8-encoded bytes than the existing one to successfully
mutate underlying byte buffer.
Returns:
Whether the value was mutated or not.
"""
encoded = value.encode('utf-8')
n = len(encoded)
if n <= len(self):
self._buf[-self._byte_width : 0] = _Pack(U, n, self._byte_width)
self._buf[0:n] = encoded
self._buf[n : len(self)] = bytearray(len(self) - n)
return True
return False
def __str__(self):
return self.Bytes.decode('utf-8')
def __repr__(self):
return 'String(%s, size=%d)' % (self._buf, len(self))
class Key(Object):
"""Data accessor for the encoded key bytes."""
__slots__ = ()
def __init__(self, buf, byte_width):
assert byte_width == 1
super().__init__(buf, byte_width)
@property
def Bytes(self):
return self._buf[0 : len(self)]
def __len__(self):
return self._buf.Find(0)
def __str__(self):
return self.Bytes.decode('ascii')
def __repr__(self):
return 'Key(%s, size=%d)' % (self._buf, len(self))
class Vector(Sized):
"""Data accessor for the encoded vector bytes."""
__slots__ = ()
def __getitem__(self, index):
if index < 0 or index >= len(self):
raise IndexError(
'vector index %s is out of [0, %d) range' % (index, len(self))
)
packed_type = self._buf[len(self) * self._byte_width + index]
buf = self._buf.Slice(index * self._byte_width)
return Ref.PackedType(buf, self._byte_width, packed_type)
@property
def Value(self):
"""Returns the underlying encoded data as a list object."""
return [e.Value for e in self]
def __repr__(self):
return 'Vector(%s, byte_width=%d, size=%d)' % (
self._buf,
self._byte_width,
self._size,
)
class TypedVector(Sized):
"""Data accessor for the encoded typed vector or fixed typed vector bytes."""
__slots__ = '_element_type', '_size'
def __init__(self, buf, byte_width, element_type, size=0):
super().__init__(buf, byte_width, size)
if element_type == Type.STRING:
# These can't be accessed as strings, since we don't know the bit-width
# of the size field, see the declaration of
# FBT_VECTOR_STRING_DEPRECATED above for details.
# We change the type here to be keys, which are a subtype of strings,
# and will ignore the size field. This will truncate strings with
# embedded nulls.
element_type = Type.KEY
self._element_type = element_type
@property
def Bytes(self):
return self._buf[: self._byte_width * len(self)]
@property
def ElementType(self):
return self._element_type
def __getitem__(self, index):
if index < 0 or index >= len(self):
raise IndexError(
'vector index %s is out of [0, %d) range' % (index, len(self))
)
buf = self._buf.Slice(index * self._byte_width)
return Ref(buf, self._byte_width, 1, self._element_type)
@property
def Value(self):
"""Returns underlying data as list object."""
if not self:
return []
if self._element_type is Type.BOOL:
return [bool(e) for e in _UnpackVector(U, self.Bytes, len(self))]
elif self._element_type is Type.INT:
return list(_UnpackVector(I, self.Bytes, len(self)))
elif self._element_type is Type.UINT:
return list(_UnpackVector(U, self.Bytes, len(self)))
elif self._element_type is Type.FLOAT:
return list(_UnpackVector(F, self.Bytes, len(self)))
elif self._element_type is Type.KEY:
return [e.AsKey for e in self]
elif self._element_type is Type.STRING:
return [e.AsString for e in self]
else:
raise TypeError('unsupported element_type: %s' % self._element_type)
def __repr__(self):
return 'TypedVector(%s, byte_width=%d, element_type=%s, size=%d)' % (
self._buf,
self._byte_width,
self._element_type,
self._size,
)
class Map(Vector):
"""Data accessor for the encoded map bytes."""
@staticmethod
def CompareKeys(a, b):
if isinstance(a, Ref):
a = a.AsKeyBytes
if isinstance(b, Ref):
b = b.AsKeyBytes
return a < b
def __getitem__(self, key):
if isinstance(key, int):
return super().__getitem__(key)
index = _BinarySearch(self.Keys, key.encode('ascii'), self.CompareKeys)
if index != -1:
return super().__getitem__(index)
raise KeyError(key)
@property
def Keys(self):
byte_width = _Unpack(
U, self._buf[-2 * self._byte_width : -self._byte_width]
)
buf = self._buf.Indirect(-3 * self._byte_width, self._byte_width)
return TypedVector(buf, byte_width, Type.KEY)
@property
def Values(self):
return Vector(self._buf, self._byte_width)
@property
def Value(self):
return {k.Value: v.Value for k, v in zip(self.Keys, self.Values)}
def __repr__(self):
return 'Map(%s, size=%d)' % (self._buf, len(self))
class Ref:
"""Data accessor for the encoded data bytes."""
__slots__ = '_buf', '_parent_width', '_byte_width', '_type'
@staticmethod
def PackedType(buf, parent_width, packed_type):
byte_width, type_ = Type.Unpack(packed_type)
return Ref(buf, parent_width, byte_width, type_)
def __init__(self, buf, parent_width, byte_width, type_):
self._buf = buf
self._parent_width = parent_width
self._byte_width = byte_width
self._type = type_
def __repr__(self):
return 'Ref(%s, parent_width=%d, byte_width=%d, type_=%s)' % (
self._buf,
self._parent_width,
self._byte_width,
self._type,
)
@property
def _Bytes(self):
return self._buf[: self._parent_width]
def _ConvertError(self, target_type):
raise TypeError('cannot convert %s to %s' % (self._type, target_type))
def _Indirect(self):
return self._buf.Indirect(0, self._parent_width)
@property
def IsNull(self):
return self._type is Type.NULL
@property
def IsBool(self):
return self._type is Type.BOOL
@property
def AsBool(self):
if self._type is Type.BOOL:
return bool(_Unpack(U, self._Bytes))
else:
return self.AsInt != 0
def MutateBool(self, value):
"""Mutates underlying boolean value bytes in place.
Args:
value: New boolean value.
Returns:
Whether the value was mutated or not.
"""
return self.IsBool and _Mutate(
U, self._buf, value, self._parent_width, BitWidth.W8
)
@property
def IsNumeric(self):
return self.IsInt or self.IsFloat
@property
def IsInt(self):
return self._type in (
Type.INT,
Type.INDIRECT_INT,
Type.UINT,
Type.INDIRECT_UINT,
)
@property
def AsInt(self):
"""Returns current reference as integer value."""
if self.IsNull:
return 0
elif self.IsBool:
return int(self.AsBool)
elif self._type is Type.INT:
return _Unpack(I, self._Bytes)
elif self._type is Type.INDIRECT_INT:
return _Unpack(I, self._Indirect()[: self._byte_width])
if self._type is Type.UINT:
return _Unpack(U, self._Bytes)
elif self._type is Type.INDIRECT_UINT:
return _Unpack(U, self._Indirect()[: self._byte_width])
elif self.IsString:
return len(self.AsString)
elif self.IsKey:
return len(self.AsKey)
elif self.IsBlob:
return len(self.AsBlob)
elif self.IsVector:
return len(self.AsVector)
elif self.IsTypedVector:
return len(self.AsTypedVector)
elif self.IsFixedTypedVector:
return len(self.AsFixedTypedVector)
else:
raise self._ConvertError(Type.INT)
def MutateInt(self, value):
"""Mutates underlying integer value bytes in place.
Args:
value: New integer value. It must fit to the byte size of the existing
encoded value.
Returns:
Whether the value was mutated or not.
"""
if self._type is Type.INT:
return _Mutate(I, self._buf, value, self._parent_width, BitWidth.I(value))
elif self._type is Type.INDIRECT_INT:
return _Mutate(
I, self._Indirect(), value, self._byte_width, BitWidth.I(value)
)
elif self._type is Type.UINT:
return _Mutate(U, self._buf, value, self._parent_width, BitWidth.U(value))
elif self._type is Type.INDIRECT_UINT:
return _Mutate(
U, self._Indirect(), value, self._byte_width, BitWidth.U(value)
)
else:
return False
@property
def IsFloat(self):
return self._type in (Type.FLOAT, Type.INDIRECT_FLOAT)
@property
def AsFloat(self):
"""Returns current reference as floating point value."""
if self.IsNull:
return 0.0
elif self.IsBool:
return float(self.AsBool)
elif self.IsInt:
return float(self.AsInt)
elif self._type is Type.FLOAT:
return _Unpack(F, self._Bytes)
elif self._type is Type.INDIRECT_FLOAT:
return _Unpack(F, self._Indirect()[: self._byte_width])
elif self.IsString:
return float(self.AsString)
elif self.IsVector:
return float(len(self.AsVector))
elif self.IsTypedVector():
return float(len(self.AsTypedVector))
elif self.IsFixedTypedVector():
return float(len(self.FixedTypedVector))
else:
raise self._ConvertError(Type.FLOAT)
def MutateFloat(self, value):
"""Mutates underlying floating point value bytes in place.
Args:
value: New float value. It must fit to the byte size of the existing
encoded value.
Returns:
Whether the value was mutated or not.
"""
if self._type is Type.FLOAT:
return _Mutate(
F,
self._buf,
value,
self._parent_width,
BitWidth.B(self._parent_width),
)
elif self._type is Type.INDIRECT_FLOAT:
return _Mutate(
F,
self._Indirect(),
value,
self._byte_width,
BitWidth.B(self._byte_width),
)
else:
return False
@property
def IsKey(self):
return self._type is Type.KEY
@property
def AsKeyBytes(self):
if self.IsKey:
return Key(self._Indirect(), self._byte_width).Bytes
else:
raise self._ConvertError(Type.KEY)
@property
def AsKey(self):
if self.IsKey:
return str(Key(self._Indirect(), self._byte_width))
else:
raise self._ConvertError(Type.KEY)
@property
def IsString(self):
return self._type is Type.STRING
@property
def AsStringBytes(self):
if self.IsString:
return String(self._Indirect(), self._byte_width).Bytes
elif self.IsKey:
return self.AsKeyBytes
else:
raise self._ConvertError(Type.STRING)
@property
def AsString(self):
if self.IsString:
return str(String(self._Indirect(), self._byte_width))
elif self.IsKey:
return self.AsKey
else:
raise self._ConvertError(Type.STRING)
def MutateString(self, value):
return String(self._Indirect(), self._byte_width).Mutate(value)
@property
def IsBlob(self):
return self._type is Type.BLOB
@property
def AsBlob(self):
if self.IsBlob:
return Blob(self._Indirect(), self._byte_width).Bytes
else:
raise self._ConvertError(Type.BLOB)
@property
def IsAnyVector(self):
return self.IsVector or self.IsTypedVector or self.IsFixedTypedVector()
@property
def IsVector(self):
return self._type in (Type.VECTOR, Type.MAP)
@property
def AsVector(self):
if self.IsVector:
return Vector(self._Indirect(), self._byte_width)
else:
raise self._ConvertError(Type.VECTOR)
@property
def IsTypedVector(self):
return Type.IsTypedVector(self._type)
@property
def AsTypedVector(self):
if self.IsTypedVector:
return TypedVector(
self._Indirect(),
self._byte_width,
Type.ToTypedVectorElementType(self._type),
)
else:
raise self._ConvertError('TYPED_VECTOR')
@property
def IsFixedTypedVector(self):
return Type.IsFixedTypedVector(self._type)
@property
def AsFixedTypedVector(self):
if self.IsFixedTypedVector:
element_type, size = Type.ToFixedTypedVectorElementType(self._type)
return TypedVector(self._Indirect(), self._byte_width, element_type, size)
else:
raise self._ConvertError('FIXED_TYPED_VECTOR')
@property
def IsMap(self):
return self._type is Type.MAP
@property
def AsMap(self):
if self.IsMap:
return Map(self._Indirect(), self._byte_width)
else:
raise self._ConvertError(Type.MAP)
@property
def Value(self):
"""Converts current reference to value of corresponding type.
This is equivalent to calling `AsInt` for integer values, `AsFloat` for
floating point values, etc.
Returns:
Value of corresponding type.
"""
if self.IsNull:
return None
elif self.IsBool:
return self.AsBool
elif self.IsInt:
return self.AsInt
elif self.IsFloat:
return self.AsFloat
elif self.IsString:
return self.AsString
elif self.IsKey:
return self.AsKey
elif self.IsBlob:
return self.AsBlob
elif self.IsMap:
return self.AsMap.Value
elif self.IsVector:
return self.AsVector.Value
elif self.IsTypedVector:
return self.AsTypedVector.Value
elif self.IsFixedTypedVector:
return self.AsFixedTypedVector.Value
else:
raise TypeError('cannot convert %r to value' % self)
def _IsIterable(obj):
try:
iter(obj)
return True
except TypeError:
return False
class Value:
"""Class to represent given value during the encoding process."""
@staticmethod
def Null():
return Value(0, Type.NULL, BitWidth.W8)
@staticmethod
def Bool(value):
return Value(value, Type.BOOL, BitWidth.W8)
@staticmethod
def Int(value, bit_width):
return Value(value, Type.INT, bit_width)
@staticmethod
def UInt(value, bit_width):
return Value(value, Type.UINT, bit_width)
@staticmethod
def Float(value, bit_width):
return Value(value, Type.FLOAT, bit_width)
@staticmethod
def Key(offset):
return Value(offset, Type.KEY, BitWidth.W8)
def __init__(self, value, type_, min_bit_width):
self._value = value
self._type = type_
# For scalars: of itself, for vector: of its elements, for string: length.
self._min_bit_width = min_bit_width
@property
def Value(self):
return self._value
@property
def Type(self):
return self._type
@property
def MinBitWidth(self):
return self._min_bit_width
def StoredPackedType(self, parent_bit_width=BitWidth.W8):
return Type.Pack(self._type, self.StoredWidth(parent_bit_width))
# We have an absolute offset, but want to store a relative offset
# elem_index elements beyond the current buffer end. Since whether
# the relative offset fits in a certain byte_width depends on
# the size of the elements before it (and their alignment), we have
# to test for each size in turn.
def ElemWidth(self, buf_size, elem_index=0):
if Type.IsInline(self._type):
return self._min_bit_width
for byte_width in 1, 2, 4, 8:
offset_loc = (
buf_size
+ _PaddingBytes(buf_size, byte_width)
+ elem_index * byte_width
)
bit_width = BitWidth.U(offset_loc - self._value)
if byte_width == (1 << bit_width):
return bit_width
raise ValueError('relative offset is too big')
def StoredWidth(self, parent_bit_width=BitWidth.W8):
if Type.IsInline(self._type):
return max(self._min_bit_width, parent_bit_width)
return self._min_bit_width
def __repr__(self):
return 'Value(%s, %s, %s)' % (self._value, self._type, self._min_bit_width)
def __str__(self):
return str(self._value)
def InMap(func):
def wrapper(self, *args, **kwargs):
if isinstance(args[0], str):
self.Key(args[0])
func(self, *args[1:], **kwargs)
else:
func(self, *args, **kwargs)
return wrapper
def InMapForString(func):
def wrapper(self, *args):
if len(args) == 1:
func(self, args[0])
elif len(args) == 2:
self.Key(args[0])
func(self, args[1])
else:
raise ValueError('invalid number of arguments')