From a987d12237cafd7a9e0f1286a8605f1a4175b27e Mon Sep 17 00:00:00 2001 From: kevin Date: Thu, 27 Aug 2020 14:40:05 +0800 Subject: [PATCH] sort imports on api generation --- tools/goctl/api/gogen/genconfig.go | 4 +--- tools/goctl/api/gogen/genhandlers.go | 10 ++++------ tools/goctl/api/gogen/genlogic.go | 6 +++--- tools/goctl/api/gogen/genmain.go | 11 ++++------- tools/goctl/api/gogen/genroutes.go | 5 +++-- 5 files changed, 15 insertions(+), 21 deletions(-) diff --git a/tools/goctl/api/gogen/genconfig.go b/tools/goctl/api/gogen/genconfig.go index a83605b0..5beb19ab 100644 --- a/tools/goctl/api/gogen/genconfig.go +++ b/tools/goctl/api/gogen/genconfig.go @@ -13,9 +13,7 @@ const ( configFile = "config.go" configTemplate = `package config -import ( - {{.authImport}} -) +import {{.authImport}} type Config struct { rest.RestConf diff --git a/tools/goctl/api/gogen/genhandlers.go b/tools/goctl/api/gogen/genhandlers.go index e4879759..c6e02e4f 100644 --- a/tools/goctl/api/gogen/genhandlers.go +++ b/tools/goctl/api/gogen/genhandlers.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "path" - "sort" "strings" "text/template" @@ -162,14 +161,13 @@ func genHandlers(dir string, api *spec.ApiSpec) error { func genHandlerImports(group spec.Group, route spec.Route, parentPkg string) string { var imports []string - imports = append(imports, fmt.Sprintf("\"%s/rest/httpx\"", vars.ProjectOpenSourceUrl)) + imports = append(imports, fmt.Sprintf("\"%s\"", + util.JoinPackages(parentPkg, getLogicFolderPath(group, route)))) imports = append(imports, fmt.Sprintf("\"%s\"", util.JoinPackages(parentPkg, contextDir))) if len(route.RequestType.Name) > 0 || len(route.ResponseType.Name) > 0 { - imports = append(imports, fmt.Sprintf("\"%s\"", util.JoinPackages(parentPkg, typesDir))) + imports = append(imports, fmt.Sprintf("\"%s\"\n", util.JoinPackages(parentPkg, typesDir))) } - imports = append(imports, fmt.Sprintf("\"%s\"", - util.JoinPackages(parentPkg, getLogicFolderPath(group, route)))) - sort.Strings(imports) + imports = append(imports, fmt.Sprintf("\"%s/rest/httpx\"", vars.ProjectOpenSourceUrl)) return strings.Join(imports, "\n\t") } diff --git a/tools/goctl/api/gogen/genlogic.go b/tools/goctl/api/gogen/genlogic.go index c8c62e87..49da400f 100644 --- a/tools/goctl/api/gogen/genlogic.go +++ b/tools/goctl/api/gogen/genlogic.go @@ -121,10 +121,10 @@ func getLogicFolderPath(group spec.Group, route spec.Route) string { func genLogicImports(route spec.Route, parentPkg string) string { var imports []string imports = append(imports, `"context"`+"\n") - imports = append(imports, fmt.Sprintf("\"%s/core/logx\"", vars.ProjectOpenSourceUrl)) + imports = append(imports, fmt.Sprintf("\"%s\"", ctlutil.JoinPackages(parentPkg, contextDir))) if len(route.ResponseType.Name) > 0 || len(route.RequestType.Name) > 0 { - imports = append(imports, fmt.Sprintf("\"%s\"", ctlutil.JoinPackages(parentPkg, typesDir))) + imports = append(imports, fmt.Sprintf("\"%s\"\n", ctlutil.JoinPackages(parentPkg, typesDir))) } - imports = append(imports, fmt.Sprintf("\"%s\"", ctlutil.JoinPackages(parentPkg, contextDir))) + imports = append(imports, fmt.Sprintf("\"%s/core/logx\"", vars.ProjectOpenSourceUrl)) return strings.Join(imports, "\n\t") } diff --git a/tools/goctl/api/gogen/genmain.go b/tools/goctl/api/gogen/genmain.go index 9d08b7f0..4562e682 100644 --- a/tools/goctl/api/gogen/genmain.go +++ b/tools/goctl/api/gogen/genmain.go @@ -3,7 +3,6 @@ package gogen import ( "bytes" "fmt" - "sort" "strings" "text/template" @@ -73,13 +72,11 @@ func genMain(dir string, api *spec.ApiSpec) error { } func genMainImports(parentPkg string) string { - imports := []string{ - fmt.Sprintf("\"%s/core/conf\"", vars.ProjectOpenSourceUrl), - fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceUrl), - } + var imports []string imports = append(imports, fmt.Sprintf("\"%s\"", ctlutil.JoinPackages(parentPkg, configDir))) imports = append(imports, fmt.Sprintf("\"%s\"", ctlutil.JoinPackages(parentPkg, handlerDir))) - imports = append(imports, fmt.Sprintf("\"%s\"", ctlutil.JoinPackages(parentPkg, contextDir))) - sort.Strings(imports) + imports = append(imports, fmt.Sprintf("\"%s\"\n", ctlutil.JoinPackages(parentPkg, contextDir))) + imports = append(imports, fmt.Sprintf("\"%s/core/conf\"", vars.ProjectOpenSourceUrl)) + imports = append(imports, fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceUrl)) return strings.Join(imports, "\n\t") } diff --git a/tools/goctl/api/gogen/genroutes.go b/tools/goctl/api/gogen/genroutes.go index a643dc4f..e431f3d1 100644 --- a/tools/goctl/api/gogen/genroutes.go +++ b/tools/goctl/api/gogen/genroutes.go @@ -131,7 +131,6 @@ func genRoutes(dir string, api *spec.ApiSpec) error { func genRouteImports(parentPkg string, api *spec.ApiSpec) string { var importSet = collection.NewSet() - importSet.AddStr(fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceUrl)) importSet.AddStr(fmt.Sprintf("\"%s\"", util.JoinPackages(parentPkg, contextDir))) for _, group := range api.Service.Groups { for _, route := range group.Routes { @@ -148,7 +147,9 @@ func genRouteImports(parentPkg string, api *spec.ApiSpec) string { } imports := importSet.KeysStr() sort.Strings(imports) - return strings.Join(imports, "\n\t") + projectSection := strings.Join(imports, "\n\t") + depSection := fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceUrl) + return fmt.Sprintf("%s\n\n\t%s", projectSection, depSection) } func getRoutes(api *spec.ApiSpec) ([]group, error) {