add parse_obj

This commit is contained in:
2025-04-12 23:07:22 -03:00
parent f7aeacb66a
commit bef51f492a
2 changed files with 18 additions and 1 deletions

View File

@@ -5,7 +5,7 @@ from base64 import urlsafe_b64decode, urlsafe_b64encode
from dataclasses import dataclass
from datetime import datetime
from ipaddress import IPv4Address
from typing import TYPE_CHECKING, Any, Type, TypedDict
from typing import TYPE_CHECKING, Any, Self, Type, TypedDict
from urllib.parse import quote, unquote
from uuid import UUID
@@ -251,6 +251,19 @@ class KeyPair(Key):
':sk': self[SK],
}
@classmethod
def parse_obj(cls, obj: Any) -> Self | None:
if not obj:
return None
match obj:
case dict():
pair = obj.values()
case _:
pair = obj
return cls(*pair)
class TransactItems:
def __init__(self, table_name: str) -> None:

View File

@@ -75,6 +75,10 @@ def test_keypair():
assert KeyPair('123', 'abc').expr_attr_name() == {'#pk': 'id', '#sk': 'sk'}
assert KeyPair('123', 'abc').expr_attr_values() == {':pk': '123', ':sk': 'abc'}
assert KeyPair.parse_obj({'id': '123', 'sk': 'abc'}) == {'id': '123', 'sk': 'abc'}
assert KeyPair.parse_obj(['123', 'abc']) == {'id': '123', 'sk': 'abc'}
assert KeyPair.parse_obj([]) is None
def test_prefixkey():
key = PrefixKey('emails')