Skip to content

Commit

Permalink
fix #736 (#738)
Browse files Browse the repository at this point in the history
* optimize performance

* rename

* rename

* revert
  • Loading branch information
kingxt authored May 29, 2021
1 parent 3022f93 commit 546fcd8
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 44 deletions.
15 changes: 10 additions & 5 deletions tools/goctl/api/gogen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 4 additions & 8 deletions tools/goctl/api/gogen/genhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -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
}
}
Expand Down
13 changes: 4 additions & 9 deletions tools/goctl/api/gogen/genlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
9 changes: 2 additions & 7 deletions tools/goctl/api/gogen/genmain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", "")
Expand All @@ -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: "",
Expand All @@ -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,
},
})
Expand Down
9 changes: 2 additions & 7 deletions tools/goctl/api/gogen/genroutes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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()),
},
})
Expand Down
11 changes: 3 additions & 8 deletions tools/goctl/api/gogen/gensvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
}

Expand Down

0 comments on commit 546fcd8

Please sign in to comment.