fix duplicate controller method generation` #4564
Draft
+106
−3
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.
Description
This PR addresses issue #4428 where
gf gen ctrl -mincorrectly generates duplicate controller methods. This happens when the controller file imports a package that shares the same version suffix as the API (e.g.,v2) but uses an alias or is a different library (e.g.,excelize "github.com/xuri/excelize/v2").Problem:
When scanning existing controllers, the generator attempts to identify which methods have already been generated by matching imports against the API version. Previously, this logic was flawed:
v2).As a result, the generator would fail to recognize existing API method implementations, assume they were missing, and regenerate them, causing duplication errors.
Solution:
go/ast) to robustly parse imports, correctly capturing both the package path and any explicit aliases.excelizealiasing.../v2will no longer match API versionv2).Test
Added a reproduction test case Test_Gen_Ctrl_Issue4428 in cmd/gf/internal/cmd/cmd_z_unit_gen_ctrl_issue_4428_test.go:
excelize "github.com/xuri/excelize/v2") into the generated controller.Verification:
The test passes, confirming that the generator now correctly handles aliased imports and avoids duplication.