1- from construct import Construct , Struct , Enum , Byte , Switch , BFloat64 , Flag , \
2- Array , LazyBound , Field , String , Container
1+ from construct import Construct , Struct , Byte , BFloat64 , Flag , \
2+ Array , LazyBound , String , Container
33from construct .core import _read_stream , _write_stream , Adapter
44
55
@@ -74,33 +74,32 @@ def _decode(self, obj, context):
7474 VLQ ("length" ),
7575 Array (lambda ctx : ctx .length ,
7676 LazyBound ("data" ,
77- lambda : variant ())))
78-
79- dict_variant = Struct ("data" ,
80- VLQ ("length" ),
81- Array (lambda ctx : ctx .length ,
82- Struct ("dict" ,
83- star_string ("key" ),
84- LazyBound ("value" , lambda : variant ()))))
85-
86- variant = lambda name = "variant" : Struct (name ,
87- Enum (Byte ("type" ),
88- NULL = 1 ,
89- DOUBLE = 2 ,
90- BOOL = 3 ,
91- SVLQ = 4 ,
92- STRING = 5 ,
93- VARIANT = 6 ,
94- DICT = 7
95- ),
96- Switch ("data" , lambda ctx : ctx .type ,
97- {
98- "DOUBLE" : BFloat64 ("data" ),
99- "BOOL" : Flag ("data" ),
100- "SVLQ" : SignedVLQ ("data" ),
101- "STRING" : star_string (
102- "data" ),
103- "VARIANT" : variant_variant ,
104- "DICT" : dict_variant
105- },
106- default = Field ("null" , 0 )))
77+ lambda : Variant ("" ))))
78+
79+ class dict_variant (Construct ):
80+ def _parse (self , stream , context ):
81+ l = VLQ ("" ).parse_stream (stream )
82+ c = {}
83+ for x in range (l ):
84+ key = star_string ("" ).parse_stream (stream )
85+ value = Variant ("" ).parse_stream (stream )
86+ c [key ] = value
87+ return c
88+
89+ class Variant (Construct ):
90+ def _parse (self , stream , context ):
91+ x = Byte ("" ).parse_stream (stream )
92+ if x == 1 :
93+ return None
94+ elif x == 2 :
95+ return BFloat64 ("" ).parse_stream (stream )
96+ elif x == 3 :
97+ return Flag ("" ).parse_stream (stream )
98+ elif x == 4 :
99+ return SignedVLQ ("" ).parse_stream (stream )
100+ elif x == 5 :
101+ return star_string ().parse_stream (stream )
102+ elif x == 6 :
103+ return variant_variant .parse_stream (stream )
104+ elif x == 7 :
105+ return dict_variant ("" ).parse_stream (stream )
0 commit comments