Skip to content

Commit 609f44c

Browse files
fix(cmd/gf): fix genservice losing versioned import paths (#4242) (#4638)
## Summary - Fix `gf gen service` incorrectly handling versioned imports (e.g., `github.com/minio/minio-go/v7` → `github.com/minio/minio-go`) - The root cause was faulty package name inference from import paths - Go allows package names to differ from directory names - Solution: Keep all non-anonymous imports and let gofmt clean up unused ones ## Changes - Simplified `calculateImportedItems` function in `genservice_calculate.go` - Added test case for versioned imports and aliased imports ## Test plan - [x] All existing genservice tests pass (`Test_Gen_Service_Default`, `Test_Issue3328`, `Test_Issue3835`) - [x] New test `Test_Issue4242` verifies both versioned imports and aliased imports are preserved - [x] Verified generated files match expected output exactly Closes #4242 --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 0a82036 commit 609f44c

File tree

7 files changed

+205
-13
lines changed

7 files changed

+205
-13
lines changed

cmd/gf/internal/cmd/cmd_z_unit_gen_service_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,48 @@ func Test_Gen_Service_PackagesFilter(t *testing.T) {
238238
t.Assert(files[0], dstFolder+filepath.FromSlash("/user.go"))
239239
})
240240
}
241+
242+
// https://github.com/gogf/gf/issues/4242
243+
// Test that versioned imports and aliased imports are correctly preserved.
244+
// The issue is that imports like "github.com/minio/minio-go/v7" were being
245+
// incorrectly handled because the package name (minio) differs from
246+
// the directory name (minio-go).
247+
func Test_Issue4242(t *testing.T) {
248+
gtest.C(t, func(t *gtest.T) {
249+
var (
250+
path = gfile.Temp(guid.S())
251+
dstFolder = path + filepath.FromSlash("/service")
252+
srvFolder = gtest.DataPath("issue", "4242", "logic")
253+
in = genservice.CGenServiceInput{
254+
SrcFolder: srvFolder,
255+
DstFolder: dstFolder,
256+
DstFileNameCase: "Snake",
257+
WatchFile: "",
258+
StPattern: "",
259+
Packages: nil,
260+
ImportPrefix: "",
261+
Clear: false,
262+
}
263+
)
264+
err := gutil.FillStructWithDefault(&in)
265+
t.AssertNil(err)
266+
267+
err = gfile.Mkdir(path)
268+
t.AssertNil(err)
269+
defer gfile.Remove(path)
270+
271+
_, err = genservice.CGenService{}.Service(ctx, in)
272+
t.AssertNil(err)
273+
274+
// Test versioned imports
275+
t.Assert(
276+
gfile.GetContents(dstFolder+filepath.FromSlash("/issue_4242.go")),
277+
gfile.GetContents(gtest.DataPath("issue", "4242", "service", "issue_4242.go")),
278+
)
279+
// Test aliased imports
280+
t.Assert(
281+
gfile.GetContents(dstFolder+filepath.FromSlash("/issue_4242_alias.go")),
282+
gfile.GetContents(gtest.DataPath("issue", "4242", "service", "issue_4242_alias.go")),
283+
)
284+
})
285+
}

cmd/gf/internal/cmd/genservice/genservice_calculate.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
"github.com/gogf/gf/v2/container/garray"
1414
"github.com/gogf/gf/v2/container/gmap"
15-
"github.com/gogf/gf/v2/os/gfile"
1615
"github.com/gogf/gf/v2/text/gregex"
1716
"github.com/gogf/gf/v2/text/gstr"
1817

@@ -37,21 +36,14 @@ func (c CGenService) calculateImportedItems(
3736
}
3837

3938
for _, item := range pkgItems {
40-
alias := item.Alias
41-
42-
// If the alias is _, it means that the package is not generated.
43-
if alias == "_" {
39+
// Skip anonymous imports
40+
if item.Alias == "_" {
4441
mlog.Debugf(`ignore anonymous package: %s`, item.RawImport)
4542
continue
4643
}
47-
// If the alias is empty, it will use the package name as the alias.
48-
if alias == "" {
49-
alias = gfile.Basename(gstr.Trim(item.Path, `"`))
50-
}
51-
if !gstr.Contains(allFuncParamType.String(), alias) {
52-
mlog.Debugf(`ignore unused package: %s`, item.RawImport)
53-
continue
54-
}
44+
// Keep all imports, let gofmt clean up unused ones.
45+
// We cannot accurately infer package name from import path
46+
// (e.g., path "minio-go" but package name is "minio").
5547
srcImportedPackages.Add(item.RawImport)
5648
}
5749
return nil
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package issue4242
2+
3+
import (
4+
"context"
5+
6+
"github.com/gogf/gf/v2/frame/g"
7+
"github.com/gogf/gf/v2/net/ghttp"
8+
9+
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/issue/4242/service"
10+
11+
"github.com/gogf/gf/contrib/drivers/mysql/v2"
12+
)
13+
14+
func init() {
15+
service.RegisterIssue4242(New())
16+
}
17+
18+
type sIssue4242 struct {
19+
}
20+
21+
func New() *sIssue4242 {
22+
return &sIssue4242{}
23+
}
24+
25+
// GetDriver tests versioned import path is preserved.
26+
func (s *sIssue4242) GetDriver(ctx context.Context) (d mysql.Driver, err error) {
27+
return mysql.Driver{}, nil
28+
}
29+
30+
// GetRequest tests another versioned import.
31+
func (s *sIssue4242) GetRequest(ctx context.Context) (*ghttp.Request, error) {
32+
g.Log().Info(ctx, "getting request")
33+
return nil, nil
34+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package issue4242alias
2+
3+
import (
4+
"context"
5+
6+
// Anonymous import (should be skipped)
7+
_ "github.com/gogf/gf/v2/os/gres"
8+
9+
// Versioned import without alias
10+
"github.com/gogf/gf/v2/net/ghttp"
11+
12+
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/issue/4242/service"
13+
14+
// Explicit alias import
15+
mysqlDriver "github.com/gogf/gf/contrib/drivers/mysql/v2"
16+
)
17+
18+
func init() {
19+
service.RegisterIssue4242Alias(New())
20+
}
21+
22+
type sIssue4242Alias struct {
23+
}
24+
25+
func New() *sIssue4242Alias {
26+
return &sIssue4242Alias{}
27+
}
28+
29+
// GetDriver tests explicit alias import.
30+
func (s *sIssue4242Alias) GetDriver(ctx context.Context) (d mysqlDriver.Driver, err error) {
31+
return mysqlDriver.Driver{}, nil
32+
}
33+
34+
// GetRequest tests versioned import.
35+
func (s *sIssue4242Alias) GetRequest(ctx context.Context) (*ghttp.Request, error) {
36+
return nil, nil
37+
}

cmd/gf/internal/cmd/testdata/issue/4242/logic/logic.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/gf/internal/cmd/testdata/issue/4242/service/issue_4242.go

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/gf/internal/cmd/testdata/issue/4242/service/issue_4242_alias.go

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)