Beispiel #1
0
    async def auto_parse_by_type(original_dict: dict) -> BaseEvent:
        """从尚未明确指定事件类型的对象中获取事件的定义, 并进行解析

        Args:
            original_dict (dict): 用 dict 表示的序列化态事件, 应包含有字段 `type` 以供分析事件定义.

        Raises:
            InvaildArgument: 目标对象中不包含字段 `type`
            ValueError: 没有找到对应的字段, 通常的, 这意味着应用获取到了一个尚未被定义的事件, 请报告问题.

        Returns:
            BaseEvent: 已经被序列化的事件
        """
        if not original_dict.get("type") and not isinstance(
                original_dict.get("type"), str):
            raise InvaildArgument(
                "you need to provide a 'type' field for automatic parsing")
        event_type = Broadcast.findEvent(original_dict.get("type"))
        if not event_type:
            raise ValueError("we cannot find a such event: {}".format(
                original_dict.get("type")))
        return await run_always_await(
            event_type.parse_obj(
                {k: v
                 for k, v in original_dict.items() if k != "type"}))
Beispiel #2
0
 async def auto_parse_by_type(original_dict: dict) -> BaseEvent:
     if not original_dict.get("type") and not isinstance(
             original_dict.get("type"), str):
         raise InvaildArgument(
             "you need to provide a 'type' field for automatic parsing")
     event_type = Broadcast.findEvent(original_dict.get("type"))
     if not event_type:
         raise ValueError("we cannot find a such event: {}".format(
             original_dict.get("type")))
     return await run_always_await(
         event_type.parse_obj(
             {k: v
              for k, v in original_dict.items() if k != "type"}))