mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-03 00:38:40 +08:00
api文件中使用group时生成的handler和logic的包名应该为group的名字 (#545)
* api文件中使用group时生成的handler和logic的包名应该为group的名字 * Update genhandlers.go fix errors. Co-authored-by: Kevin Wan <wanjunfeng@gmail.com>
This commit is contained in:
parent
d8905b9e9e
commit
bc92b57bdb
@ -13,7 +13,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
||||||
)
|
)
|
||||||
|
|
||||||
const handlerTemplate = `package handler
|
const handlerTemplate = `package {{.PkgName}}
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -30,7 +30,7 @@ func {{.HandlerName}}(ctx *svc.ServiceContext) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
{{end}}l := logic.New{{.LogicType}}(r.Context(), ctx)
|
{{end}}l := {{.LogicName}}.New{{.LogicType}}(r.Context(), ctx)
|
||||||
{{if .HasResp}}resp, {{end}}err := l.{{.Call}}({{if .HasRequest}}req{{end}})
|
{{if .HasResp}}resp, {{end}}err := l.{{.Call}}({{if .HasRequest}}req{{end}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpx.Error(w, err)
|
httpx.Error(w, err)
|
||||||
@ -42,9 +42,11 @@ func {{.HandlerName}}(ctx *svc.ServiceContext) http.HandlerFunc {
|
|||||||
`
|
`
|
||||||
|
|
||||||
type handlerInfo struct {
|
type handlerInfo struct {
|
||||||
|
PkgName string
|
||||||
ImportPackages string
|
ImportPackages string
|
||||||
HandlerName string
|
HandlerName string
|
||||||
RequestType string
|
RequestType string
|
||||||
|
LogicName string
|
||||||
LogicType string
|
LogicType string
|
||||||
Call string
|
Call string
|
||||||
HasResp bool
|
HasResp bool
|
||||||
@ -54,18 +56,28 @@ type handlerInfo struct {
|
|||||||
|
|
||||||
func genHandler(dir, rootPkg string, cfg *config.Config, group spec.Group, route spec.Route) error {
|
func genHandler(dir, rootPkg string, cfg *config.Config, group spec.Group, route spec.Route) error {
|
||||||
handler := getHandlerName(route)
|
handler := getHandlerName(route)
|
||||||
if getHandlerFolderPath(group, route) != handlerDir {
|
handlerPath := getHandlerFolderPath(group, route)
|
||||||
|
pkgName := handlerPath[strings.LastIndex(handlerPath, "/")+1:]
|
||||||
|
logicName := "logic"
|
||||||
|
if handlerPath != handlerDir {
|
||||||
handler = strings.Title(handler)
|
handler = strings.Title(handler)
|
||||||
|
logicName = pkgName
|
||||||
|
}
|
||||||
|
parentPkg, err := getParentPackage(dir)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
goctlVersion := version.GetGoctlVersion()
|
goctlVersion := version.GetGoctlVersion()
|
||||||
// todo(anqiansong): This will be removed after a certain number of production versions of goctl (probably 5)
|
// todo(anqiansong): This will be removed after a certain number of production versions of goctl (probably 5)
|
||||||
after1_1_10 := version.IsVersionGreaterThan(goctlVersion, "1.1.10")
|
after1_1_10 := version.IsVersionGreaterThan(goctlVersion, "1.1.10")
|
||||||
return doGenToFile(dir, handler, cfg, group, route, handlerInfo{
|
return doGenToFile(dir, handler, cfg, group, route, handlerInfo{
|
||||||
ImportPackages: genHandlerImports(group, route, rootPkg),
|
PkgName: pkgName,
|
||||||
|
ImportPackages: genHandlerImports(group, route, parentPkg),
|
||||||
HandlerName: handler,
|
HandlerName: handler,
|
||||||
After1_1_10: after1_1_10,
|
After1_1_10: after1_1_10,
|
||||||
RequestType: util.Title(route.RequestTypeName()),
|
RequestType: util.Title(route.RequestTypeName()),
|
||||||
|
LogicName: logicName,
|
||||||
LogicType: strings.Title(getLogicName(route)),
|
LogicType: strings.Title(getLogicName(route)),
|
||||||
Call: strings.Title(strings.TrimSuffix(handler, "Handler")),
|
Call: strings.Title(strings.TrimSuffix(handler, "Handler")),
|
||||||
HasResp: len(route.ResponseTypeName()) > 0,
|
HasResp: len(route.ResponseTypeName()) > 0,
|
||||||
|
@ -12,7 +12,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
||||||
)
|
)
|
||||||
|
|
||||||
const logicTemplate = `package logic
|
const logicTemplate = `package {{.pkgName}}
|
||||||
|
|
||||||
import (
|
import (
|
||||||
{{.imports}}
|
{{.imports}}
|
||||||
@ -78,15 +78,17 @@ func genLogicByRoute(dir, rootPkg string, cfg *config.Config, group spec.Group,
|
|||||||
requestString = "req " + requestGoTypeName(route, typesPacket)
|
requestString = "req " + requestGoTypeName(route, typesPacket)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
subDir := getLogicFolderPath(group, route)
|
||||||
return genFile(fileGenConfig{
|
return genFile(fileGenConfig{
|
||||||
dir: dir,
|
dir: dir,
|
||||||
subdir: getLogicFolderPath(group, route),
|
subdir: subDir,
|
||||||
filename: goFile + ".go",
|
filename: goFile + ".go",
|
||||||
templateName: "logicTemplate",
|
templateName: "logicTemplate",
|
||||||
category: category,
|
category: category,
|
||||||
templateFile: logicTemplateFile,
|
templateFile: logicTemplateFile,
|
||||||
builtinTemplate: logicTemplate,
|
builtinTemplate: logicTemplate,
|
||||||
data: map[string]string{
|
data: map[string]string{
|
||||||
|
"pkgName": subDir[strings.LastIndex(subDir, "/")+1:],
|
||||||
"imports": imports,
|
"imports": imports,
|
||||||
"logic": strings.Title(logic),
|
"logic": strings.Title(logic),
|
||||||
"function": strings.Title(strings.TrimSuffix(logic, "Logic")),
|
"function": strings.Title(strings.TrimSuffix(logic, "Logic")),
|
||||||
|
Loading…
Reference in New Issue
Block a user