From 5f964151c2f3da71a2c44479cbe76df3d221de45 Mon Sep 17 00:00:00 2001 From: andrii-hantkovskyi <131773947+andrii-hantkovskyi@users.noreply.github.com> Date: Wed, 27 Mar 2024 16:43:47 +0200 Subject: [PATCH] feat: [ACI-800] implement avro serializer for ccx locator (#9) Co-authored-by: Andrii --- .../event_bus/avro/custom_serializers.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/openedx_events/event_bus/avro/custom_serializers.py b/openedx_events/event_bus/avro/custom_serializers.py index 37a8a150..0ce6bf41 100644 --- a/openedx_events/event_bus/avro/custom_serializers.py +++ b/openedx_events/event_bus/avro/custom_serializers.py @@ -5,6 +5,7 @@ from abc import ABC, abstractmethod from datetime import datetime +from ccx_keys.locator import CCXLocator from opaque_keys.edx.keys import CourseKey, UsageKey from opaque_keys.edx.locator import LibraryLocatorV2, LibraryUsageLocatorV2 @@ -49,6 +50,25 @@ def deserialize(data: str): return CourseKey.from_string(data) +class CcxCourseLocatorAvroSerializer(BaseCustomTypeAvroSerializer): + """ + CustomTypeAvroSerializer for CCXLocator class. + """ + + cls = CCXLocator + field_type = PYTHON_TYPE_TO_AVRO_MAPPING[str] + + @staticmethod + def serialize(obj) -> str: + """Serialize obj into string.""" + return str(obj) + + @staticmethod + def deserialize(data: str): + """Deserialize string into obj.""" + return CCXLocator.from_string(data) + + class DatetimeAvroSerializer(BaseCustomTypeAvroSerializer): """ CustomTypeAvroSerializer for datetime class. @@ -131,6 +151,7 @@ def deserialize(data: str): DEFAULT_CUSTOM_SERIALIZERS = [ CourseKeyAvroSerializer, + CcxCourseLocatorAvroSerializer, DatetimeAvroSerializer, LibraryLocatorV2AvroSerializer, LibraryUsageLocatorV2AvroSerializer,