-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Option to passthrough UUID type #338
Comments
I can add a new option to enable passthrough of UUID objects. Can you describe your use case? |
I would like to be able to pack uuid as bytes using extension type and unpack it back like this: import dataclasses as dc
from typing import Any
from uuid import UUID, uuid4
import ormsgpack as mp
@dc.dataclass
class Message:
id: UUID
...
PACK_OPTIONS = mp.OPT_PASSTHROUGH_UUID | mp.OPT_PASSTHROUGH_DATACLASS
def default(obj: Any) -> mp.Ext:
if dc.is_dataclass(obj):
return mp.Ext(0, mp.packb(dc.astuple(obj), default=default, option=PACK_OPTIONS))
elif isinstance(obj, UUID):
return mp.Ext(1, obj.bytes)
raise TypeError
def ext_hook(tag: int, data: bytes) -> Any:
if tag == 0:
return Message(*mp.unpackb(data, ext_hook=ext_hook))
if tag == 1:
return UUID(bytes=data)
raise TypeError
msg1 = Message(id=uuid4())
data = mp.packb(msg1, default=default, option=PACK_OPTIONS)
msg2 = mp.unpackb(data, ext_hook=ext_hook)
assert msg1 == msg2 I can implement it by myself and submit MR if don't have enough time. |
I see.
I can do it, but thanks for the offer. I will add the option and release a new version soon. |
Opened #341. |
Is it possible to passthrough UUID type during packing similar to datetime (using OPT_PASSTHROUGH_DATETIME)?
The text was updated successfully, but these errors were encountered: