From a1125db503f671e952c6462d19cbd0b40c879564 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=9C=86=E5=BB=BA?= <624508914@qq.com> Date: Fri, 15 Jul 2022 10:45:31 +0800 Subject: [PATCH] fix: goctl genhandler duplicate rest/httpx & goctl genhandler template support custom import httpx package --- tools/goctl/api/gogen/genhandlers.go | 39 ++++++++++++++-------------- tools/goctl/api/gogen/handler.tpl | 2 +- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/tools/goctl/api/gogen/genhandlers.go b/tools/goctl/api/gogen/genhandlers.go index 5b2862284133..560ffe946aa1 100644 --- a/tools/goctl/api/gogen/genhandlers.go +++ b/tools/goctl/api/gogen/genhandlers.go @@ -21,15 +21,16 @@ const defaultLogicPackage = "logic" var handlerTemplate string type handlerInfo struct { - PkgName string - ImportPackages string - HandlerName string - RequestType string - LogicName string - LogicType string - Call string - HasResp bool - HasRequest bool + PkgName string + ImportPackages string + ImportHttpxPackage string + HandlerName string + RequestType string + LogicName string + LogicType string + Call string + HasResp bool + HasRequest bool } 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{ - PkgName: pkgName, - ImportPackages: genHandlerImports(group, route, parentPkg), - HandlerName: handler, - RequestType: util.Title(route.RequestTypeName()), - LogicName: logicName, - LogicType: strings.Title(getLogicName(route)), - Call: strings.Title(strings.TrimSuffix(handler, "Handler")), - HasResp: len(route.ResponseTypeName()) > 0, - HasRequest: len(route.RequestTypeName()) > 0, + PkgName: pkgName, + ImportPackages: genHandlerImports(group, route, parentPkg), + ImportHttpxPackage: fmt.Sprintf("\"%s/rest/httpx\"", vars.ProjectOpenSourceURL), + HandlerName: handler, + RequestType: util.Title(route.RequestTypeName()), + LogicName: logicName, + LogicType: strings.Title(getLogicName(route)), + Call: strings.Title(strings.TrimSuffix(handler, "Handler")), + 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 { 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") } diff --git a/tools/goctl/api/gogen/handler.tpl b/tools/goctl/api/gogen/handler.tpl index 033dc22ed518..3a9f9bac384a 100644 --- a/tools/goctl/api/gogen/handler.tpl +++ b/tools/goctl/api/gogen/handler.tpl @@ -3,8 +3,8 @@ package {{.PkgName}} import ( "net/http" - "github.com/zeromicro/go-zero/rest/httpx" {{.ImportPackages}} + {{.ImportHttpxPackage}} ) func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {