* optimize performance

* rename

* rename

* revert
master
kingxt 4 years ago committed by GitHub
parent 3022f93b6d
commit 546fcd8bab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -54,14 +54,19 @@ func DoGenProject(apiFile, dir, style string) error {
} }
logx.Must(util.MkdirIfNotExist(dir)) logx.Must(util.MkdirIfNotExist(dir))
rootPkg, err := getParentPackage(dir)
if err != nil {
return err
}
logx.Must(genEtc(dir, cfg, api)) logx.Must(genEtc(dir, cfg, api))
logx.Must(genConfig(dir, cfg, api)) logx.Must(genConfig(dir, cfg, api))
logx.Must(genMain(dir, cfg, api)) logx.Must(genMain(dir, rootPkg, cfg, api))
logx.Must(genServiceContext(dir, cfg, api)) logx.Must(genServiceContext(dir, rootPkg, cfg, api))
logx.Must(genTypes(dir, cfg, api)) logx.Must(genTypes(dir, cfg, api))
logx.Must(genRoutes(dir, cfg, api)) logx.Must(genRoutes(dir, rootPkg, cfg, api))
logx.Must(genHandlers(dir, cfg, api)) logx.Must(genHandlers(dir, rootPkg, cfg, api))
logx.Must(genLogic(dir, cfg, api)) logx.Must(genLogic(dir, rootPkg, cfg, api))
logx.Must(genMiddleware(dir, cfg, api)) logx.Must(genMiddleware(dir, cfg, api))
if err := backupAndSweep(apiFile); err != nil { if err := backupAndSweep(apiFile); err != nil {

@ -49,18 +49,14 @@ type handlerInfo struct {
HasRequest bool 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) handler := getHandlerName(route)
if getHandlerFolderPath(group, route) != handlerDir { if getHandlerFolderPath(group, route) != handlerDir {
handler = strings.Title(handler) handler = strings.Title(handler)
} }
parentPkg, err := getParentPackage(dir)
if err != nil {
return err
}
return doGenToFile(dir, handler, cfg, group, route, handlerInfo{ return doGenToFile(dir, handler, cfg, group, route, handlerInfo{
ImportPackages: genHandlerImports(group, route, parentPkg), ImportPackages: genHandlerImports(group, route, rootPkg),
HandlerName: handler, HandlerName: handler,
RequestType: util.Title(route.RequestTypeName()), RequestType: util.Title(route.RequestTypeName()),
LogicType: strings.Title(getLogicName(route)), 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 _, group := range api.Service.Groups {
for _, route := range group.Routes { 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 return err
} }
} }

@ -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 _, g := range api.Service.Groups {
for _, r := range g.Routes { for _, r := range g.Routes {
err := genLogicByRoute(dir, cfg, g, r) err := genLogicByRoute(dir, rootPkg, cfg, g, r)
if err != nil { if err != nil {
return err return err
} }
@ -51,19 +51,14 @@ func genLogic(dir string, cfg *config.Config, api *spec.ApiSpec) error {
return nil 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) logic := getLogicName(route)
goFile, err := format.FileNamingFormat(cfg.NamingFormat, logic) goFile, err := format.FileNamingFormat(cfg.NamingFormat, logic)
if err != nil { if err != nil {
return err return err
} }
parentPkg, err := getParentPackage(dir) imports := genLogicImports(route, rootPkg)
if err != nil {
return err
}
imports := genLogicImports(route, parentPkg)
var responseString string var responseString string
var returnString string var returnString string
var requestString string var requestString string

@ -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) name := strings.ToLower(api.Service.Name)
if strings.HasSuffix(name, "-api") { if strings.HasSuffix(name, "-api") {
name = strings.ReplaceAll(name, "-api", "") name = strings.ReplaceAll(name, "-api", "")
@ -49,11 +49,6 @@ func genMain(dir string, cfg *config.Config, api *spec.ApiSpec) error {
return err return err
} }
parentPkg, err := getParentPackage(dir)
if err != nil {
return err
}
return genFile(fileGenConfig{ return genFile(fileGenConfig{
dir: dir, dir: dir,
subdir: "", subdir: "",
@ -63,7 +58,7 @@ func genMain(dir string, cfg *config.Config, api *spec.ApiSpec) error {
templateFile: mainTemplateFile, templateFile: mainTemplateFile,
builtinTemplate: mainTemplate, builtinTemplate: mainTemplate,
data: map[string]string{ data: map[string]string{
"importPackages": genMainImports(parentPkg), "importPackages": genMainImports(rootPkg),
"serviceName": api.Service.Name, "serviceName": api.Service.Name,
}, },
}) })

@ -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 var builder strings.Builder
groups, err := getRoutes(api) groups, err := getRoutes(api)
if err != nil { 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) routeFilename, err := format.FileNamingFormat(cfg.NamingFormat, routesFilename)
if err != nil { if err != nil {
return err return err
@ -139,7 +134,7 @@ func genRoutes(dir string, cfg *config.Config, api *spec.ApiSpec) error {
templateFile: "", templateFile: "",
builtinTemplate: routesTemplate, builtinTemplate: routesTemplate,
data: map[string]string{ data: map[string]string{
"importPackages": genRouteImports(parentPkg, api), "importPackages": genRouteImports(rootPkg, api),
"routesAdditions": strings.TrimSpace(builder.String()), "routesAdditions": strings.TrimSpace(builder.String()),
}, },
}) })

@ -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) filename, err := format.FileNamingFormat(cfg.NamingFormat, contextFilename)
if err != nil { if err != nil {
return err 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)) auths = append(auths, fmt.Sprintf("%s config.AuthConfig", item))
} }
parentPkg, err := getParentPackage(dir)
if err != nil {
return err
}
var middlewareStr string var middlewareStr string
var middlewareAssignment string var middlewareAssignment string
middlewares := getMiddleware(api) 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")) fmt.Sprintf("middleware.New%s().%s", strings.Title(name), "Handle"))
} }
configImport := "\"" + ctlutil.JoinPackages(parentPkg, configDir) + "\"" configImport := "\"" + ctlutil.JoinPackages(rootPkg, configDir) + "\""
if len(middlewareStr) > 0 { 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) configImport += fmt.Sprintf("\n\t\"%s/rest\"", vars.ProjectOpenSourceURL)
} }

Loading…
Cancel
Save