fix: handle DatabaseType enum conversion in config management
Fix type conversion issues when handling DatabaseType enum values in configuration loading and GUI updates. - config/loader.py: Handle both enum and string types for DB_TYPE - config/schema.py: Properly convert db_type when it's already a string - gui/config_manager.py: Add Enum type conversion with validation in set_nested_value These changes prevent AttributeError when accessing .value on string values and ensure proper type handling throughout the configuration system. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -111,7 +111,7 @@ class ConfigLoader:
|
|||||||
"ERP_IGNORE_HTTPS_ERRORS": config.erp.ignore_https_errors,
|
"ERP_IGNORE_HTTPS_ERRORS": config.erp.ignore_https_errors,
|
||||||
"ERP_AUTO_CLOSE_BROWSER": config.erp.auto_close_browser,
|
"ERP_AUTO_CLOSE_BROWSER": config.erp.auto_close_browser,
|
||||||
# 数据库配置
|
# 数据库配置
|
||||||
"DB_TYPE": config.database.db_type.value,
|
"DB_TYPE": config.database.db_type.value if isinstance(config.database.db_type, DatabaseType) else config.database.db_type,
|
||||||
"DB_SERVER": config.database.server,
|
"DB_SERVER": config.database.server,
|
||||||
"DB_NAME": config.database.database,
|
"DB_NAME": config.database.database,
|
||||||
"DB_USERNAME": config.database.username,
|
"DB_USERNAME": config.database.username,
|
||||||
|
|||||||
@@ -312,7 +312,7 @@ class AppConfig:
|
|||||||
"auto_close_browser": self.erp.auto_close_browser,
|
"auto_close_browser": self.erp.auto_close_browser,
|
||||||
},
|
},
|
||||||
"database": {
|
"database": {
|
||||||
"db_type": self.database.db_type.value,
|
"db_type": self.database.db_type if isinstance(self.database.db_type, str) else self.database.db_type.value,
|
||||||
"server": self.database.server,
|
"server": self.database.server,
|
||||||
"database": self.database.database,
|
"database": self.database.database,
|
||||||
"username": self.database.username,
|
"username": self.database.username,
|
||||||
|
|||||||
@@ -94,8 +94,20 @@ class ConfigManager:
|
|||||||
for k in keys[:-1]:
|
for k in keys[:-1]:
|
||||||
obj = getattr(obj, k)
|
obj = getattr(obj, k)
|
||||||
|
|
||||||
|
# 获取目标字段信息用于类型转换
|
||||||
|
target_field = keys[-1]
|
||||||
|
field_type = type(getattr(obj, target_field))
|
||||||
|
|
||||||
|
# 如果是字符串且目标字段是枚举类型,进行转换
|
||||||
|
if isinstance(value, str) and hasattr(field_type, "__members__"): # 它是一个 Enum
|
||||||
|
try:
|
||||||
|
value = field_type(value)
|
||||||
|
except ValueError:
|
||||||
|
# 无效的枚举值,保持当前值不变
|
||||||
|
value = getattr(obj, target_field)
|
||||||
|
|
||||||
# 设置最终值
|
# 设置最终值
|
||||||
setattr(obj, keys[-1], value)
|
setattr(obj, target_field, value)
|
||||||
|
|
||||||
def reset_to_defaults(self) -> None:
|
def reset_to_defaults(self) -> None:
|
||||||
"""重置为默认配置"""
|
"""重置为默认配置"""
|
||||||
|
|||||||
Reference in New Issue
Block a user