sort imports on api generation

master
kevin 4 years ago
parent 33208e6ef6
commit a987d12237

@ -13,9 +13,7 @@ const (
configFile = "config.go" configFile = "config.go"
configTemplate = `package config configTemplate = `package config
import ( import {{.authImport}}
{{.authImport}}
)
type Config struct { type Config struct {
rest.RestConf rest.RestConf

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"path" "path"
"sort"
"strings" "strings"
"text/template" "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 { func genHandlerImports(group spec.Group, route spec.Route, parentPkg string) string {
var imports []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))) imports = append(imports, fmt.Sprintf("\"%s\"", util.JoinPackages(parentPkg, contextDir)))
if len(route.RequestType.Name) > 0 || len(route.ResponseType.Name) > 0 { 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\"", imports = append(imports, fmt.Sprintf("\"%s/rest/httpx\"", vars.ProjectOpenSourceUrl))
util.JoinPackages(parentPkg, getLogicFolderPath(group, route))))
sort.Strings(imports)
return strings.Join(imports, "\n\t") return strings.Join(imports, "\n\t")
} }

@ -121,10 +121,10 @@ func getLogicFolderPath(group spec.Group, route spec.Route) string {
func genLogicImports(route spec.Route, parentPkg string) string { func genLogicImports(route spec.Route, parentPkg string) string {
var imports []string var imports []string
imports = append(imports, `"context"`+"\n") 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 { 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") return strings.Join(imports, "\n\t")
} }

@ -3,7 +3,6 @@ package gogen
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"sort"
"strings" "strings"
"text/template" "text/template"
@ -73,13 +72,11 @@ func genMain(dir string, api *spec.ApiSpec) error {
} }
func genMainImports(parentPkg string) string { func genMainImports(parentPkg string) string {
imports := []string{ var imports []string
fmt.Sprintf("\"%s/core/conf\"", vars.ProjectOpenSourceUrl),
fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceUrl),
}
imports = append(imports, fmt.Sprintf("\"%s\"", ctlutil.JoinPackages(parentPkg, configDir))) 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, handlerDir)))
imports = append(imports, fmt.Sprintf("\"%s\"", ctlutil.JoinPackages(parentPkg, contextDir))) imports = append(imports, fmt.Sprintf("\"%s\"\n", ctlutil.JoinPackages(parentPkg, contextDir)))
sort.Strings(imports) 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") return strings.Join(imports, "\n\t")
} }

@ -131,7 +131,6 @@ func genRoutes(dir string, api *spec.ApiSpec) error {
func genRouteImports(parentPkg string, api *spec.ApiSpec) string { func genRouteImports(parentPkg string, api *spec.ApiSpec) string {
var importSet = collection.NewSet() var importSet = collection.NewSet()
importSet.AddStr(fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceUrl))
importSet.AddStr(fmt.Sprintf("\"%s\"", util.JoinPackages(parentPkg, contextDir))) importSet.AddStr(fmt.Sprintf("\"%s\"", util.JoinPackages(parentPkg, contextDir)))
for _, group := range api.Service.Groups { for _, group := range api.Service.Groups {
for _, route := range group.Routes { for _, route := range group.Routes {
@ -148,7 +147,9 @@ func genRouteImports(parentPkg string, api *spec.ApiSpec) string {
} }
imports := importSet.KeysStr() imports := importSet.KeysStr()
sort.Strings(imports) 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) { func getRoutes(api *spec.ApiSpec) ([]group, error) {

Loading…
Cancel
Save