mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-10 08:29:58 +08:00
Generate caches that support custom key prefix. (#4643)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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{
|
||||
|
||||
Reference in New Issue
Block a user