def decode_json_object(data: dict) -> Do: m = Map(data) tpe_s = yield m.lift(tpe_key).to_either( f'no `{tpe_key}` attr in json object {m}') tpe = yield Either.import_path(tpe_s) dec = yield Decoder.e(tpe) yield dec.decode(tpe, m)
def run(data: Json) -> Do: mod_field = yield data.field('mod') mod_path = yield mod_field.as_scalar names_field = yield data.field('names') names_json = yield names_field.as_array names = Lists.wrap(names_json.native) mod = yield (Either.import_module(mod_path.native) if isinstance( mod_path.native, str) else Left( JsonError(data, 'module is not a string'))) yield names.fold_m(Right(mod))(Either.getattr)
def decode(self, tpe: Type[Sub], data: JsonObject) -> Do: sub_json = yield data.field(tpe_key) sub = yield sub_json.as_scalar sub_type = yield Either.import_path(sub.native) yield decode_dat(sub_type, data)
def _either(self, result: Either, status=404): return result.map(lambda a: "ok").right_or_map(L(error)(_, status))
def select_type(type_info: TypeInfo) -> Do: mod = yield Either.import_module(type_info.module) yield type_info.names.fold_m(Right(mod))(Either.getattr)
def decode_json_object(data: dict) -> Generator: m = Map(data) tpe_s = yield m.get('__type__').to_either(f'no `__type__` attr in json object {m}') tpe = yield Either.import_path(tpe_s) dec = yield Decoder.e(tpe) yield dec.decode(tpe, m)