Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support UUID's correctly; convert them to/from JSON correctly. #31

Merged
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions xmodel/converters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import uuid
from uuid import UUID

import ciso8601

Expand Down Expand Up @@ -249,6 +249,33 @@ def convert_decimal(
return Decimal(value)


def convert_uuid(
api,
direction: Direction,
field: Field,
value: Any,
):
if value is None or value is Null:
return value

if direction is Direction.to_json:
# Convert UUID object into a str.
return str(value)

if direction not in (Direction.to_model, Direction.from_json):
# We don't know the direction (new direct?)
raise XModelError(
f"Unknown direction ({direction}), can't convert value ({value}); "
f"is this a new direction I need to handle?"
)

# Going into model, return a UUID.
if isinstance(value, UUID):
return value

return UUID(value)


DEFAULT_CONVERTERS: Dict[Type, Converter] = {
Decimal: convert_decimal,
dt.date: convert_json_date,
Expand All @@ -257,5 +284,5 @@ def convert_decimal(
float: ConvertBasicType(basic_type=float),
str: ConvertBasicType(basic_type=str),
bool: ConvertBasicBool(),
uuid.UUID: ConvertBasicType(basic_type=str),
UUID: convert_uuid,
}
Loading