Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
zshipko committed Sep 25, 2024
1 parent 5cffa9c commit 7b6bd34
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions lib/src/prelude.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@
import extism_ffi as ffi

LogLevel = ffi.LogLevel
log = ffi.log
input_str = ffi.input_str
input_bytes = ffi.input_bytes
output_str = ffi.output_str
output_bytes = ffi.output_bytes
memory = ffi.memory

def log(level, msg):
if isinstance(msg, bytes):
msg = msg.decode()
elif not isinstance(msg, str):
msg = str(msg)
ffi.log(level, msg)

HttpRequest = ffi.HttpRequest

__exports = []
Expand All @@ -38,25 +44,33 @@ def decode(s: bytes):
"""Decode a value from bytes"""
raise Exception("encode not implemented")

def __post_init__(self):
self._fix_fields()

def _fix_fields(self):
if not hasattr(self, '__dict__'):
return
types = self.__annotations__
for k, v in self.__dict__.items():
if k in types:
setattr(self, k, self._fix_field(types[k], k, v))
setattr(self, k, self._fix_field(types[k], v))
return self

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

return v
def _fix_field(self, ty: type, v):
def check_subclass(a, b):
try:
return issubclass(a, b)
except Exception as _:
return False
if isinstance(v, dict) and check_subclass(ty, Codec):
return ty(**v)._fix_fields()
elif isinstance(v, str) and check_subclass(ty, Enum):
return ty(v)
elif isinstance(v, list) and hasattr(ty, '__origin__') and ty.__origin__ is list:
ty = ty.__args__[0]
return [self._fix_field(ty, x) for x in v]
else:
return v


class JSONEncoder(json.JSONEncoder):
Expand Down

0 comments on commit 7b6bd34

Please sign in to comment.