Skip to content

Commit

Permalink
feat: add Celery encoder for TypeID serialization
Browse files Browse the repository at this point in the history
Generated-by: aiautocommit
  • Loading branch information
iloveitaly committed Feb 1, 2025
1 parent e850016 commit 416b04a
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions activemodel/celery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
Do not import unless you have Celery/Kombu installed.
In order for TypeID objects to be properly handled by celery, a custom encoder must be registered.
"""

from kombu.utils.json import register_type
from typeid import TypeID


def register_celery_typeid_encoder():
"this ensures TypeID objects passed as arguments to a delayed function are properly serialized"

def class_full_name(clz) -> str:
return ".".join([clz.__module__, clz.__qualname__])

def _encoder(obj: TypeID) -> str:
return str(obj)

def _decoder(data: str) -> TypeID:
return TypeID.from_string(data)

register_type(
TypeID,
class_full_name(TypeID),
encoder=_encoder,
decoder=_decoder,
)

0 comments on commit 416b04a

Please sign in to comment.