From 546fcd8bab3257edf5c7147d7853538ccc25e804 Mon Sep 17 00:00:00 2001 From: kingxt Date: Sat, 29 May 2021 23:01:02 +0800 Subject: [PATCH] fix #736 (#738) * optimize performance * rename * rename * revert --- tools/goctl/api/gogen/gen.go | 15 ++++++++++----- tools/goctl/api/gogen/genhandlers.go | 12 ++++-------- tools/goctl/api/gogen/genlogic.go | 13 ++++--------- tools/goctl/api/gogen/genmain.go | 9 ++------- tools/goctl/api/gogen/genroutes.go | 9 ++------- tools/goctl/api/gogen/gensvc.go | 11 +++-------- 6 files changed, 25 insertions(+), 44 deletions(-) diff --git a/tools/goctl/api/gogen/gen.go b/tools/goctl/api/gogen/gen.go index 025d0007..07f8ffd8 100644 --- a/tools/goctl/api/gogen/gen.go +++ b/tools/goctl/api/gogen/gen.go @@ -54,14 +54,19 @@ func DoGenProject(apiFile, dir, style string) error { } logx.Must(util.MkdirIfNotExist(dir)) + rootPkg, err := getParentPackage(dir) + if err != nil { + return err + } + logx.Must(genEtc(dir, cfg, api)) logx.Must(genConfig(dir, cfg, api)) - logx.Must(genMain(dir, cfg, api)) - logx.Must(genServiceContext(dir, cfg, api)) + logx.Must(genMain(dir, rootPkg, cfg, api)) + logx.Must(genServiceContext(dir, rootPkg, cfg, api)) logx.Must(genTypes(dir, cfg, api)) - logx.Must(genRoutes(dir, cfg, api)) - logx.Must(genHandlers(dir, cfg, api)) - logx.Must(genLogic(dir, cfg, api)) + logx.Must(genRoutes(dir, rootPkg, cfg, api)) + logx.Must(genHandlers(dir, rootPkg, cfg, api)) + logx.Must(genLogic(dir, rootPkg, cfg, api)) logx.Must(genMiddleware(dir, cfg, api)) if err := backupAndSweep(apiFile); err != nil { diff --git a/tools/goctl/api/gogen/genhandlers.go b/tools/goctl/api/gogen/genhandlers.go index d82dc14c..4dd17f24 100644 --- a/tools/goctl/api/gogen/genhandlers.go +++ b/tools/goctl/api/gogen/genhandlers.go @@ -49,18 +49,14 @@ type handlerInfo struct { HasRequest bool } -func genHandler(dir 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) if getHandlerFolderPath(group, route) != handlerDir { handler = strings.Title(handler) } - parentPkg, err := getParentPackage(dir) - if err != nil { - return err - } return doGenToFile(dir, handler, cfg, group, route, handlerInfo{ - ImportPackages: genHandlerImports(group, route, parentPkg), + ImportPackages: genHandlerImports(group, route, rootPkg), HandlerName: handler, RequestType: util.Title(route.RequestTypeName()), LogicType: strings.Title(getLogicName(route)), @@ -89,10 +85,10 @@ func doGenToFile(dir, handler string, cfg *config.Config, group spec.Group, }) } -func genHandlers(dir string, cfg *config.Config, api *spec.ApiSpec) error { +func genHandlers(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error { for _, group := range api.Service.Groups { for _, route := range group.Routes { - if err := genHandler(dir, cfg, group, route); err != nil { + if err := genHandler(dir, rootPkg, cfg, group, route); err != nil { return err } } diff --git a/tools/goctl/api/gogen/genlogic.go b/tools/goctl/api/gogen/genlogic.go index 09a0c543..267f32c5 100644 --- a/tools/goctl/api/gogen/genlogic.go +++ b/tools/goctl/api/gogen/genlogic.go @@ -39,10 +39,10 @@ func (l *{{.logic}}) {{.function}}({{.request}}) {{.responseType}} { } ` -func genLogic(dir string, cfg *config.Config, api *spec.ApiSpec) error { +func genLogic(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error { for _, g := range api.Service.Groups { for _, r := range g.Routes { - err := genLogicByRoute(dir, cfg, g, r) + err := genLogicByRoute(dir, rootPkg, cfg, g, r) if err != nil { return err } @@ -51,19 +51,14 @@ func genLogic(dir string, cfg *config.Config, api *spec.ApiSpec) error { return nil } -func genLogicByRoute(dir string, cfg *config.Config, group spec.Group, route spec.Route) error { +func genLogicByRoute(dir, rootPkg string, cfg *config.Config, group spec.Group, route spec.Route) error { logic := getLogicName(route) goFile, err := format.FileNamingFormat(cfg.NamingFormat, logic) if err != nil { return err } - parentPkg, err := getParentPackage(dir) - if err != nil { - return err - } - - imports := genLogicImports(route, parentPkg) + imports := genLogicImports(route, rootPkg) var responseString string var returnString string var requestString string diff --git a/tools/goctl/api/gogen/genmain.go b/tools/goctl/api/gogen/genmain.go index 269c25a3..1af504c4 100644 --- a/tools/goctl/api/gogen/genmain.go +++ b/tools/goctl/api/gogen/genmain.go @@ -39,7 +39,7 @@ func main() { } ` -func genMain(dir string, cfg *config.Config, api *spec.ApiSpec) error { +func genMain(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error { name := strings.ToLower(api.Service.Name) if strings.HasSuffix(name, "-api") { name = strings.ReplaceAll(name, "-api", "") @@ -49,11 +49,6 @@ func genMain(dir string, cfg *config.Config, api *spec.ApiSpec) error { return err } - parentPkg, err := getParentPackage(dir) - if err != nil { - return err - } - return genFile(fileGenConfig{ dir: dir, subdir: "", @@ -63,7 +58,7 @@ func genMain(dir string, cfg *config.Config, api *spec.ApiSpec) error { templateFile: mainTemplateFile, builtinTemplate: mainTemplate, data: map[string]string{ - "importPackages": genMainImports(parentPkg), + "importPackages": genMainImports(rootPkg), "serviceName": api.Service.Name, }, }) diff --git a/tools/goctl/api/gogen/genroutes.go b/tools/goctl/api/gogen/genroutes.go index 98f89672..3ce3ec0d 100644 --- a/tools/goctl/api/gogen/genroutes.go +++ b/tools/goctl/api/gogen/genroutes.go @@ -62,7 +62,7 @@ type ( } ) -func genRoutes(dir string, cfg *config.Config, api *spec.ApiSpec) error { +func genRoutes(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error { var builder strings.Builder groups, err := getRoutes(api) if err != nil { @@ -116,11 +116,6 @@ func genRoutes(dir string, cfg *config.Config, api *spec.ApiSpec) error { } } - parentPkg, err := getParentPackage(dir) - if err != nil { - return err - } - routeFilename, err := format.FileNamingFormat(cfg.NamingFormat, routesFilename) if err != nil { return err @@ -139,7 +134,7 @@ func genRoutes(dir string, cfg *config.Config, api *spec.ApiSpec) error { templateFile: "", builtinTemplate: routesTemplate, data: map[string]string{ - "importPackages": genRouteImports(parentPkg, api), + "importPackages": genRouteImports(rootPkg, api), "routesAdditions": strings.TrimSpace(builder.String()), }, }) diff --git a/tools/goctl/api/gogen/gensvc.go b/tools/goctl/api/gogen/gensvc.go index 1d0c893c..2686596c 100644 --- a/tools/goctl/api/gogen/gensvc.go +++ b/tools/goctl/api/gogen/gensvc.go @@ -33,7 +33,7 @@ func NewServiceContext(c {{.config}}) *ServiceContext { ` ) -func genServiceContext(dir string, cfg *config.Config, api *spec.ApiSpec) error { +func genServiceContext(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error { filename, err := format.FileNamingFormat(cfg.NamingFormat, contextFilename) if err != nil { return err @@ -45,11 +45,6 @@ func genServiceContext(dir string, cfg *config.Config, api *spec.ApiSpec) error auths = append(auths, fmt.Sprintf("%s config.AuthConfig", item)) } - parentPkg, err := getParentPackage(dir) - if err != nil { - return err - } - var middlewareStr string var middlewareAssignment string middlewares := getMiddleware(api) @@ -61,9 +56,9 @@ func genServiceContext(dir string, cfg *config.Config, api *spec.ApiSpec) error fmt.Sprintf("middleware.New%s().%s", strings.Title(name), "Handle")) } - configImport := "\"" + ctlutil.JoinPackages(parentPkg, configDir) + "\"" + configImport := "\"" + ctlutil.JoinPackages(rootPkg, configDir) + "\"" if len(middlewareStr) > 0 { - configImport += "\n\t\"" + ctlutil.JoinPackages(parentPkg, middlewareDir) + "\"" + configImport += "\n\t\"" + ctlutil.JoinPackages(rootPkg, middlewareDir) + "\"" configImport += fmt.Sprintf("\n\t\"%s/rest\"", vars.ProjectOpenSourceURL) }