Fix code generator: support generic type parameters and import aliasing - #26
Open
fc221 wants to merge 6 commits into
Open
Fix code generator: support generic type parameters and import aliasing#26fc221 wants to merge 6 commits into
fc221 wants to merge 6 commits into
Conversation
Contributor
There was a problem hiding this comment.
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 viaprocessGenericType. - Adds
IsUnderlyingComparableplussplitGenericArgshelper 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.
| // 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 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 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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]whereUserTagTypeis a custom type), the generator would:Example: