Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prefix for cache keys. #3702

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tools/goctl/api/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func init() {

goCmdFlags.StringVar(&gogen.VarStringDir, "dir")
goCmdFlags.StringVar(&gogen.VarStringAPI, "api")
goCmdFlags.BoolVarP(&gogen.VarWithoutSuffix, "without-suffix", "w")
goCmdFlags.StringVar(&gogen.VarStringHome, "home")
goCmdFlags.StringVar(&gogen.VarStringRemote, "remote")
goCmdFlags.StringVar(&gogen.VarStringBranch, "branch")
Expand All @@ -84,6 +85,7 @@ func init() {
newCmdFlags.StringVar(&new.VarStringHome, "home")
newCmdFlags.StringVar(&new.VarStringRemote, "remote")
newCmdFlags.StringVar(&new.VarStringBranch, "branch")
newCmdFlags.BoolVarP(&gogen.VarWithoutSuffix, "without-suffix", "w")
newCmdFlags.StringVarWithDefaultValue(&new.VarStringStyle, "style", config.DefaultFormat)

pluginCmdFlags.StringVarP(&plugin.VarStringPlugin, "plugin", "p")
Expand Down
16 changes: 9 additions & 7 deletions tools/goctl/api/gogen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ var (
VarStringDir string
// VarStringAPI describes the API.
VarStringAPI string
// VarWithoutSuffix remove logic etc...
VarWithoutSuffix bool
// VarStringHome describes the go home.
VarStringHome string
// VarStringRemote describes the remote git repository.
Expand All @@ -45,6 +47,7 @@ var (
func GoCommand(_ *cobra.Command, _ []string) error {
apiFile := VarStringAPI
dir := VarStringDir
withoutSuffix := VarWithoutSuffix
namingStyle := VarStringStyle
home := VarStringHome
remote := VarStringRemote
Expand All @@ -65,12 +68,11 @@ func GoCommand(_ *cobra.Command, _ []string) error {
if len(dir) == 0 {
return errors.New("missing -dir")
}

return DoGenProject(apiFile, dir, namingStyle)
return DoGenProject(apiFile, dir, namingStyle, withoutSuffix)
}

// DoGenProject gen go project files with api file
func DoGenProject(apiFile, dir, style string) error {
func DoGenProject(apiFile, dir, style string, withoutSuffix bool) error {
api, err := parser.Parse(apiFile)
if err != nil {
return err
Expand All @@ -96,10 +98,10 @@ func DoGenProject(apiFile, dir, style string) error {
logx.Must(genMain(dir, rootPkg, cfg, api))
logx.Must(genServiceContext(dir, rootPkg, cfg, api))
logx.Must(genTypes(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))
logx.Must(genRoutes(dir, rootPkg, cfg, api, withoutSuffix))
logx.Must(genHandlers(dir, rootPkg, cfg, api, withoutSuffix))
logx.Must(genLogic(dir, rootPkg, cfg, api, withoutSuffix))
logx.Must(genMiddleware(dir, cfg, api, withoutSuffix))

if err := backupAndSweep(apiFile); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/api/gogen/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func validateWithCamel(t *testing.T, api, camel string) {
assert.Nil(t, err)
err = initMod(dir)
assert.Nil(t, err)
err = DoGenProject(api, dir, camel)
err = DoGenProject(api, dir, camel, false)
assert.Nil(t, err)
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, ".go") {
Expand Down
26 changes: 15 additions & 11 deletions tools/goctl/api/gogen/genhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ type handlerInfo struct {
HasRequest bool
}

func genHandler(dir, rootPkg string, cfg *config.Config, group spec.Group, route spec.Route) error {
handler := getHandlerName(route)
func genHandler(dir, rootPkg string, cfg *config.Config, group spec.Group, route spec.Route, withoutSuffix bool) error {
handler := getHandlerName(route, withoutSuffix)
handlerPath := getHandlerFolderPath(group, route)
pkgName := handlerPath[strings.LastIndex(handlerPath, "/")+1:]
logicName := defaultLogicPackage
Expand All @@ -47,7 +47,7 @@ func genHandler(dir, rootPkg string, cfg *config.Config, group spec.Group, route
HandlerName: handler,
RequestType: util.Title(route.RequestTypeName()),
LogicName: logicName,
LogicType: strings.Title(getLogicName(route)),
LogicType: strings.Title(getLogicName(route, withoutSuffix)),
Call: strings.Title(strings.TrimSuffix(handler, "Handler")),
HasResp: len(route.ResponseTypeName()) > 0,
HasRequest: len(route.RequestTypeName()) > 0,
Expand All @@ -74,10 +74,10 @@ func doGenToFile(dir, handler string, cfg *config.Config, group spec.Group,
})
}

func genHandlers(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error {
func genHandlers(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec, withoutSuffix bool) error {
for _, group := range api.Service.Groups {
for _, route := range group.Routes {
if err := genHandler(dir, rootPkg, cfg, group, route); err != nil {
if err := genHandler(dir, rootPkg, cfg, group, route, withoutSuffix); err != nil {
return err
}
}
Expand Down Expand Up @@ -122,20 +122,24 @@ func getHandlerFolderPath(group spec.Group, route spec.Route) string {
return path.Join(handlerDir, folder)
}

func getHandlerName(route spec.Route) string {
func getHandlerName(route spec.Route, withoutSuffix bool) string {
handler, err := getHandlerBaseName(route)
if err != nil {
panic(err)
}

return handler + "Handler"
if !withoutSuffix {
return handler + "Handler"
}
return handler
}

func getLogicName(route spec.Route) string {
func getLogicName(route spec.Route, withoutSuffix bool) string {
handler, err := getHandlerBaseName(route)
if err != nil {
panic(err)
}

return handler + "Logic"
if !withoutSuffix {
return handler + "Logic"
}
return handler
}
8 changes: 4 additions & 4 deletions tools/goctl/api/gogen/genlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import (
//go:embed logic.tpl
var logicTemplate string

func genLogic(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error {
func genLogic(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec, withoutSuffix bool) error {
for _, g := range api.Service.Groups {
for _, r := range g.Routes {
err := genLogicByRoute(dir, rootPkg, cfg, g, r)
err := genLogicByRoute(dir, rootPkg, cfg, g, r, withoutSuffix)
if err != nil {
return err
}
Expand All @@ -30,8 +30,8 @@ func genLogic(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error
return nil
}

func genLogicByRoute(dir, rootPkg string, cfg *config.Config, group spec.Group, route spec.Route) error {
logic := getLogicName(route)
func genLogicByRoute(dir, rootPkg string, cfg *config.Config, group spec.Group, route spec.Route, withoutSuffix bool) error {
logic := getLogicName(route, withoutSuffix)
goFile, err := format.FileNamingFormat(cfg.NamingFormat, logic)
if err != nil {
return err
Expand Down
13 changes: 9 additions & 4 deletions tools/goctl/api/gogen/genmiddleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,21 @@ import (
//go:embed middleware.tpl
var middlewareImplementCode string

func genMiddleware(dir string, cfg *config.Config, api *spec.ApiSpec) error {
func genMiddleware(dir string, cfg *config.Config, api *spec.ApiSpec, withoutSuffix bool) error {
middlewares := getMiddleware(api)
for _, item := range middlewares {
middlewareFilename := strings.TrimSuffix(strings.ToLower(item), "middleware") + "_middleware"
middlewareFilename := strings.TrimSuffix(strings.ToLower(item), "middleware")
if !withoutSuffix {
middlewareFilename = strings.TrimSuffix(strings.ToLower(item), "middleware") + "_middleware"
}
filename, err := format.FileNamingFormat(cfg.NamingFormat, middlewareFilename)
if err != nil {
return err
}

name := strings.TrimSuffix(item, "Middleware") + "Middleware"
name := strings.TrimSuffix(item, "Middleware")
if !withoutSuffix {
name = strings.TrimSuffix(item, "Middleware") + "Middleware"
}
err = genFile(fileGenConfig{
dir: dir,
subdir: middlewareDir,
Expand Down
8 changes: 4 additions & 4 deletions tools/goctl/api/gogen/genroutes.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ type (
}
)

func genRoutes(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error {
func genRoutes(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec, withoutSuffix bool) error {
var builder strings.Builder
groups, err := getRoutes(api)
groups, err := getRoutes(api, withoutSuffix)
if err != nil {
return err
}
Expand Down Expand Up @@ -218,13 +218,13 @@ func genRouteImports(parentPkg string, api *spec.ApiSpec) string {
return fmt.Sprintf("%s\n\n\t%s", projectSection, depSection)
}

func getRoutes(api *spec.ApiSpec) ([]group, error) {
func getRoutes(api *spec.ApiSpec, withoutSuffix bool) ([]group, error) {
var routes []group

for _, g := range api.Service.Groups {
var groupedRoutes group
for _, r := range g.Routes {
handler := getHandlerName(r)
handler := getHandlerName(r, withoutSuffix)
handler = handler + "(serverCtx)"
folder := r.GetAnnotation(groupProperty)
if len(folder) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/api/new/newservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,6 @@ func CreateServiceCommand(_ *cobra.Command, args []string) error {
return err
}

err = gogen.DoGenProject(apiFilePath, abs, VarStringStyle)
err = gogen.DoGenProject(apiFilePath, abs, VarStringStyle, true)
return err
}
6 changes: 6 additions & 0 deletions tools/goctl/internal/flags/default_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"home": "{{.global.home}}",
"remote": "{{.global.remote}}",
"branch": "{{.global.branch}}",
"without-suffix": "Remove the default suffix when generating files, such as Logic, Handler, Middleware",
"style": "{{.global.style}}"
},
"new": {
Expand Down Expand Up @@ -155,6 +156,7 @@
"url": "The data source of database,like \"root:password@tcp(127.0.0.1:3306)/database",
"table": "The table or table globbing patterns in the database",
"cache": "Generate code with cache [optional]",
"prefix": "Generate code with cache prefix [optional]",
"dir": "{{.goctl.model.dir}}",
"style": "{{.global.style}}",
"idea": "For idea plugin [optional]",
Expand All @@ -168,6 +170,7 @@
"dir": "{{.goctl.model.dir}}",
"style": "{{.global.style}}",
"cache": "Generate code with cache [optional]",
"prefix": "Generate code with cache prefix [optional]",
"idea": "For idea plugin [optional]",
"home": "{{.global.home}}",
"remote": "{{.global.remote}}",
Expand All @@ -182,6 +185,7 @@
"table": "The table or table globbing patterns in the database",
"schema": "The schema or schema globbing patterns in the database",
"cache": "Generate code with cache [optional]",
"prefix": "Generate code with cache prefix [optional]",
"dir": "{{.goctl.model.dir}}",
"style": "{{.global.style}}",
"idea": "For idea plugin [optional]",
Expand All @@ -195,6 +199,7 @@
"short": "Generate mongo model",
"type": "Specified model type name",
"cache": "Generate code with cache [optional]",
"prefix": "Generate code with cache prefix [optional]",
"easy": "Generate code with auto generated CollectionName for easy declare [optional]",
"dir": "{{.goctl.model.dir}}",
"style": "{{.global.style}}",
Expand Down Expand Up @@ -241,6 +246,7 @@
"multiple": "Generated in multiple rpc service mode",
"zrpc_out": "The zrpc output directory",
"style": "{{.global.style}}",
"without-suffix": "Remove the default suffix when generating files, such as Logic, Service",
"home": "{{.global.home}}",
"remote": "{{.global.remote}}",
"branch": "{{.global.branch}}",
Expand Down
13 changes: 9 additions & 4 deletions tools/goctl/model/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ func init() {
ddlCmdFlags.StringVarP(&command.VarStringSrc, "src", "s")
ddlCmdFlags.StringVarP(&command.VarStringDir, "dir", "d")
ddlCmdFlags.StringVar(&command.VarStringStyle, "style")
ddlCmdFlags.BoolVarP(&command.VarWithoutSuffix, "without-suffix", "w")
ddlCmdFlags.BoolVarP(&command.VarBoolCache, "cache", "c")
ddlCmdFlags.StringVar(&command.VarStringPrefix, "prefix")
ddlCmdFlags.BoolVar(&command.VarBoolIdea, "idea")
ddlCmdFlags.StringVar(&command.VarStringDatabase, "database")
ddlCmdFlags.StringVar(&command.VarStringHome, "home")
Expand All @@ -38,8 +40,10 @@ func init() {
datasourceCmdFlags.StringVar(&command.VarStringURL, "url")
datasourceCmdFlags.StringSliceVarP(&command.VarStringSliceTable, "table", "t")
datasourceCmdFlags.BoolVarP(&command.VarBoolCache, "cache", "c")
datasourceCmdFlags.StringVar(&command.VarStringPrefix, "prefix")
datasourceCmdFlags.StringVarP(&command.VarStringDir, "dir", "d")
datasourceCmdFlags.StringVar(&command.VarStringStyle, "style")
datasourceCmdFlags.BoolVarP(&command.VarWithoutSuffix, "without-suffix", "w")
datasourceCmdFlags.BoolVar(&command.VarBoolIdea, "idea")
datasourceCmdFlags.StringVar(&command.VarStringHome, "home")
datasourceCmdFlags.StringVar(&command.VarStringRemote, "remote")
Expand All @@ -49,28 +53,29 @@ func init() {
pgDatasourceCmdFlags.StringSliceVarP(&command.VarStringSliceTable, "table", "t")
pgDatasourceCmdFlags.StringVarPWithDefaultValue(&command.VarStringSchema, "schema", "s", "public")
pgDatasourceCmdFlags.BoolVarP(&command.VarBoolCache, "cache", "c")
pgDatasourceCmdFlags.StringVar(&command.VarStringPrefix, "prefix")
pgDatasourceCmdFlags.StringVarP(&command.VarStringDir, "dir", "d")
pgDatasourceCmdFlags.StringVar(&command.VarStringStyle, "style")
pgDatasourceCmdFlags.BoolVarP(&command.VarWithoutSuffix, "without-suffix", "w")
pgDatasourceCmdFlags.BoolVar(&command.VarBoolIdea, "idea")
pgDatasourceCmdFlags.BoolVar(&command.VarBoolStrict, "strict")
pgDatasourceCmdFlags.StringVar(&command.VarStringHome, "home")
pgDatasourceCmdFlags.StringVar(&command.VarStringRemote, "remote")
pgDatasourceCmdFlags.StringVar(&command.VarStringBranch, "branch")
pgCmd.PersistentFlags().StringSliceVarPWithDefaultValue(&command.VarStringSliceIgnoreColumns,
"ignore-columns", "i", []string{"create_at", "created_at", "create_time", "update_at", "updated_at", "update_time"})

mongoCmdFlags.StringSliceVarP(&mongo.VarStringSliceType, "type", "t")
mongoCmdFlags.BoolVarP(&mongo.VarBoolCache, "cache", "c")
mongoCmdFlags.StringVar(&command.VarStringPrefix, "prefix")
mongoCmdFlags.BoolVarP(&mongo.VarBoolEasy, "easy", "e")
mongoCmdFlags.StringVarP(&mongo.VarStringDir, "dir", "d")
mongoCmdFlags.StringVar(&mongo.VarStringStyle, "style")
mongoCmdFlags.BoolVarP(&command.VarWithoutSuffix, "without-suffix", "w")
mongoCmdFlags.StringVar(&mongo.VarStringHome, "home")
mongoCmdFlags.StringVar(&mongo.VarStringRemote, "remote")
mongoCmdFlags.StringVar(&mongo.VarStringBranch, "branch")

mysqlCmd.PersistentFlags().BoolVar(&command.VarBoolStrict, "strict")
mysqlCmd.PersistentFlags().StringSliceVarPWithDefaultValue(&command.VarStringSliceIgnoreColumns,
"ignore-columns", "i", []string{"create_at", "created_at", "create_time", "update_at", "updated_at", "update_time"})
mysqlCmd.PersistentFlags().StringSliceVarPWithDefaultValue(&command.VarStringSliceIgnoreColumns, "ignore-columns", "i", []string{"create_at", "created_at", "create_time", "update_at", "updated_at", "update_time"})

mysqlCmd.AddCommand(datasourceCmd, ddlCmd)
pgCmd.AddCommand(pgDatasourceCmd)
Expand Down
Loading