Skip to content

Commit

Permalink
serializer protocol fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nekufa committed Sep 23, 2023
1 parent 422d874 commit 3d2988f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
8 changes: 3 additions & 5 deletions sharded_queue/drivers.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
from json import dumps, loads
from typing import Any, List, Sequence, TypeVar
from typing import Any, List, Sequence

from redis.asyncio import Redis

from sharded_queue.protocols import Lock, Serializer, Storage
from sharded_queue.settings import settings

T = TypeVar('T')


class JsonTupleSerializer(Serializer):
def get_values(self, request) -> list[Any]:
if isinstance(request, Sequence):
return [k for k in request]
return list(request.__dict__.values())

def serialize(self, request: T) -> str:
def serialize(self, request: Any) -> str:
return dumps(self.get_values(request))

def deserialize(self, cls: type[T], source: str) -> T:
def deserialize(self, cls: type[Any], source: str) -> Any:
values = loads(source)
if hasattr(cls, 'model_fields'):
return cls(**dict(zip(cls.model_fields, values)))
Expand Down
10 changes: 4 additions & 6 deletions sharded_queue/protocols.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from typing import Any, Protocol, TypeVar

T = TypeVar('T')
from typing import Any, Protocol


class Lock(Protocol):
Expand All @@ -10,10 +8,10 @@ async def release(self, key: str) -> None: ...
async def ttl(self, key: str, ttl: int) -> bool: ...


class Serializer(Protocol[T]):
class Serializer(Protocol):
def get_values(self, request) -> list[Any]: ...
def serialize(self, request: T) -> str: ...
def deserialize(self, cls: type[T], source: str) -> T: ...
def serialize(self, request: Any) -> str: ...
def deserialize(self, cls: type[Any], source: str) -> Any: ...


class Storage(Protocol):
Expand Down

0 comments on commit 3d2988f

Please sign in to comment.