Synchronize mcp service api

This commit is contained in:
Liujian committed 2025-10-15 14:49:20 +08:00
1 parent a0b827b32e
commit c790fbef51
15 files changed
+316 -66

No files matched your search

+29 -1
View File
@@ -1,8 +1,17 @@
from typing import get_origin, get_args
from enum import Enum as PyEnum
class SqlalchemyUtils:
@staticmethod
def model_to_dict(model):
return {column.name: getattr(model, column.name) for column in model.__table__.columns}
result = {column.name: getattr(model, column.name) for column in model.__table__.columns}
# Normalize Enum values to primitive for safe serialization
for key, value in list(result.items()):
if isinstance(value, PyEnum):
result[key] = value.value
return result
@staticmethod
def dict_to_model(model_class, data_dict):
@@ -24,6 +33,25 @@ class SqlalchemyUtils:
# Set attributes that exist in both the dictionary and the model
for key, value in data_dict.items():
if key in column_names and hasattr(instance, key):
# Attempt to restore Enum types from strings using type annotations
annotations = getattr(model_class, "__annotations__", {})
ann = annotations.get(key)
target_type = None
if ann is not None:
origin = get_origin(ann)
if origin is None:
target_type = ann
else:
args = get_args(ann)
if args:
target_type = args[0]
if isinstance(target_type, type) and issubclass(target_type, PyEnum) and isinstance(value, str):
try:
value = target_type(value)
except Exception:
pass
setattr(instance, key, value)
return instance