-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Celery encoder for TypeID serialization
Generated-by: aiautocommit
- Loading branch information
1 parent
e850016
commit 416b04a
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |