Skip to content

Commit

Permalink
wip: enums
Browse files Browse the repository at this point in the history
  • Loading branch information
zshipko committed Sep 25, 2024
1 parent 2f8a6ed commit 5cffa9c
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/src/prelude.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def _fix_field(self, ty: type, k, v):
try:
if isinstance(v, dict) and issubclass(ty, Codec):
return ty(**v)._fix_fields()
elif isinstance(v, str) and issubclass(ty, Enum):
return ty(v)
except Exception as _:
pass

Expand All @@ -65,6 +67,8 @@ def default(self, o):
return b64encode(o).decode()
elif isinstance(o, datetime):
return o.isoformat()
elif isinstance(o, Enum):
return str(o.value)
return self.super().encode(o)


Expand Down Expand Up @@ -118,6 +122,8 @@ def _store(x) -> int:
return ffi.memory.alloc(json.dumps(x, cls=JSONEncoder).encode()).offset
elif isinstance(x, Codec):
return ffi.memory.alloc(x.encode()).offset
elif isinstance(x, Enum):
return ffi.memory.alloc(str(x.value).encode()).offset
elif isinstance(x, ffi.memory.MemoryHandle):
return x.offset
elif isinstance(x, int):
Expand All @@ -144,6 +150,8 @@ def _load(t, x):
return json.loads(ffi.memory.string(mem), cls=JSONDecoder)
elif issubclass(t, Codec):
return t.decode(ffi.memory.bytes(mem))
elif issubclass(t, Enum):
return t(ffi.memory.string(mem))
elif t is ffi.memory.MemoryHandle:
return mem
elif t is type(None):
Expand Down

0 comments on commit 5cffa9c

Please sign in to comment.