1
0
mirror of https://github.com/gogf/gf.git synced 2025-04-05 11:18:50 +08:00

fix(cmd/gf): gen service error when there's version number at the end of package import path (#3836)

This commit is contained in:
oldme 2024-10-05 12:25:43 +08:00 committed by GitHub
parent d4fa2c82bf
commit 183395f0a0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 148 additions and 11 deletions

View File

@ -22,9 +22,9 @@ func Test_Gen_Service_Default(t *testing.T) {
var (
path = gfile.Temp(guid.S())
dstFolder = path + filepath.FromSlash("/service")
apiFolder = gtest.DataPath("genservice", "logic")
srvFolder = gtest.DataPath("genservice", "logic")
in = genservice.CGenServiceInput{
SrcFolder: apiFolder,
SrcFolder: srvFolder,
DstFolder: dstFolder,
DstFileNameCase: "Snake",
WatchFile: "",
@ -46,11 +46,11 @@ func Test_Gen_Service_Default(t *testing.T) {
// logic file
var (
genApi = apiFolder + filepath.FromSlash("/logic.go")
genApiExpect = apiFolder + filepath.FromSlash("/logic_expect.go")
genSrv = srvFolder + filepath.FromSlash("/logic.go")
genSrvExpect = srvFolder + filepath.FromSlash("/logic_expect.go")
)
defer gfile.Remove(genApi)
t.Assert(gfile.GetContents(genApi), gfile.GetContents(genApiExpect))
defer gfile.Remove(genSrv)
t.Assert(gfile.GetContents(genSrv), gfile.GetContents(genSrvExpect))
// files
files, err := gfile.ScanDir(dstFolder, "*.go", true)
@ -80,10 +80,10 @@ func Test_Issue3328(t *testing.T) {
var (
path = gfile.Temp(guid.S())
dstFolder = path + filepath.FromSlash("/service")
apiFolder = gtest.DataPath("issue", "3328", "logic")
logicGoPath = apiFolder + filepath.FromSlash("/logic.go")
srvFolder = gtest.DataPath("issue", "3328", "logic")
logicGoPath = srvFolder + filepath.FromSlash("/logic.go")
in = genservice.CGenServiceInput{
SrcFolder: apiFolder,
SrcFolder: srvFolder,
DstFolder: dstFolder,
DstFileNameCase: "Snake",
WatchFile: "",
@ -106,7 +106,7 @@ func Test_Issue3328(t *testing.T) {
_, err = genservice.CGenService{}.Service(ctx, in)
t.AssertNil(err)
files, err := gfile.ScanDir(apiFolder, "*", true)
files, err := gfile.ScanDir(srvFolder, "*", true)
for _, file := range files {
if file == logicGoPath {
if gfile.IsDir(logicGoPath) {
@ -116,3 +116,40 @@ func Test_Issue3328(t *testing.T) {
}
})
}
// https://github.com/gogf/gf/issues/3835
func Test_Issue3835(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
path = gfile.Temp(guid.S())
dstFolder = path + filepath.FromSlash("/service")
srvFolder = gtest.DataPath("issue", "3835", "logic")
in = genservice.CGenServiceInput{
SrcFolder: srvFolder,
DstFolder: dstFolder,
DstFileNameCase: "Snake",
WatchFile: "",
StPattern: "",
Packages: nil,
ImportPrefix: "",
Clear: false,
}
)
err := gutil.FillStructWithDefault(&in)
t.AssertNil(err)
err = gfile.Mkdir(path)
t.AssertNil(err)
defer gfile.Remove(path)
_, err = genservice.CGenService{}.Service(ctx, in)
t.AssertNil(err)
// contents
var (
genFile = dstFolder + filepath.FromSlash("/issue_3835.go")
expectFile = gtest.DataPath("issue", "3835", "service", "issue_3835.go")
)
t.Assert(gfile.GetContents(genFile), gfile.GetContents(expectFile))
})
}

View File

@ -12,6 +12,7 @@ import (
"go/token"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/text/gstr"
)
type pkgItem struct {
@ -78,12 +79,19 @@ func (c CGenService) parseImportPackages(node *ast.ImportSpec) (packages pkgItem
path = node.Path.Value
rawImport string
)
if node.Name != nil {
alias = node.Name.Name
rawImport = alias + " " + path
rawImport = node.Name.Name + " " + path
} else {
rawImport = path
}
// if the alias is empty, it will further retrieve the real alias.
if alias == "" {
alias = c.getRealAlias(path)
}
return pkgItem{
Alias: alias,
Path: path,
@ -91,6 +99,33 @@ func (c CGenService) parseImportPackages(node *ast.ImportSpec) (packages pkgItem
}
}
// getRealAlias retrieves the real alias of the package.
// If package is "github.com/gogf/gf", the alias is "gf".
// If package is "github.com/gogf/gf/v2", the alias is "gf" instead of "v2".
func (c CGenService) getRealAlias(importPath string) (pkgName string) {
importPath = gstr.Trim(importPath, `"`)
parts := gstr.Split(importPath, "/")
if len(parts) == 0 {
return
}
pkgName = parts[len(parts)-1]
if !gstr.HasPrefix(pkgName, "v") {
return pkgName
}
if len(parts) < 2 {
return pkgName
}
if gstr.IsNumeric(gstr.SubStr(pkgName, 1)) {
pkgName = parts[len(parts)-2]
}
return pkgName
}
// parseFuncReceiverTypeName retrieves the receiver type of the function.
// For example:
//

View File

@ -0,0 +1,23 @@
package issue3835
import (
"context"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/issue/3835/service"
"github.com/gogf/gf/contrib/drivers/mysql/v2"
)
func init() {
service.RegisterItest(New())
}
type sItest struct {
}
func New() *sItest {
return &sItest{}
}
func (s *sItest) F(ctx context.Context) (d mysql.Driver, err error) {
return mysql.Driver{}, nil
}

View File

@ -0,0 +1,9 @@
// ==========================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// ==========================================================================
package logic
import (
_ "github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/issue/3835/logic/issue3835"
)

View File

@ -0,0 +1,33 @@
// ================================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// You can delete these comments if you wish manually maintain this interface file.
// ================================================================================
package service
import (
"context"
"github.com/gogf/gf/contrib/drivers/mysql/v2"
)
type (
IItest interface {
F(ctx context.Context) (d mysql.Driver, err error)
}
)
var (
localItest IItest
)
func Itest() IItest {
if localItest == nil {
panic("implement not found for interface IItest, forgot register?")
}
return localItest
}
func RegisterItest(i IItest) {
localItest = i
}