Skip to content

Fix code generator: support generic type parameters and import aliasing - #26

Open
fc221 wants to merge 6 commits into
go-gorm:masterfrom
fc221:master
Open

Fix code generator: support generic type parameters and import aliasing#26
fc221 wants to merge 6 commits into
go-gorm:masterfrom
fc221:master

Conversation

@fc221

@fc221 fc221 commented Nov 27, 2025

Copy link
Copy Markdown
  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

generics types with custom type support

User Case Description

When using generic types from external packages with custom model types as type parameters (e.g., datatypes.JSONSlice[UserTagType] where UserTagType is a custom type), the generator would:

  1. Incorrectly parse package and type names by treating dots inside generic brackets as package separators
  2. Generate invalid Go code with full package paths in generic parameters instead of using short package names with proper imports

Example:

type User struct {
    TagTypes datatypes.JSONSlice[UserTagType] `gorm:"column:tag_types"`
}

TagTypes   field.Struct[datatypes.JSONSlice[models.UserTagType]]
<!-- Summary by @propel-code-bot -->

---

**Generator now properly parses & imports generic types with custom parameters**

This PR refactors the GORM CLI code-generator so it can recognise and rewrite generic type expressions that include custom / external model types (e.g., `datatypes.JSONSlice[models.UserTagType]`). The update prevents erroneous package-path retention inside brackets, adds missing imports, and treats comparable custom enums as scalars. Examples, golden output and unit-tests are expanded to cover the new behaviour.

<details>
<summary><strong>Key Changes</strong></summary>Re-worked `Field.Type()` to locate the last `.` before a `[` token and call new helper `processGenericType()` for generic-aware rewritingIntroduced `Field.processGenericType()`, `File.getImportAliasType()`, `File.getImport()` and `utils.splitGenericArgs()` to recursively normalise package paths and register imports for nested generic argumentsAdded `IsUnderlyingComparable()` in `internal/gen/utils.go` and integrated it in `Field.Type()` allowing custom enum types (non-struct comparables) to map to `field.Field`Updated example model `examples/models/user.go`, golden file `examples/output/models/user.go`, and unit test `internal/gen/generator_test.go` with fields that exercise the new generic parsing logicAdded two stub enum packages under `examples/models/enum` for test coverage

</details>

<details>
<summary><strong>Affected Areas</strong></summary>`internal/gen/generator.go``internal/gen/utils.go`code-gen examples & golden outputunit tests

</details>

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

@propel-code-bot propel-code-bot Bot changed the title Fix generics types with custom type support Fix generator handling of generic types with custom type parameters Nov 27, 2025
@propel-code-bot propel-code-bot Bot changed the title Fix generator handling of generic types with custom type parameters Fix generator parsing and imports for generic types with custom parameters Nov 27, 2025
@propel-code-bot propel-code-bot Bot changed the title Fix generator parsing and imports for generic types with custom parameters Fix code generator: correctly parse & import generic types with custom parameters Nov 28, 2025
@propel-code-bot propel-code-bot Bot changed the title Fix code generator: correctly parse & import generic types with custom parameters Fix code generator: support generic type parameters and import aliasing Nov 28, 2025
@jinzhu
jinzhu requested a review from Copilot July 31, 2026 08:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the internal code generator to better handle Go generic type expressions (especially when type arguments reference external/custom model types) and improves import alias resolution so generated code uses short package names with correct imports.

Changes:

  • Refactors Field.Type() to be generic-aware when splitting package/type names and to normalize generic type arguments via processGenericType.
  • Adds IsUnderlyingComparable plus splitGenericArgs helper logic to support treating comparable custom types (e.g., enums) as scalar fields and to correctly parse nested generic arguments.
  • Expands examples and tests to cover generic type parameters and import aliasing scenarios (including stub enum packages).

Reviewed changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
internal/gen/utils.go Adds helpers for comparable-type detection and splitting generic argument lists.
internal/gen/generator.go Updates type classification to be generic-aware; adds generic rewriting + import alias resolution helpers.
internal/gen/generator_test.go Extends struct parsing test coverage to include new generic/custom types.
examples/output/models/user.go Updates golden generated output to reflect new generic/custom type handling.
examples/models/user.go Extends example model with generic fields and custom enum types/import aliases.
examples/models/enum/enum/enum.go Adds stub enum package for aliasing/import coverage.
examples/models/enum/enum.go Adds stub enum package for aliasing/import coverage.
Files not reviewed (1)
  • examples/output/models/user.go: Generated file

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread internal/gen/generator.go
// Process generic type parameters to convert full paths to short names
goType = f.processGenericType(goType)

if typ := loadNamedType(f.file.goModDir, f.file.getFullImportPath(pkgName), typName); typ != nil {
Comment thread internal/gen/generator.go
Comment on lines +529 to +531
// getImportAliasType returns the import alias type string for a raw type string
// e.g., "datatypes2 gorm.io/datatypes.JSONSlice" -> "datatypes2.JSONSlice"
func (p *File) getImportAliasType(raw string) string {
Comment thread internal/gen/generator.go
Comment on lines +856 to +863
func (p *File) getImport(path string) *Import {
for _, i := range p.Imports {
if i.Path == path {
return &i
}
}
return nil
}
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.

2 participants