Repository navigation
Expand file tree
/
Copy pathdynamodb_deserializer.py
More file actions
114 lines (85 loc) · 4.13 KB
/
Copy pathdynamodb_deserializer.py
File metadata and controls
114 lines (85 loc) · 4.13 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
from __future__ import annotations
from decimal import Clamped, Context, Decimal, Inexact, Overflow, Rounded, Underflow
from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
from collections.abc import Callable, Sequence
# NOTE: DynamoDB supports up to 38 digits precision
# Therefore, this ensures our Decimal follows what's stored in the table
DYNAMODB_CONTEXT = Context(
Emin=-128,
Emax=126,
prec=38,
traps=[Clamped, Overflow, Inexact, Rounded, Underflow],
)
# Allow removing trailing zeros, but reject any loss of precision.
_NORMALIZE_CONTEXT = Context(prec=38)
_NORMALIZE_CONTEXT.traps[Inexact] = True
class TypeDeserializer:
"""
Deserializes DynamoDB types to Python types.
It's based on boto3's [DynamoDB TypeDeserializer](https://boto3.amazonaws.com/v1/documentation/api/latest/_modules/boto3/dynamodb/types.html).
The only notable difference is that for Binary (`B`, `BS`) values we return Python Bytes directly,
since we don't support Python 2.
"""
def deserialize(self, value: dict) -> Any:
"""Deserialize DynamoDB data types into Python types.
Parameters
----------
value: Any
DynamoDB value to be deserialized to a python type
Here are the various conversions:
DynamoDB Python
-------- ------
{'NULL': True} None
{'BOOL': True/False} True/False
{'N': Decimal(value)} Decimal(value)
{'S': string} string
{'B': bytes} bytes
{'NS': [str(value)]} set([str(value)])
{'SS': [string]} set([string])
{'BS': [bytes]} set([bytes])
{'L': list} list
{'M': dict} dict
Parameters
----------
value: Any
DynamoDB value to be deserialized to a python type
Returns
--------
any
Python native type converted from DynamoDB type
"""
dynamodb_type = list(value.keys())[0]
deserializer: Callable | None = getattr(self, f"_deserialize_{dynamodb_type}".lower(), None)
if deserializer is None:
raise TypeError(f"Dynamodb type {dynamodb_type} is not supported")
return deserializer(value[dynamodb_type])
def _deserialize_null(self, value: bool) -> None:
return None
def _deserialize_bool(self, value: bool) -> bool:
return value
def _deserialize_n(self, value: str) -> Decimal:
# Preserve the existing fallback for empty strings and a lone decimal point.
if value in ("", "."):
return DYNAMODB_CONTEXT.create_decimal(0)
number = Decimal(value)
if len(number.as_tuple().digits) > 38:
# See: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes.Number
# Trailing zeros don't count towards the 38 digits of precision, so move them
# into the exponent. Cutting characters off the string would change the value.
number = number.normalize(_NORMALIZE_CONTEXT)
return DYNAMODB_CONTEXT.create_decimal(number)
def _deserialize_s(self, value: str) -> str:
return value
def _deserialize_b(self, value: bytes) -> bytes:
return value
def _deserialize_ns(self, value: Sequence[str]) -> set[Decimal]:
return set(map(self._deserialize_n, value))
def _deserialize_ss(self, value: Sequence[str]) -> set[str]:
return set(map(self._deserialize_s, value))
def _deserialize_bs(self, value: Sequence[bytes]) -> set[bytes]:
return set(map(self._deserialize_b, value))
def _deserialize_l(self, value: Sequence[dict]) -> Sequence[Any]:
return [self.deserialize(v) for v in value]
def _deserialize_m(self, value: dict) -> dict:
return {k: self.deserialize(v) for k, v in value.items()}