Skip to content

Conversation

@tr1v3r
Copy link
Member

@tr1v3r tr1v3r commented Apr 9, 2024

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

User Case Description

@tr1v3r tr1v3r force-pushed the feature/generics branch from b842b51 to c5fbbbc Compare April 10, 2024 12:12
@tr1v3r tr1v3r changed the title refactor: refactor funcs with generics WIP: refactor: refactor funcs with generics Apr 10, 2024
@fy0
Copy link

fy0 commented Jun 29, 2025

非常期待这一特性,能减少生成代码中很多重复部分

@propel-code-bot
Copy link

propel-code-bot bot commented Oct 14, 2025

MAJOR: Refactor Field and CRUD Layer to Use Generics (Go 1.18+) for GORM Code Gen

This PR implements a major refactor of the query/model generation in the GORM code generator to leverage Go 1.18+ generics. The key motivation is to enable type-safe, extensible, and more expressive method signatures for generated DAOs and model field types, reducing code duplication and making method chains/types parameterizable. Major parts of the codebase related to model field types, the CRUD/data access layer, and template generation have been rewritten to support both legacy and new generic modes. There is also extensive regeneration of unit test expect files to verify generic support, and dependencies have been updated (notably, introducing golang.org/x/exp/constraints for generic type constraints).

The overall design introduces new generic abstractions (Number[T], Chars[T], generic DAOs, etc.) that systematically replace old, type-specialized methods/types for primitive data types, unifying field handling and CRUD operations. Code generation templates, internal APIs, type signatures, method receivers, and compatibility logic have been reworked to phase in generic support but with backwards compatibility switches. Generated files, unit tests, and method struct/DAO interface code are all now generic-aware.

Key Changes

• Introduces generic wrapper types for model fields (e.g., Number[T], Chars[T], generic Field aliases) and refactors field/type files accordingly.
• Implements a generic DAO interface (IGenericsDo[T, E]) and struct (GenericsDo[T, E]) with generic CRUD methods, supporting type-safe chainable operations and auto-typed returns.
• Updates code generation templates/internal templates to render generic-aware method structs, DAOs, and CRUD method suites for user tables and user code.
• Adds generic mode controls to config, enabling generic/non-generic hybrid mode through a new WithGeneric flag.
• Augments generated tests and expectations to verify generic behaviors for query methods and data models, confirming backwards-compatibility.
• Major refactor of field and type method code: old per-type logic for ints, floats, strings, bytes, etc., has been replaced/simplified with single generic logic using constraint-based generics and generic method implementation.
• Updates go.mod and go.sum to add golang.org/x/exp (for constraints), upgrade dependency versions, and tidy up transitive dependencies.
• Internal code and template rendering logic is updated for dual support of legacy and generics modes, including interface/type switches and new helper methods.

Affected Areas

field/ package (all model field type implementations)
generics.go and all new generic-aware DAO/model structures
• Internal code and templates for query/model generation (internal/template/struct.go, internal/template/method.go, etc.)
generator.go, generate/query.go, config and codegen infrastructure
• Test suite and .expect/ snapshot directories (auto-regenerated for generics coverage)
• go.mod and go.sum

This summary was automatically generated by @propel-code-bot

Copy link

@propel-code-bot propel-code-bot bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, ship it! 🚢

Details
Why was this auto-approved?
AI analysis completed with no actionable comments or suggestions.

@tr1v3r tr1v3r changed the title WIP: refactor: refactor funcs with generics refactor: refactor funcs with generics Oct 15, 2025
@tr1v3r tr1v3r merged commit e408ec2 into master Oct 15, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants