Skip to content

Commit

Permalink
Fixed serialization
Browse files Browse the repository at this point in the history
Changelog
  • Loading branch information
davidhozic committed Jan 30, 2024
1 parent 55240fd commit 14ba0f2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
4 changes: 4 additions & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ Glossary
Releases
---------------------

v4.0.1
====================
- Fixed remote serialization of regex and time.

v4.0.0
===================
- New automatic message responder (DM and guild) - :ref:`Automatic responder`.
Expand Down
2 changes: 1 addition & 1 deletion src/daf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import warnings


VERSION = "4.0.0"
VERSION = "4.0.1"


if sys.version_info.minor == 12 and sys.version_info.major == 3:
Expand Down
14 changes: 12 additions & 2 deletions src/daf/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from typing import Union, Any, Mapping
from contextlib import suppress
from enum import Enum
from enum import Enum, Flag
from inspect import isclass, isfunction, signature, _empty

from daf.logging.tracing import TraceLEVELS, trace
Expand All @@ -15,6 +15,7 @@
import copy
import asyncio
import datetime
import re

import _discord as discord

Expand Down Expand Up @@ -123,6 +124,10 @@ def import_class(path: str):
"custom_encoder": lambda object: object.total_seconds(),
"custom_decoder": lambda seconds: datetime.timedelta(seconds=seconds)
},
datetime.time: {
"custom_encoder": lambda object: object.isoformat(),
"custom_decoder": lambda string: datetime.time.fromisoformat(string)
},
bytes: {
"custom_encoder": lambda data: data.hex(),
"custom_decoder": lambda hex_str: bytes.fromhex(hex_str)
Expand All @@ -146,8 +151,13 @@ def import_class(path: str):
},
discord.VoiceChannel: {
"attrs": ["name", "id"],
},
re.Pattern: {
"custom_encoder": lambda data: {"pattern": convert_object_to_semi_dict(data.pattern), "flags": data.flags},
"custom_decoder": lambda data: re.compile(data["pattern"], data.get("flags", 0))
}
}

"""
This is a custom conversion dictionary.
It's values are datatypes of objects which cannot be normally converted to JSON, so custom rules are required.
Expand Down Expand Up @@ -367,7 +377,7 @@ def _convert_json_slots(to_convert):
to_convert = [convert_object_to_semi_dict(value) for value in to_convert]
return to_convert

if isinstance(to_convert, Enum):
if isinstance(to_convert, (Enum, Flag)):
return {"enum_type": f"{object_type.__module__}.{object_type.__name__}", "value": to_convert.value}

# Class itself, not an actual instance. Can also be function as it only imports.
Expand Down
2 changes: 1 addition & 1 deletion src/daf/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def pattern(self):

@property
def flags(self):
return self._compiled.flags
return re.RegexFlag(self._compiled.flags)

@property
def full_match(self):
Expand Down

0 comments on commit 14ba0f2

Please sign in to comment.