Common: Type checking in EventSerializerRegistry

This commit is contained in:
Ilija Lazoroski 2022-08-18 15:21:33 +02:00
parent 4b1ad70f84
commit b541dc465d
1 changed files with 24 additions and 2 deletions

View File

@ -7,13 +7,35 @@ from common.events import AbstractEvent
class EventSerializerRegistry: class EventSerializerRegistry:
""" """
Registry for event serializers using event class. Registry for event serializers using event class.
Example:
event_serializer_registry = EventSerializerRegistry()
event_serializer_registry[MyEvent] = MyEventSerializer()
my_event_dict = {"type": "MyEvent", "data": "123"}
serializer = event_serializer_registry[my_event_dict["type"]]
my_event_object = serializer.deserialize(my_event_dict)
""" """
def __init__(self): def __init__(self):
self._registry = {} self._registry = {}
def __setitem__(self, event_class: Type[AbstractEvent], event_serializer: IEventSerializer): def __setitem__(self, event_class: Type[AbstractEvent], event_serializer: IEventSerializer):
self._registry[event_class] = event_serializer if not issubclass(event_class, AbstractEvent):
raise TypeError(f"Event class must be of type: {AbstractEvent.__name__}")
if not isinstance(event_serializer, IEventSerializer):
raise TypeError(f"Event serializer must be of type: {IEventSerializer.__name__}")
self._registry[event_class] = event_serializer
self._registry[event_class.__name__] = event_serializer
def __getitem__(self, event_class: Union[str, Type[AbstractEvent]]) -> IEventSerializer:
if not (isinstance(event_class, str) or issubclass(event_class, AbstractEvent)):
raise TypeError(
f"Registry get key {event_class} must be of type: {AbstractEvent.__name__} or "
f"{str.__name__}"
)
def __getitem__(self, event_class: Union[str, Type[AbstractEvent]]):
return self._registry[event_class] return self._registry[event_class]