add rename key to sort key

This commit is contained in:
2025-08-19 15:04:37 -03:00
parent 3ee190c66f
commit 18221ee1f4
4 changed files with 71 additions and 20 deletions

View File

@@ -1,6 +1,7 @@
import json
import logging
import os
import warnings
from abc import ABC, abstractmethod
from base64 import urlsafe_b64decode, urlsafe_b64encode
from dataclasses import dataclass
@@ -33,7 +34,7 @@ serializer = TypeSerializer()
deserializer = TypeDeserializer()
def _serialize_to_basic_types(data: Any) -> str | dict | set:
def _serialize_to_basic_types(data: Any) -> str | dict | set | list:
match data:
case datetime():
return data.isoformat()
@@ -42,7 +43,12 @@ def _serialize_to_basic_types(data: Any) -> str | dict | set:
case IPv4Address():
return str(data)
case tuple() | list():
return set(_serialize_to_basic_types(v) for v in data)
serialized = [_serialize_to_basic_types(v) for v in data]
if any(isinstance(v, dict) for v in serialized):
return serialized
return set(serialized)
case dict():
return {k: _serialize_to_basic_types(v) for k, v in data.items()}
case _:
@@ -85,6 +91,12 @@ else:
*,
delimiter: str = '#',
) -> str:
warnings.warn(
'ComposeKey() is deprecated and will be removed in the future.',
DeprecationWarning,
stacklevel=2,
)
if isinstance(keyparts, str):
keyparts = (keyparts,)
@@ -119,6 +131,12 @@ else:
class PrefixKey(str):
def __new__(cls, prefix: str, delimiter: str | None = '#') -> str:
warnings.warn(
'PrefixKey() is deprecated and will be removed in the future.',
DeprecationWarning,
stacklevel=2,
)
if not delimiter:
return super().__new__(cls, prefix)
@@ -145,12 +163,15 @@ if TYPE_CHECKING:
The sort key value.
path_spec: str, optional
Optional specification for nested data extraction.
rename_key : str, optional
If provided, renames the sort key in the output.
remove_prefix: str, optional
Optional prefix to remove from the key when forming the result dict.
"""
sk: str
path_spec: str | None = None
rename_key: str | None = None
remove_prefix: str | None = None
else:
@@ -166,6 +187,8 @@ else:
The sort key value.
path_spec: str, optional
Optional specification for nested data extraction.
rename_key : str, optional
If provided, renames the sort key in the output.
remove_prefix: str, optional
Optional prefix to remove from the key when forming the result dict.
"""
@@ -175,6 +198,7 @@ else:
sk: str,
*,
path_spec: str | None = None,
rename_key: str | None = None,
remove_prefix: str | None = None,
) -> str:
return super().__new__(cls, sk)
@@ -184,12 +208,14 @@ else:
sk: str,
*,
path_spec: str | None = None,
rename_key: str | None = None,
remove_prefix: str | None = None,
) -> None:
# __init__ is used to store the parameters for later reference.
# For immutable types like str, __init__ cannot change the instance's value.
self.sk = sk
self.path_spec = path_spec
self.rename_key = rename_key
self.remove_prefix = remove_prefix
@@ -1074,6 +1100,9 @@ class DynamoDBCollection:
if pair.rename_key:
return pair.rename_key
if getattr(sk, 'rename_key', None):
return sk.rename_key
if not isinstance(sk, SortKey):
return pk