1
1
from aws_database_encryption_sdk .transform import dict_to_ddb
2
2
from .boto3_conversions import BotoInterfaceShapeConverter
3
3
from aws_database_encryption_sdk .internal .condition_expression_builder import InternalDBESDKDynamoDBConditionExpressionBuilder
4
- from boto3 .dynamodb .conditions import ConditionBase
4
+ from boto3 .dynamodb .conditions import ConditionBase , BuiltConditionExpression
5
5
6
6
class ResourceShapeToClientShapeConverter (BotoInterfaceShapeConverter ):
7
7
8
8
def __init__ (self , table_name = None ):
9
9
self .table_name = table_name
10
10
self .expression_builder = InternalDBESDKDynamoDBConditionExpressionBuilder ()
11
11
12
+ def _unpack_built_condition_expression (self , request_to_update , expression_key ):
13
+ built_condition_expression = request_to_update [expression_key ]
14
+ request_to_update [expression_key ] = built_condition_expression .condition_expression
15
+ attribute_names_from_built_expression = built_condition_expression .attribute_name_placeholders
16
+ # Join any placeholder ExpressionAttributeNames with any other ExpressionAttributeNames
17
+ try :
18
+ request_to_update ["ExpressionAttributeNames" ] = request_to_update ["ExpressionAttributeNames" ] | attribute_names_from_built_expression
19
+ except KeyError :
20
+ request_to_update ["ExpressionAttributeNames" ] = attribute_names_from_built_expression
21
+ # BuiltConditionExpression stores values in resource format; convert to client format before joining
22
+ attribute_values_from_built_expression = super ().expression_attribute_values (
23
+ built_condition_expression .attribute_value_placeholders
24
+ )
25
+ try :
26
+ request_to_update ["ExpressionAttributeValues" ] = request_to_update ["ExpressionAttributeValues" ] | attribute_values_from_built_expression
27
+ except KeyError :
28
+ request_to_update ["ExpressionAttributeValues" ] = attribute_values_from_built_expression
29
+
12
30
def item (self , item ):
13
31
return dict_to_ddb (item )
14
32
@@ -23,7 +41,10 @@ def put_item_request(self, put_item_request):
23
41
if not self .table_name :
24
42
raise ValueError ("Table name must be provided to ResourceShapeToClientShapeConverter to use put_item" )
25
43
put_item_request ["TableName" ] = self .table_name
26
- return super ().put_item_request (put_item_request )
44
+ super_conversion = super ().put_item_request (put_item_request )
45
+ if "ConditionExpression" in super_conversion and isinstance (super_conversion ["ConditionExpression" ], BuiltConditionExpression ):
46
+ self ._unpack_built_condition_expression (super_conversion , "ConditionExpression" )
47
+ return super_conversion
27
48
28
49
def get_item_request (self , get_item_request ):
29
50
if not self .table_name :
@@ -35,18 +56,30 @@ def query_request(self, query_request):
35
56
if not self .table_name :
36
57
raise ValueError ("Table name must be provided to ResourceShapeToClientShapeConverter to use query" )
37
58
query_request ["TableName" ] = self .table_name
38
- return super ().query_request (query_request )
59
+ super_conversion = super ().query_request (query_request )
60
+ if "KeyConditionExpression" in super_conversion and isinstance (super_conversion ["KeyConditionExpression" ], BuiltConditionExpression ):
61
+ self ._unpack_built_condition_expression (super_conversion , "KeyConditionExpression" )
62
+ if "FilterExpression" in super_conversion and isinstance (super_conversion ["FilterExpression" ], BuiltConditionExpression ):
63
+ self ._unpack_built_condition_expression (super_conversion , "FilterExpression" )
64
+ return super_conversion
39
65
40
66
def scan_request (self , scan_request ):
41
67
if not self .table_name :
42
68
raise ValueError ("Table name must be provided to ResourceShapeToClientShapeConverter to use scan" )
43
69
scan_request ["TableName" ] = self .table_name
44
- return super ().scan_request (scan_request )
70
+ super_conversion = super ().scan_request (scan_request )
71
+ if "FilterExpression" in super_conversion and isinstance (super_conversion ["FilterExpression" ], BuiltConditionExpression ):
72
+ self ._unpack_built_condition_expression (super_conversion , "FilterExpression" )
73
+ return super_conversion
45
74
46
75
def expression (self , condition_expression ):
47
76
# Expressions provided to tables can be Condition objects, which need to be converted to strings.
48
- if isinstance (condition_expression , ConditionBase ):
49
- return self .expression_builder .build_expression (condition_expression , "placeholder" , "placeholder" )
77
+ if condition_expression .__module__ == "boto3.dynamodb.conditions" :
78
+ out = self .expression_builder .build_expression (condition_expression , {}, {})
79
+ print (f"{ condition_expression = } " )
80
+ print (f"{ out = } " )
81
+ return out
50
82
# Expressions provided to tables can also already be string-like.
51
83
# Assume the user has provided something string-like, and let Smithy-Python/DBESDK internals raise exceptions if not.
84
+ print (f"probably a string: { condition_expression = } " )
52
85
return condition_expression
0 commit comments