Debian Codegraph @

boto3/dynamodb/transform.py

raw
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 import copy
14
15 from boto3.compat import collections_abc
16 from boto3.docs.utils import DocumentModifiedShape
17 from boto3.dynamodb.conditions import ConditionBase, ConditionExpressionBuilder
18 from boto3.dynamodb.types import TypeDeserializer, TypeSerializer
19
20
21 def register_high_level_interface(base_classes, **kwargs):
22 base_classes.insert(0, DynamoDBHighLevelResource)
23
24
25 class _ForgetfulDict(dict):
26 """A dictionary that discards any items set on it. For use as `memo` in
27 `copy.deepcopy()` when every instance of a repeated object in the deepcopied
28 data structure should result in a separate copy.
29 """
30
31 def __setitem__(self, key, value):
32 pass
33
34
35 def copy_dynamodb_params(params, **kwargs):
36 return copy.deepcopy(params, memo=_ForgetfulDict())
37
38
39 class DynamoDBHighLevelResource:
40 def __init__(self, *args, **kwargs):
41 super().__init__(*args, **kwargs)
42
43 # Apply handler that creates a copy of the user provided dynamodb
44 # item such that it can be modified.
45 self.meta.client.meta.events.register(
46 'provide-client-params.dynamodb',
47 copy_dynamodb_params,
48 unique_id='dynamodb-create-params-copy',
49 )
50
51 self._injector = TransformationInjector()
52 # Apply the handler that generates condition expressions including
53 # placeholders.
54 self.meta.client.meta.events.register(
55 'before-parameter-build.dynamodb',
56 self._injector.inject_condition_expressions,
57 unique_id='dynamodb-condition-expression',
58 )
59
60 # Apply the handler that serializes the request from python
61 # types to dynamodb types.
62 self.meta.client.meta.events.register(
63 'before-parameter-build.dynamodb',
64 self._injector.inject_attribute_value_input,
65 unique_id='dynamodb-attr-value-input',
66 )
67
68 # Apply the handler that deserializes the response from dynamodb
69 # types to python types.
70 self.meta.client.meta.events.register(
71 'after-call.dynamodb',
72 self._injector.inject_attribute_value_output,
73 unique_id='dynamodb-attr-value-output',
74 )
75
76 # Apply the documentation customizations to account for
77 # the transformations.
78 attr_value_shape_docs = DocumentModifiedShape(
79 'AttributeValue',
80 new_type='valid DynamoDB type',
81 new_description=(
82 '- The value of the attribute. The valid value types are '
83 'listed in the '
84 ':ref:`DynamoDB Reference Guide<ref_valid_dynamodb_types>`.'
85 ),
86 new_example_value=(
87 '\'string\'|123|Binary(b\'bytes\')|True|None|set([\'string\'])'
88 '|set([123])|set([Binary(b\'bytes\')])|[]|{}'
89 ),
90 )
91
92 key_expression_shape_docs = DocumentModifiedShape(
93 'KeyExpression',
94 new_type=(
95 'condition from :py:class:`boto3.dynamodb.conditions.Key` '
96 'method'
97 ),
98 new_description=(
99 'The condition(s) a key(s) must meet. Valid conditions are '
100 'listed in the '
101 ':ref:`DynamoDB Reference Guide<ref_dynamodb_conditions>`.'
102 ),
103 new_example_value='Key(\'mykey\').eq(\'myvalue\')',
104 )
105
106 con_expression_shape_docs = DocumentModifiedShape(
107 'ConditionExpression',
108 new_type=(
109 'condition from :py:class:`boto3.dynamodb.conditions.Attr` '
110 'method'
111 ),
112 new_description=(
113 'The condition(s) an attribute(s) must meet. Valid conditions '
114 'are listed in the '
115 ':ref:`DynamoDB Reference Guide<ref_dynamodb_conditions>`.'
116 ),
117 new_example_value='Attr(\'myattribute\').eq(\'myvalue\')',
118 )
119
120 self.meta.client.meta.events.register(
121 'docs.*.dynamodb.*.complete-section',
122 attr_value_shape_docs.replace_documentation_for_matching_shape,
123 unique_id='dynamodb-attr-value-docs',
124 )
125
126 self.meta.client.meta.events.register(
127 'docs.*.dynamodb.*.complete-section',
128 key_expression_shape_docs.replace_documentation_for_matching_shape,
129 unique_id='dynamodb-key-expression-docs',
130 )
131
132 self.meta.client.meta.events.register(
133 'docs.*.dynamodb.*.complete-section',
134 con_expression_shape_docs.replace_documentation_for_matching_shape,
135 unique_id='dynamodb-cond-expression-docs',
136 )
137
138
139 class TransformationInjector:
140 """Injects the transformations into the user provided parameters."""
141
142 def __init__(
143 self,
144 transformer=None,
145 condition_builder=None,
146 serializer=None,
147 deserializer=None,
148 ):
149 self._transformer = transformer
150 if transformer is None:
151 self._transformer = ParameterTransformer()
152
153 self._condition_builder = condition_builder
154 if condition_builder is None:
155 self._condition_builder = ConditionExpressionBuilder()
156
157 self._serializer = serializer
158 if serializer is None:
159 self._serializer = TypeSerializer()
160
161 self._deserializer = deserializer
162 if deserializer is None:
163 self._deserializer = TypeDeserializer()
164
165 def inject_condition_expressions(self, params, model, **kwargs):
166 """Injects the condition expression transformation into the parameters
167
168 This injection includes transformations for ConditionExpression shapes
169 and KeyExpression shapes. It also handles any placeholder names and
170 values that are generated when transforming the condition expressions.
171 """
172 self._condition_builder.reset()
173 generated_names = {}
174 generated_values = {}
175
176 # Create and apply the Condition Expression transformation.
177 transformation = ConditionExpressionTransformation(
178 self._condition_builder,
179 placeholder_names=generated_names,
180 placeholder_values=generated_values,
181 is_key_condition=False,
182 )
183 self._transformer.transform(
184 params, model.input_shape, transformation, 'ConditionExpression'
185 )
186
187 # Create and apply the Key Condition Expression transformation.
188 transformation = ConditionExpressionTransformation(
189 self._condition_builder,
190 placeholder_names=generated_names,
191 placeholder_values=generated_values,
192 is_key_condition=True,
193 )
194 self._transformer.transform(
195 params, model.input_shape, transformation, 'KeyExpression'
196 )
197
198 expr_attr_names_input = 'ExpressionAttributeNames'
199 expr_attr_values_input = 'ExpressionAttributeValues'
200
201 # Now that all of the condition expression transformation are done,
202 # update the placeholder dictionaries in the request.
203 if expr_attr_names_input in params:
204 params[expr_attr_names_input].update(generated_names)
205 else:
206 if generated_names:
207 params[expr_attr_names_input] = generated_names
208
209 if expr_attr_values_input in params:
210 params[expr_attr_values_input].update(generated_values)
211 else:
212 if generated_values:
213 params[expr_attr_values_input] = generated_values
214
215 def inject_attribute_value_input(self, params, model, **kwargs):
216 """Injects DynamoDB serialization into parameter input"""
217 self._transformer.transform(
218 params,
219 model.input_shape,
220 self._serializer.serialize,
221 'AttributeValue',
222 )
223
224 def inject_attribute_value_output(self, parsed, model, **kwargs):
225 """Injects DynamoDB deserialization into responses"""
226 if model.output_shape is not None:
227 self._transformer.transform(
228 parsed,
229 model.output_shape,
230 self._deserializer.deserialize,
231 'AttributeValue',
232 )
233
234
235 class ConditionExpressionTransformation:
236 """Provides a transformation for condition expressions
237
238 The ``ParameterTransformer`` class can call this class directly
239 to transform the condition expressions in the parameters provided.
240 """
241
242 def __init__(
243 self,
244 condition_builder,
245 placeholder_names,
246 placeholder_values,
247 is_key_condition=False,
248 ):
249 self._condition_builder = condition_builder
250 self._placeholder_names = placeholder_names
251 self._placeholder_values = placeholder_values
252 self._is_key_condition = is_key_condition
253
254 def __call__(self, value):
255 if isinstance(value, ConditionBase):
256 # Create a conditional expression string with placeholders
257 # for the provided condition.
258 built_expression = self._condition_builder.build_expression(
259 value, is_key_condition=self._is_key_condition
260 )
261
262 self._placeholder_names.update(
263 built_expression.attribute_name_placeholders
264 )
265 self._placeholder_values.update(
266 built_expression.attribute_value_placeholders
267 )
268
269 return built_expression.condition_expression
270 # Use the user provided value if it is not a ConditonBase object.
271 return value
272
273
274 class ParameterTransformer:
275 """Transforms the input to and output from botocore based on shape"""
276
277 def transform(self, params, model, transformation, target_shape):
278 """Transforms the dynamodb input to or output from botocore
279
280 It applies a specified transformation whenever a specific shape name
281 is encountered while traversing the parameters in the dictionary.
282
283 :param params: The parameters structure to transform.
284 :param model: The operation model.
285 :param transformation: The function to apply the parameter
286 :param target_shape: The name of the shape to apply the
287 transformation to
288 """
289 self._transform_parameters(model, params, transformation, target_shape)
290
291 def _transform_parameters(
292 self, model, params, transformation, target_shape
293 ):
294 type_name = model.type_name
295 if type_name in ('structure', 'map', 'list'):
296 getattr(self, f'_transform_{type_name}')(
297 model, params, transformation, target_shape
298 )
299
300 def _transform_structure(
301 self, model, params, transformation, target_shape
302 ):
303 if not isinstance(params, collections_abc.Mapping):
304 return
305 for param in params:
306 if param in model.members:
307 member_model = model.members[param]
308 member_shape = member_model.name
309 if member_shape == target_shape:
310 params[param] = transformation(params[param])
311 else:
312 self._transform_parameters(
313 member_model,
314 params[param],
315 transformation,
316 target_shape,
317 )
318
319 def _transform_map(self, model, params, transformation, target_shape):
320 if not isinstance(params, collections_abc.Mapping):
321 return
322 value_model = model.value
323 value_shape = value_model.name
324 for key, value in params.items():
325 if value_shape == target_shape:
326 params[key] = transformation(value)
327 else:
328 self._transform_parameters(
329 value_model, params[key], transformation, target_shape
330 )
331
332 def _transform_list(self, model, params, transformation, target_shape):
333 if not isinstance(params, collections_abc.MutableSequence):
334 return
335 member_model = model.member
336 member_shape = member_model.name
337 for i, item in enumerate(params):
338 if member_shape == target_shape:
339 params[i] = transformation(item)
340 else:
341 self._transform_parameters(
342 member_model, params[i], transformation, target_shape
343 )