| 1 |
# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 |
# may not use this file except in compliance with the License. A copy of |
| 5 |
# the License is located at |
| 6 |
# |
| 7 |
# https://aws.amazon.com/apache2.0/ |
| 8 |
# |
| 9 |
# or in the "license" file accompanying this file. This file is |
| 10 |
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 |
# ANY KIND, either express or implied. See the License for the specific |
| 12 |
# language governing permissions and limitations under the License. |
| 13 |
from decimal import ( |
| 14 |
Clamped, |
| 15 |
Context, |
| 16 |
Decimal, |
| 17 |
Inexact, |
| 18 |
Overflow, |
| 19 |
Rounded, |
| 20 |
Underflow, |
| 21 |
) |
| 22 |
|
| 23 |
from boto3.compat import collections_abc |
| 24 |
|
| 25 |
STRING = 'S' |
| 26 |
NUMBER = 'N' |
| 27 |
BINARY = 'B' |
| 28 |
STRING_SET = 'SS' |
| 29 |
NUMBER_SET = 'NS' |
| 30 |
BINARY_SET = 'BS' |
| 31 |
NULL = 'NULL' |
| 32 |
BOOLEAN = 'BOOL' |
| 33 |
MAP = 'M' |
| 34 |
LIST = 'L' |
| 35 |
|
| 36 |
|
| 37 |
DYNAMODB_CONTEXT = Context( |
| 38 |
Emin=-128, |
| 39 |
Emax=126, |
| 40 |
prec=38, |
| 41 |
traps=[Clamped, Overflow, Inexact, Rounded, Underflow], |
| 42 |
) |
| 43 |
|
| 44 |
|
| 45 |
BINARY_TYPES = (bytearray, bytes) |
| 46 |
|
| 47 |
|
| 48 |
class Binary: |
| 49 |
"""A class for representing Binary in dynamodb |
| 50 |
|
| 51 |
Especially for Python 2, use this class to explicitly specify |
| 52 |
binary data for item in DynamoDB. It is essentially a wrapper around |
| 53 |
binary. Unicode and Python 3 string types are not allowed. |
| 54 |
""" |
| 55 |
|
| 56 |
def __init__(self, value): |
| 57 |
if not isinstance(value, BINARY_TYPES): |
| 58 |
types = ', '.join([str(t) for t in BINARY_TYPES]) |
| 59 |
raise TypeError(f'Value must be of the following types: {types}') |
| 60 |
self.value = value |
| 61 |
|
| 62 |
def __eq__(self, other): |
| 63 |
if isinstance(other, Binary): |
| 64 |
return self.value == other.value |
| 65 |
return self.value == other |
| 66 |
|
| 67 |
def __ne__(self, other): |
| 68 |
return not self.__eq__(other) |
| 69 |
|
| 70 |
def __repr__(self): |
| 71 |
return f'Binary({self.value!r})' |
| 72 |
|
| 73 |
def __str__(self): |
| 74 |
return self.value |
| 75 |
|
| 76 |
def __bytes__(self): |
| 77 |
return self.value |
| 78 |
|
| 79 |
def __hash__(self): |
| 80 |
return hash(self.value) |
| 81 |
|
| 82 |
|
| 83 |
class TypeSerializer: |
| 84 |
"""This class serializes Python data types to DynamoDB types.""" |
| 85 |
|
| 86 |
def serialize(self, value): |
| 87 |
"""The method to serialize the Python data types. |
| 88 |
|
| 89 |
:param value: A python value to be serialized to DynamoDB. Here are |
| 90 |
the various conversions: |
| 91 |
|
| 92 |
Python DynamoDB |
| 93 |
------ -------- |
| 94 |
None {'NULL': True} |
| 95 |
True/False {'BOOL': True/False} |
| 96 |
int/Decimal {'N': str(value)} |
| 97 |
string {'S': string} |
| 98 |
Binary/bytearray/bytes (py3 only) {'B': bytes} |
| 99 |
set([int/Decimal]) {'NS': [str(value)]} |
| 100 |
set([string]) {'SS': [string]) |
| 101 |
set([Binary/bytearray/bytes]) {'BS': [bytes]} |
| 102 |
list {'L': list} |
| 103 |
dict {'M': dict} |
| 104 |
|
| 105 |
For types that involve numbers, it is recommended that ``Decimal`` |
| 106 |
objects are used to be able to round-trip the Python type. |
| 107 |
For types that involve binary, it is recommended that ``Binary`` |
| 108 |
objects are used to be able to round-trip the Python type. |
| 109 |
|
| 110 |
:rtype: dict |
| 111 |
:returns: A dictionary that represents a dynamoDB data type. These |
| 112 |
dictionaries can be directly passed to botocore methods. |
| 113 |
""" |
| 114 |
dynamodb_type = self._get_dynamodb_type(value) |
| 115 |
serializer = getattr(self, f'_serialize_{dynamodb_type}'.lower()) |
| 116 |
return {dynamodb_type: serializer(value)} |
| 117 |
|
| 118 |
def _get_dynamodb_type(self, value): |
| 119 |
dynamodb_type = None |
| 120 |
|
| 121 |
if self._is_null(value): |
| 122 |
dynamodb_type = NULL |
| 123 |
|
| 124 |
elif self._is_boolean(value): |
| 125 |
dynamodb_type = BOOLEAN |
| 126 |
|
| 127 |
elif self._is_number(value): |
| 128 |
dynamodb_type = NUMBER |
| 129 |
|
| 130 |
elif self._is_string(value): |
| 131 |
dynamodb_type = STRING |
| 132 |
|
| 133 |
elif self._is_binary(value): |
| 134 |
dynamodb_type = BINARY |
| 135 |
|
| 136 |
elif self._is_type_set(value, self._is_number): |
| 137 |
dynamodb_type = NUMBER_SET |
| 138 |
|
| 139 |
elif self._is_type_set(value, self._is_string): |
| 140 |
dynamodb_type = STRING_SET |
| 141 |
|
| 142 |
elif self._is_type_set(value, self._is_binary): |
| 143 |
dynamodb_type = BINARY_SET |
| 144 |
|
| 145 |
elif self._is_map(value): |
| 146 |
dynamodb_type = MAP |
| 147 |
|
| 148 |
elif self._is_listlike(value): |
| 149 |
dynamodb_type = LIST |
| 150 |
|
| 151 |
else: |
| 152 |
msg = f'Unsupported type "{type(value)}" for value "{value}"' |
| 153 |
raise TypeError(msg) |
| 154 |
|
| 155 |
return dynamodb_type |
| 156 |
|
| 157 |
def _is_null(self, value): |
| 158 |
if value is None: |
| 159 |
return True |
| 160 |
return False |
| 161 |
|
| 162 |
def _is_boolean(self, value): |
| 163 |
if isinstance(value, bool): |
| 164 |
return True |
| 165 |
return False |
| 166 |
|
| 167 |
def _is_number(self, value): |
| 168 |
if isinstance(value, (int, Decimal)): |
| 169 |
return True |
| 170 |
elif isinstance(value, float): |
| 171 |
raise TypeError( |
| 172 |
'Float types are not supported. Use Decimal types instead.' |
| 173 |
) |
| 174 |
return False |
| 175 |
|
| 176 |
def _is_string(self, value): |
| 177 |
if isinstance(value, str): |
| 178 |
return True |
| 179 |
return False |
| 180 |
|
| 181 |
def _is_binary(self, value): |
| 182 |
if isinstance(value, (Binary, bytearray, bytes)): |
| 183 |
return True |
| 184 |
return False |
| 185 |
|
| 186 |
def _is_set(self, value): |
| 187 |
if isinstance(value, collections_abc.Set): |
| 188 |
return True |
| 189 |
return False |
| 190 |
|
| 191 |
def _is_type_set(self, value, type_validator): |
| 192 |
if self._is_set(value): |
| 193 |
if False not in map(type_validator, value): |
| 194 |
return True |
| 195 |
return False |
| 196 |
|
| 197 |
def _is_map(self, value): |
| 198 |
if isinstance(value, collections_abc.Mapping): |
| 199 |
return True |
| 200 |
return False |
| 201 |
|
| 202 |
def _is_listlike(self, value): |
| 203 |
if isinstance(value, (list, tuple)): |
| 204 |
return True |
| 205 |
return False |
| 206 |
|
| 207 |
def _serialize_null(self, value): |
| 208 |
return True |
| 209 |
|
| 210 |
def _serialize_bool(self, value): |
| 211 |
return value |
| 212 |
|
| 213 |
def _serialize_n(self, value): |
| 214 |
number = str(DYNAMODB_CONTEXT.create_decimal(value)) |
| 215 |
if number in ['Infinity', 'NaN']: |
| 216 |
raise TypeError('Infinity and NaN not supported') |
| 217 |
return number |
| 218 |
|
| 219 |
def _serialize_s(self, value): |
| 220 |
return value |
| 221 |
|
| 222 |
def _serialize_b(self, value): |
| 223 |
if isinstance(value, Binary): |
| 224 |
value = value.value |
| 225 |
return value |
| 226 |
|
| 227 |
def _serialize_ss(self, value): |
| 228 |
return [self._serialize_s(s) for s in value] |
| 229 |
|
| 230 |
def _serialize_ns(self, value): |
| 231 |
return [self._serialize_n(n) for n in value] |
| 232 |
|
| 233 |
def _serialize_bs(self, value): |
| 234 |
return [self._serialize_b(b) for b in value] |
| 235 |
|
| 236 |
def _serialize_l(self, value): |
| 237 |
return [self.serialize(v) for v in value] |
| 238 |
|
| 239 |
def _serialize_m(self, value): |
| 240 |
return {k: self.serialize(v) for k, v in value.items()} |
| 241 |
|
| 242 |
|
| 243 |
class TypeDeserializer: |
| 244 |
"""This class deserializes DynamoDB types to Python types.""" |
| 245 |
|
| 246 |
def deserialize(self, value): |
| 247 |
"""The method to deserialize the DynamoDB data types. |
| 248 |
|
| 249 |
:param value: A DynamoDB value to be deserialized to a pythonic value. |
| 250 |
Here are the various conversions: |
| 251 |
|
| 252 |
DynamoDB Python |
| 253 |
-------- ------ |
| 254 |
{'NULL': True} None |
| 255 |
{'BOOL': True/False} True/False |
| 256 |
{'N': str(value)} Decimal(str(value)) |
| 257 |
{'S': string} string |
| 258 |
{'B': bytes} Binary(bytes) |
| 259 |
{'NS': [str(value)]} set([Decimal(str(value))]) |
| 260 |
{'SS': [string]} set([string]) |
| 261 |
{'BS': [bytes]} set([bytes]) |
| 262 |
{'L': list} list |
| 263 |
{'M': dict} dict |
| 264 |
|
| 265 |
:returns: The pythonic value of the DynamoDB type. |
| 266 |
""" |
| 267 |
|
| 268 |
if not value: |
| 269 |
raise TypeError( |
| 270 |
'Value must be a nonempty dictionary whose key ' |
| 271 |
'is a valid dynamodb type.' |
| 272 |
) |
| 273 |
dynamodb_type = list(value.keys())[0] |
| 274 |
try: |
| 275 |
deserializer = getattr( |
| 276 |
self, f'_deserialize_{dynamodb_type}'.lower() |
| 277 |
) |
| 278 |
except AttributeError: |
| 279 |
raise TypeError(f'Dynamodb type {dynamodb_type} is not supported') |
| 280 |
return deserializer(value[dynamodb_type]) |
| 281 |
|
| 282 |
def _deserialize_null(self, value): |
| 283 |
return None |
| 284 |
|
| 285 |
def _deserialize_bool(self, value): |
| 286 |
return value |
| 287 |
|
| 288 |
def _deserialize_n(self, value): |
| 289 |
return DYNAMODB_CONTEXT.create_decimal(value) |
| 290 |
|
| 291 |
def _deserialize_s(self, value): |
| 292 |
return value |
| 293 |
|
| 294 |
def _deserialize_b(self, value): |
| 295 |
return Binary(value) |
| 296 |
|
| 297 |
def _deserialize_ns(self, value): |
| 298 |
return set(map(self._deserialize_n, value)) |
| 299 |
|
| 300 |
def _deserialize_ss(self, value): |
| 301 |
return set(map(self._deserialize_s, value)) |
| 302 |
|
| 303 |
def _deserialize_bs(self, value): |
| 304 |
return set(map(self._deserialize_b, value)) |
| 305 |
|
| 306 |
def _deserialize_l(self, value): |
| 307 |
return [self.deserialize(v) for v in value] |
| 308 |
|
| 309 |
def _deserialize_m(self, value): |
| 310 |
return {k: self.deserialize(v) for k, v in value.items()} |