Skip to content

Conversation

@fc221
Copy link

@fc221 fc221 commented Nov 27, 2025

  • 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
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.

1 participant