Skip to content

提取 ORM loader 公共代码到公共模块,消除三框架间的重复 #267

Description

@allmonday

问题

sqlalchemy/loader.pydjango/loader.pytortoise/loader.py 中存在大量完全相同的工具函数:

  • _normalize_identifier — 完全相同
  • _build_loader_identity — 完全相同
  • _dedupe_fields — 完全相同
  • _get_default_fields — 完全相同
  • _get_effective_query_fields — 几乎一致
  • _finalize_loader_class — 仅前缀不同(SA_DJ_TO_

建议方案

  1. 创建 pydantic_resolve/integration/common.py 公共模块
  2. 将上述函数提取到公共模块,_finalize_loader_class 接受前缀参数
  3. 各 ORM loader import 公共函数,只保留差异部分
# pydantic_resolve/integration/common.py
def _finalize_loader_class(cls, identity, prefix):
    class_name = f"{prefix}_{identity}"
    cls.__name__ = class_name
    cls.__qualname__ = class_name
    cls.__module__ = __name__
    return cls

收益

  • 减少约 80-100 行重复代码
  • 一处修 bug 处处生效
  • 新增 ORM 适配器时只需关注差异部分

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions