mirror of
https://github.com/A-Minos/nonebot-plugin-tetris-stats.git
synced 2026-03-05 05:36:54 +08:00
✨ 适配 Pydantic V2
This commit is contained in:
@@ -3,7 +3,7 @@ from datetime import datetime
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from nonebot.adapters import Message
|
from nonebot.adapters import Message
|
||||||
from nonebot.compat import type_validate_json
|
from nonebot.compat import PYDANTIC_V2, type_validate_json
|
||||||
from nonebot_plugin_orm import Model
|
from nonebot_plugin_orm import Model
|
||||||
from pydantic import BaseModel, ValidationError
|
from pydantic import BaseModel, ValidationError
|
||||||
from sqlalchemy import JSON, DateTime, Dialect, PickleType, String, TypeDecorator
|
from sqlalchemy import JSON, DateTime, Dialect, PickleType, String, TypeDecorator
|
||||||
@@ -22,12 +22,22 @@ class PydanticType(TypeDecorator):
|
|||||||
self.get_model = get_model
|
self.get_model = get_model
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
@override
|
if PYDANTIC_V2:
|
||||||
def process_bind_param(self, value: Any | None, dialect: Dialect) -> str:
|
|
||||||
# 将 Pydantic 模型实例转换为 JSON
|
@override
|
||||||
if isinstance(value, tuple(self.get_model())):
|
def process_bind_param(self, value: Any | None, dialect: Dialect) -> str:
|
||||||
return value.json(by_alias=True) # type: ignore[union-attr]
|
# 将 Pydantic 模型实例转换为 JSON
|
||||||
raise TypeError
|
if isinstance(value, tuple(self.get_model())):
|
||||||
|
return value.model_dump_json(by_alias=True) # type: ignore[union-attr]
|
||||||
|
raise TypeError
|
||||||
|
else:
|
||||||
|
|
||||||
|
@override
|
||||||
|
def process_bind_param(self, value: Any | None, dialect: Dialect) -> str:
|
||||||
|
# 将 Pydantic 模型实例转换为 JSON
|
||||||
|
if isinstance(value, tuple(self.get_model())):
|
||||||
|
return value.json(by_alias=True) # type: ignore[union-attr]
|
||||||
|
raise TypeError
|
||||||
|
|
||||||
@override
|
@override
|
||||||
def process_result_value(self, value: Any | None, dialect: Dialect) -> BaseModel:
|
def process_result_value(self, value: Any | None, dialect: Dialect) -> BaseModel:
|
||||||
|
|||||||
Reference in New Issue
Block a user