Skip to content

Commit

Permalink
♻️ 将相同代码提取出来
Browse files Browse the repository at this point in the history
  • Loading branch information
he0119 committed Feb 14, 2024
1 parent 334fe68 commit a80f745
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions nonebot_plugin_saa/registries/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,7 @@ class SerializationMeta(BaseModel, ABC):

@classmethod
def __pydantic_init_subclass__(cls, *args, **kwargs) -> None:
if cls._level == Level.MetaBase:
cls._level = Level.Base
cls._deserializer_dict = {}
elif cls._level == Level.Base:
cls._level = Level.Normal
cls._deserializer_dict[cls.model_fields[cls._index_key].default] = cls
elif cls._level == Level.Normal:
pass
else:
raise RuntimeError("SerializationMeta init error")
cls._register_subclass()

else:

Expand All @@ -46,19 +37,23 @@ class Config:

@classmethod
def __init_subclass__(cls, *args, **kwargs) -> None:
if cls._level == Level.MetaBase:
cls._level = Level.Base
cls._deserializer_dict = {}
elif cls._level == Level.Base:
cls._level = Level.Normal
cls._deserializer_dict[cls.__fields__[cls._index_key].default] = cls
elif cls._level == Level.Normal:
pass
else:
raise RuntimeError("SerializationMeta init error")
cls._register_subclass()

super().__init_subclass__(*args, **kwargs)

@classmethod
def _register_subclass(cls) -> None:
if cls._level == Level.MetaBase:
cls._level = Level.Base
cls._deserializer_dict = {}
elif cls._level == Level.Base:
cls._level = Level.Normal
cls._deserializer_dict[cls.__fields__[cls._index_key].default] = cls
elif cls._level == Level.Normal:
pass
else:
raise RuntimeError("SerializationMeta init error")

@classmethod
def deserialize(cls, source: Any) -> Self:
if isinstance(source, str):
Expand Down

0 comments on commit a80f745

Please sign in to comment.