fix: goctl genhandler duplicate rest/httpx & goctl genhandler template support custom import httpx package (#2152)
This commit is contained in:
@@ -21,15 +21,16 @@ const defaultLogicPackage = "logic"
|
|||||||
var handlerTemplate string
|
var handlerTemplate string
|
||||||
|
|
||||||
type handlerInfo struct {
|
type handlerInfo struct {
|
||||||
PkgName string
|
PkgName string
|
||||||
ImportPackages string
|
ImportPackages string
|
||||||
HandlerName string
|
ImportHttpxPackage string
|
||||||
RequestType string
|
HandlerName string
|
||||||
LogicName string
|
RequestType string
|
||||||
LogicType string
|
LogicName string
|
||||||
Call string
|
LogicType string
|
||||||
HasResp bool
|
Call string
|
||||||
HasRequest bool
|
HasResp bool
|
||||||
|
HasRequest bool
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
@@ -47,15 +48,16 @@ func genHandler(dir, rootPkg string, cfg *config.Config, group spec.Group, route
|
|||||||
}
|
}
|
||||||
|
|
||||||
return doGenToFile(dir, handler, cfg, group, route, handlerInfo{
|
return doGenToFile(dir, handler, cfg, group, route, handlerInfo{
|
||||||
PkgName: pkgName,
|
PkgName: pkgName,
|
||||||
ImportPackages: genHandlerImports(group, route, parentPkg),
|
ImportPackages: genHandlerImports(group, route, parentPkg),
|
||||||
HandlerName: handler,
|
ImportHttpxPackage: fmt.Sprintf("\"%s/rest/httpx\"", vars.ProjectOpenSourceURL),
|
||||||
RequestType: util.Title(route.RequestTypeName()),
|
HandlerName: handler,
|
||||||
LogicName: logicName,
|
RequestType: util.Title(route.RequestTypeName()),
|
||||||
LogicType: strings.Title(getLogicName(route)),
|
LogicName: logicName,
|
||||||
Call: strings.Title(strings.TrimSuffix(handler, "Handler")),
|
LogicType: strings.Title(getLogicName(route)),
|
||||||
HasResp: len(route.ResponseTypeName()) > 0,
|
Call: strings.Title(strings.TrimSuffix(handler, "Handler")),
|
||||||
HasRequest: len(route.RequestTypeName()) > 0,
|
HasResp: len(route.ResponseTypeName()) > 0,
|
||||||
|
HasRequest: len(route.RequestTypeName()) > 0,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,7 +101,6 @@ func genHandlerImports(group spec.Group, route spec.Route, parentPkg string) str
|
|||||||
if len(route.RequestTypeName()) > 0 {
|
if len(route.RequestTypeName()) > 0 {
|
||||||
imports = append(imports, fmt.Sprintf("\"%s\"\n", pathx.JoinPackages(parentPkg, typesDir)))
|
imports = append(imports, fmt.Sprintf("\"%s\"\n", pathx.JoinPackages(parentPkg, typesDir)))
|
||||||
}
|
}
|
||||||
imports = append(imports, fmt.Sprintf("\"%s/rest/httpx\"", vars.ProjectOpenSourceURL))
|
|
||||||
|
|
||||||
return strings.Join(imports, "\n\t")
|
return strings.Join(imports, "\n\t")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ package {{.PkgName}}
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/rest/httpx"
|
|
||||||
{{.ImportPackages}}
|
{{.ImportPackages}}
|
||||||
|
{{.ImportHttpxPackage}}
|
||||||
)
|
)
|
||||||
|
|
||||||
func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||||
|
|||||||
Reference in New Issue
Block a user