From 7a4818da59e22b1536f38d1efdec643a8244939f Mon Sep 17 00:00:00 2001 From: kesonan Date: Wed, 12 Feb 2025 23:31:30 +0800 Subject: [PATCH] Generate caches that support custom key prefix. (#4643) --- tools/goctl/internal/flags/default_en.json | 1 + tools/goctl/model/cmd.go | 1 + tools/goctl/model/sql/command/command.go | 18 +++++++++++--- tools/goctl/model/sql/example/makefile | 16 ++++++------- tools/goctl/model/sql/gen/gen.go | 7 +++--- tools/goctl/model/sql/gen/gen_test.go | 14 +++++------ tools/goctl/model/sql/gen/keys.go | 16 ++++++------- tools/goctl/model/sql/gen/keys_test.go | 4 ++-- tools/goctl/util/ctx/gomod_test.go | 28 +++++++++++----------- 9 files changed, 60 insertions(+), 45 deletions(-) diff --git a/tools/goctl/internal/flags/default_en.json b/tools/goctl/internal/flags/default_en.json index ae5b486c3..4e3193645 100644 --- a/tools/goctl/internal/flags/default_en.json +++ b/tools/goctl/internal/flags/default_en.json @@ -151,6 +151,7 @@ "short": "Generate mysql model", "strict": "Generate model in strict mode", "ignore-columns": "Ignore columns while creating or updating rows", + "prefix": "The cache prefix, effective when --cache is true", "datasource": { "short": "Generate model from datasource", "url": "The data source of database,like \"root:password@tcp(127.0.0.1:3306)/database", diff --git a/tools/goctl/model/cmd.go b/tools/goctl/model/cmd.go index e340f2c58..7ae7477fd 100644 --- a/tools/goctl/model/cmd.go +++ b/tools/goctl/model/cmd.go @@ -71,6 +71,7 @@ func init() { 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().StringVarPWithDefaultValue(&command.VarStringCachePrefix, "prefix", "p", "cache") mysqlCmd.AddCommand(datasourceCmd, ddlCmd) pgCmd.AddCommand(pgDatasourceCmd) diff --git a/tools/goctl/model/sql/command/command.go b/tools/goctl/model/sql/command/command.go index 2b4e49e49..8fbcb5cd2 100644 --- a/tools/goctl/model/sql/command/command.go +++ b/tools/goctl/model/sql/command/command.go @@ -50,6 +50,8 @@ var ( VarBoolStrict bool // VarStringSliceIgnoreColumns represents the columns which are ignored. VarStringSliceIgnoreColumns []string + // VarStringCachePrefix describes the prefix of cache. + VarStringCachePrefix string ) var errNotMatched = errors.New("sql not matched") @@ -57,6 +59,9 @@ var errNotMatched = errors.New("sql not matched") // MysqlDDL generates model code from ddl func MysqlDDL(_ *cobra.Command, _ []string) error { migrationnotes.BeforeCommands(VarStringDir, VarStringStyle) + if VarBoolCache && len(VarStringCachePrefix) == 0 { + return errors.New("cache prefix is empty") + } src := VarStringSrc dir := VarStringDir cache := VarBoolCache @@ -89,6 +94,7 @@ func MysqlDDL(_ *cobra.Command, _ []string) error { database: database, strict: VarBoolStrict, ignoreColumns: mergeColumns(VarStringSliceIgnoreColumns), + prefix: VarStringCachePrefix, } return fromDDL(arg) } @@ -96,6 +102,9 @@ func MysqlDDL(_ *cobra.Command, _ []string) error { // MySqlDataSource generates model code from datasource func MySqlDataSource(_ *cobra.Command, _ []string) error { migrationnotes.BeforeCommands(VarStringDir, VarStringStyle) + if VarBoolCache && len(VarStringCachePrefix) == 0 { + return errors.New("cache prefix is empty") + } url := strings.TrimSpace(VarStringURL) dir := strings.TrimSpace(VarStringDir) cache := VarBoolCache @@ -130,6 +139,7 @@ func MySqlDataSource(_ *cobra.Command, _ []string) error { idea: idea, strict: VarBoolStrict, ignoreColumns: mergeColumns(VarStringSliceIgnoreColumns), + prefix: VarStringCachePrefix, } return fromMysqlDataSource(arg) } @@ -225,6 +235,7 @@ type ddlArg struct { database string strict bool ignoreColumns []string + prefix string } func fromDDL(arg ddlArg) error { @@ -243,7 +254,7 @@ func fromDDL(arg ddlArg) error { return errNotMatched } - generator, err := gen.NewDefaultGenerator(arg.dir, arg.cfg, + generator, err := gen.NewDefaultGenerator(arg.prefix, arg.dir, arg.cfg, gen.WithConsoleOption(log), gen.WithIgnoreColumns(arg.ignoreColumns)) if err != nil { return err @@ -266,6 +277,7 @@ type dataSourceArg struct { cache, idea bool strict bool ignoreColumns []string + prefix string } func fromMysqlDataSource(arg dataSourceArg) error { @@ -318,7 +330,7 @@ func fromMysqlDataSource(arg dataSourceArg) error { return errors.New("no tables matched") } - generator, err := gen.NewDefaultGenerator(arg.dir, arg.cfg, + generator, err := gen.NewDefaultGenerator(arg.prefix, arg.dir, arg.cfg, gen.WithConsoleOption(log), gen.WithIgnoreColumns(arg.ignoreColumns)) if err != nil { return err @@ -369,7 +381,7 @@ func fromPostgreSqlDataSource(url string, pattern pattern, dir, schema string, c return errors.New("no tables matched") } - generator, err := gen.NewDefaultGenerator(dir, cfg, gen.WithConsoleOption(log), gen.WithPostgreSql(), gen.WithIgnoreColumns(ignoreColumns)) + generator, err := gen.NewDefaultGenerator("", dir, cfg, gen.WithConsoleOption(log), gen.WithPostgreSql(), gen.WithIgnoreColumns(ignoreColumns)) if err != nil { return err } diff --git a/tools/goctl/model/sql/example/makefile b/tools/goctl/model/sql/example/makefile index 993683091..9ce80752a 100644 --- a/tools/goctl/model/sql/example/makefile +++ b/tools/goctl/model/sql/example/makefile @@ -3,15 +3,15 @@ # generate model with cache from ddl fromDDLWithCache: goctl template clean - goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/cache" -cache + goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/cache" -cache --prefix gozero fromDDLWithCacheAndIgnoreColumns: goctl template clean - goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/ignore_columns/cache" -cache -i 'gmt_create,create_at' -i 'gmt_modified,update_at' + goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/ignore_columns/cache" -cache -i 'gmt_create,create_at' -i 'gmt_modified,update_at' --prefix gozero fromDDLWithCacheAndDb: goctl template clean - goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/cache_db" -database="1gozero" -cache + goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/cache_db" -database="1gozero" -cache --prefix gozero fromDDLWithoutCache: goctl template clean; @@ -19,11 +19,11 @@ fromDDLWithoutCache: # generate model with cache from data source -user=root -password=password -datasource=127.0.0.1:3306 -database=gozero +user=app_user +password=PLO75FbcfmFYRuQEGmygZ9PyQCQbmgeD5 +datasource=k8s-istiosys-unifydev-2cdcebd306-b64e09be84220820.elb.ap-southeast-1.amazonaws.com:3306 +database=db_fiat fromDataSource: goctl template clean - goctl model mysql datasource -url="$(user):$(password)@tcp($(datasource))/$(database)" -table="*" -dir ./model/cache -c -style gozero + goctl model mysql datasource -url="$(user):$(password)@tcp($(datasource))/$(database)" -table="biz_switch" -dir ./model/cache -c -style gozero --prefix gozero diff --git a/tools/goctl/model/sql/gen/gen.go b/tools/goctl/model/sql/gen/gen.go index 37b9e7250..83056919e 100644 --- a/tools/goctl/model/sql/gen/gen.go +++ b/tools/goctl/model/sql/gen/gen.go @@ -30,6 +30,7 @@ type ( cfg *config.Config isPostgreSql bool ignoreColumns []string + prefix string } // Option defines a function with argument defaultGenerator @@ -56,7 +57,7 @@ type ( ) // NewDefaultGenerator creates an instance for defaultGenerator -func NewDefaultGenerator(dir string, cfg *config.Config, opt ...Option) (*defaultGenerator, error) { +func NewDefaultGenerator(prefix, dir string, cfg *config.Config, opt ...Option) (*defaultGenerator, error) { if dir == "" { dir = pwd } @@ -72,7 +73,7 @@ func NewDefaultGenerator(dir string, cfg *config.Config, opt ...Option) (*defaul return nil, err } - generator := &defaultGenerator{dir: dir, cfg: cfg, pkg: pkg} + generator := &defaultGenerator{dir: dir, cfg: cfg, pkg: pkg, prefix: prefix} var optionList []Option optionList = append(optionList, newDefaultOption()) optionList = append(optionList, opt...) @@ -260,7 +261,7 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er return "", fmt.Errorf("table %s: missing primary key", in.Name.Source()) } - primaryKey, uniqueKey := genCacheKeys(in) + primaryKey, uniqueKey := genCacheKeys(g.prefix, in) var table Table table.Table = in diff --git a/tools/goctl/model/sql/gen/gen_test.go b/tools/goctl/model/sql/gen/gen_test.go index ded112aff..5caaf5af3 100644 --- a/tools/goctl/model/sql/gen/gen_test.go +++ b/tools/goctl/model/sql/gen/gen_test.go @@ -34,7 +34,7 @@ func TestCacheModel(t *testing.T) { dir := filepath.Join(pathx.MustTempDir(), "./testmodel") cacheDir := filepath.Join(dir, "cache") noCacheDir := filepath.Join(dir, "nocache") - g, err := NewDefaultGenerator(cacheDir, &config.Config{ + g, err := NewDefaultGenerator("cache", cacheDir, &config.Config{ NamingFormat: "GoZero", }) assert.Nil(t, err) @@ -45,7 +45,7 @@ func TestCacheModel(t *testing.T) { _, err := os.Stat(filepath.Join(cacheDir, "TestUserModel.go")) return err == nil }()) - g, err = NewDefaultGenerator(noCacheDir, &config.Config{ + g, err = NewDefaultGenerator("cache", noCacheDir, &config.Config{ NamingFormat: "gozero", }) assert.Nil(t, err) @@ -72,7 +72,7 @@ func TestNamingModel(t *testing.T) { defer func() { _ = os.RemoveAll(dir) }() - g, err := NewDefaultGenerator(camelDir, &config.Config{ + g, err := NewDefaultGenerator("cache", camelDir, &config.Config{ NamingFormat: "GoZero", }) assert.Nil(t, err) @@ -83,7 +83,7 @@ func TestNamingModel(t *testing.T) { _, err := os.Stat(filepath.Join(camelDir, "TestUserModel.go")) return err == nil }()) - g, err = NewDefaultGenerator(snakeDir, &config.Config{ + g, err = NewDefaultGenerator("cache", snakeDir, &config.Config{ NamingFormat: "go_zero", }) assert.Nil(t, err) @@ -110,7 +110,7 @@ func TestFolderName(t *testing.T) { defer func() { _ = os.RemoveAll(dir) }() - g, err := NewDefaultGenerator(camelDir, &config.Config{ + g, err := NewDefaultGenerator("cache", camelDir, &config.Config{ NamingFormat: "GoZero", }) assert.Nil(t, err) @@ -125,7 +125,7 @@ func TestFolderName(t *testing.T) { }()) assert.Equal(t, pkg, g.pkg) - g, err = NewDefaultGenerator(snakeDir, &config.Config{ + g, err = NewDefaultGenerator("cache", snakeDir, &config.Config{ NamingFormat: "go_zero", }) assert.Nil(t, err) @@ -180,7 +180,7 @@ func Test_genPublicModel(t *testing.T) { err = os.WriteFile(modelFilename, []byte(source), 0o777) require.NoError(t, err) - g, err := NewDefaultGenerator(modelDir, &config.Config{ + g, err := NewDefaultGenerator("cache", modelDir, &config.Config{ NamingFormat: config.DefaultFormat, }) require.NoError(t, err) diff --git a/tools/goctl/model/sql/gen/keys.go b/tools/goctl/model/sql/gen/keys.go index 2a19ff47e..ff4f93185 100644 --- a/tools/goctl/model/sql/gen/keys.go +++ b/tools/goctl/model/sql/gen/keys.go @@ -37,12 +37,12 @@ type Key struct { // Join describes an alias of string slice type Join []string -func genCacheKeys(table parser.Table) (Key, []Key) { +func genCacheKeys(prefix string, table parser.Table) (Key, []Key) { var primaryKey Key var uniqueKey []Key - primaryKey = genCacheKey(table.Db, table.Name, []*parser.Field{&table.PrimaryKey.Field}) + primaryKey = genCacheKey(prefix, table.Db, table.Name, []*parser.Field{&table.PrimaryKey.Field}) for _, each := range table.UniqueIndex { - uniqueKey = append(uniqueKey, genCacheKey(table.Db, table.Name, each)) + uniqueKey = append(uniqueKey, genCacheKey(prefix, table.Db, table.Name, each)) } sort.Slice(uniqueKey, func(i, j int) bool { return uniqueKey[i].VarLeft < uniqueKey[j].VarLeft @@ -51,7 +51,7 @@ func genCacheKeys(table parser.Table) (Key, []Key) { return primaryKey, uniqueKey } -func genCacheKey(db, table stringx.String, in []*parser.Field) Key { +func genCacheKey(prefix string, db, table stringx.String, in []*parser.Field) Key { var ( varLeftJoin, varRightJoin, fieldNameJoin Join varLeft, varRight, varExpression string @@ -62,12 +62,12 @@ func genCacheKey(db, table stringx.String, in []*parser.Field) Key { dbName, tableName := util.SafeString(db.Source()), util.SafeString(table.Source()) if len(dbName) > 0 { - varLeftJoin = append(varLeftJoin, "cache", dbName, tableName) - varRightJoin = append(varRightJoin, "cache", dbName, tableName) + varLeftJoin = append(varLeftJoin, prefix, dbName, tableName) + varRightJoin = append(varRightJoin, prefix, dbName, tableName) keyLeftJoin = append(keyLeftJoin, dbName, tableName) } else { - varLeftJoin = append(varLeftJoin, "cache", tableName) - varRightJoin = append(varRightJoin, "cache", tableName) + varLeftJoin = append(varLeftJoin, prefix, tableName) + varRightJoin = append(varRightJoin, prefix, tableName) keyLeftJoin = append(keyLeftJoin, tableName) } diff --git a/tools/goctl/model/sql/gen/keys_test.go b/tools/goctl/model/sql/gen/keys_test.go index c54cc4fed..fc86674d6 100644 --- a/tools/goctl/model/sql/gen/keys_test.go +++ b/tools/goctl/model/sql/gen/keys_test.go @@ -34,7 +34,7 @@ func TestGenCacheKeys(t *testing.T) { Comment: "姓名", SeqInIndex: 2, } - primariCacheKey, uniqueCacheKey := genCacheKeys(parser.Table{ + primariCacheKey, uniqueCacheKey := genCacheKeys("cache", parser.Table{ Name: stringx.From("user"), Db: stringx.From("go_zero"), PrimaryKey: parser.Primary{ @@ -129,7 +129,7 @@ func TestGenCacheKeys(t *testing.T) { }()) }) t.Run("no database name", func(t *testing.T) { - primariCacheKey, _ = genCacheKeys(parser.Table{ + primariCacheKey, _ = genCacheKeys("cache", parser.Table{ Name: stringx.From("user"), Db: stringx.From(""), PrimaryKey: parser.Primary{ diff --git a/tools/goctl/util/ctx/gomod_test.go b/tools/goctl/util/ctx/gomod_test.go index e1bae88cb..35175bde8 100644 --- a/tools/goctl/util/ctx/gomod_test.go +++ b/tools/goctl/util/ctx/gomod_test.go @@ -101,54 +101,54 @@ func Test_getRealModule(t *testing.T) { { name: "go work duplicate prefix", args: args{ - workDir: "D:\\code\\company\\core-ee\\service", + workDir: "/code/company/core-ee/service", execRun: func(arg, dir string, in ...*bytes.Buffer) (string, error) { return ` { "Path": "gitee.com/unitedrhino/core", - "Dir": "D:\\code\\company\\core", - "GoMod": "D:\\code\\company\\core\\go.mod", + "Dir": "/code/company/core", + "GoMod": "/code/company/core/go.mod", "GoVersion": "1.21.4" } { "Path": "gitee.com/unitedrhino/core-ee", - "Dir": "D:\\code\\company\\core-ee", - "GoMod": "D:\\code\\company\\core-ee\\go.mod", + "Dir": "/code/company/core-ee", + "GoMod": "/code/company/core-ee/go.mod", "GoVersion": "1.21.4" }`, nil }, }, want: &Module{ Path: "gitee.com/unitedrhino/core-ee", - Dir: "D:\\code\\company\\core-ee", - GoMod: "D:\\code\\company\\core-ee\\go.mod", + Dir: "/code/company/core-ee", + GoMod: "/code/company/core-ee/go.mod", GoVersion: "1.21.4", }, }, { name: "go work duplicate prefix2", args: args{ - workDir: "D:\\code\\company\\core-ee", + workDir: "/code/company/core-ee", execRun: func(arg, dir string, in ...*bytes.Buffer) (string, error) { return ` { "Path": "gitee.com/unitedrhino/core", - "Dir": "D:\\code\\company\\core", - "GoMod": "D:\\code\\company\\core\\go.mod", + "Dir": "/code/company/core", + "GoMod": "/code/company/core/go.mod", "GoVersion": "1.21.4" } { "Path": "gitee.com/unitedrhino/core-ee", - "Dir": "D:\\code\\company\\core-ee", - "GoMod": "D:\\code\\company\\core-ee\\go.mod", + "Dir": "/code/company/core-ee", + "GoMod": "/code/company/core-ee/go.mod", "GoVersion": "1.21.4" }`, nil }, }, want: &Module{ Path: "gitee.com/unitedrhino/core-ee", - Dir: "D:\\code\\company\\core-ee", - GoMod: "D:\\code\\company\\core-ee\\go.mod", + Dir: "/code/company/core-ee", + GoMod: "/code/company/core-ee/go.mod", GoVersion: "1.21.4", }, },