|
25 | 25 | Ref: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_AttributeValue.html#DDB-Type-AttributeValue-NS |
26 | 26 | """ |
27 | 27 |
|
28 | | -from json_syntax.helpers import issub_safe, NoneType, has_origin, get_origin |
| 28 | +from json_syntax.helpers import ( |
| 29 | + issub_safe, |
| 30 | + NoneType, |
| 31 | + has_origin, |
| 32 | + get_origin, |
| 33 | + STR2PY, |
| 34 | + PY2STR, |
| 35 | +) |
29 | 36 | from json_syntax.product import build_attribute_map |
| 37 | +from json_syntax.string import stringify_keys |
30 | 38 | from json_syntax.ruleset import SimpleRuleSet |
31 | 39 |
|
32 | 40 | import base64 as b64 |
|
39 | 47 |
|
40 | 48 | DDB2PY = "dynamodb_to_python" |
41 | 49 | PY2DDB = "python_to_dynamodb" |
| 50 | +_STRING_ACTIONS = {DDB2PY: STR2PY, PY2DDB: PY2STR} |
42 | 51 |
|
43 | 52 |
|
44 | 53 | def booleans(verb, typ, ctx): |
@@ -122,17 +131,17 @@ def dicts(verb, typ, ctx): |
122 | 131 | """ |
123 | 132 | A rule to represent lists as Dynamo list values. |
124 | 133 | """ |
125 | | - if not has_origin(typ, dict, num_args=2): |
| 134 | + if verb not in _STRING_ACTIONS or not has_origin(typ, dict, num_args=2): |
126 | 135 | return |
127 | 136 | (key_typ, val_typ) = typ.__args__ |
128 | | - if key_typ != str: |
129 | | - return |
130 | | - |
131 | | - inner = ctx.lookup(verb=verb, typ=val_typ) |
| 137 | + inner_key = ctx.lookup(verb=_STRING_ACTIONS[verb], typ=key_typ) |
| 138 | + inner_val = ctx.lookup(verb=verb, typ=val_typ) |
132 | 139 | if verb == DDB2PY: |
133 | | - return partial(decode_dict, inner_key=str, inner_val=inner, con=get_origin(typ)) |
| 140 | + return partial( |
| 141 | + decode_dict, inner_key=inner_key, inner_val=inner_val, con=get_origin(typ) |
| 142 | + ) |
134 | 143 | elif verb == PY2DDB: |
135 | | - return partial(encode_dict, inner=inner) |
| 144 | + return partial(encode_dict, inner_key=inner_key, inner_val=inner_val) |
136 | 145 |
|
137 | 146 |
|
138 | 147 | def sets(verb, typ, ctx): |
@@ -169,7 +178,7 @@ def attrs(verb, typ, ctx): |
169 | 178 | """ |
170 | 179 | A rule to represent attrs classes. This isn't trying to support hooks or any of that. |
171 | 180 | """ |
172 | | - inner_map = build_attribute_map(verb, typ, ctx, read_all=verb == PY2DDB) |
| 181 | + inner_map = build_attribute_map(verb, typ, ctx) |
173 | 182 | if inner_map is None: |
174 | 183 | return |
175 | 184 |
|
@@ -322,6 +331,7 @@ def dynamodb_ruleset( |
322 | 331 | enums, |
323 | 332 | sets, |
324 | 333 | dicts, |
| 334 | + stringify_keys, |
325 | 335 | optionals, |
326 | 336 | nulls, |
327 | 337 | *extras, |
@@ -461,8 +471,8 @@ def decode_dict(value, inner_key, inner_val, con): |
461 | 471 | return con(((inner_key(key), inner_val(val)) for key, val in value.items())) |
462 | 472 |
|
463 | 473 |
|
464 | | -def encode_dict(value, inner): |
465 | | - return {"M": {str(key): inner(val) for key, val in value.items()}} |
| 474 | +def encode_dict(value, inner_key, inner_val): |
| 475 | + return {"M": {inner_key(key): inner_val(val) for key, val in value.items()}} |
466 | 476 |
|
467 | 477 |
|
468 | 478 | def decode_map(value, inner_map, con): |
|
0 commit comments