@@ -6,45 +6,47 @@ def parse(dynamodb_object: Union[dict, list]) -> Union[dict, list]:
6
6
if isinstance (dynamodb_object , list ):
7
7
return [parse (obj ) for obj in dynamodb_object ]
8
8
9
- parsed = {}
10
- for key in dynamodb_object .keys ():
11
- parsed [key ] = _parse_pair (dynamodb_object [key ])
12
- return parsed
9
+ return {key : _parse_property (value ) for key , value in dynamodb_object .items ()}
13
10
14
11
15
12
# pylint:disable=too-many-return-statements
16
- def _parse_pair (type_value_pair : dict ) -> Any :
17
- data_type = list (type_value_pair .keys ())[0 ]
13
+ def _parse_property (prop : dict ) -> Any :
14
+ data_type = next (iter (prop ))
15
+ value = prop .get (data_type , [])
18
16
19
17
if data_type in {"S" , "SS" , "BOOL" }:
20
- return type_value_pair [ data_type ]
18
+ return value
21
19
22
20
if data_type == "N" :
23
- return _to_num (type_value_pair [ data_type ] )
21
+ return _to_num (value )
24
22
25
23
if data_type == "NS" :
26
- return [_to_num (data ) for data in type_value_pair [ data_type ] ]
24
+ return [_to_num (item ) for item in value ]
27
25
28
26
if data_type == "B" :
29
- return base64 . decodebytes ( bytes ( type_value_pair [ data_type ], "utf-8" ) )
27
+ return _to_bytes ( value )
30
28
31
29
if data_type == "BS" :
32
- return [base64 . decodebytes ( bytes ( data , "utf-8" )) for data in type_value_pair [ data_type ] ]
30
+ return [_to_bytes ( item ) for item in value ]
33
31
34
32
if data_type == "M" :
35
- return parse (type_value_pair [ data_type ] )
33
+ return parse (value )
36
34
37
35
if data_type == "L" :
38
- return [_parse_pair (item ) for item in type_value_pair [ data_type ] ]
36
+ return [_parse_property (item ) for item in value ]
39
37
40
- if data_type == "NULL" and type_value_pair [ data_type ] :
38
+ if data_type == "NULL" and value :
41
39
return None
42
40
43
- raise TypeError ("Unknown DynamoDB data type '%s' with value '%s'" % (data_type , type_value_pair [ data_type ] ))
41
+ raise TypeError ("Unknown DynamoDB data type '%s' with value '%s'" % (data_type , value ))
44
42
45
43
46
- def _to_num (number : Any ) -> Union [float , int ]:
44
+ def _to_num (value : Any ) -> Union [float , int ]:
47
45
try :
48
- return int (number )
46
+ return int (value )
49
47
except ValueError :
50
- return float (number )
48
+ return float (value )
49
+
50
+
51
+ def _to_bytes (value : Any ) -> bytes :
52
+ return base64 .decodebytes (bytes (value , "utf-8" ))
0 commit comments