Optimize model naming (#910)

* add unit test

* fix #907

* format code

* format code

* format code

Co-authored-by: anqiansong <anqiansong@xiaoheiban.cn>
This commit is contained in:
anqiansong
2021-08-18 17:09:34 +08:00
committed by GitHub
parent a1fe8bf6cd
commit b2fea65faa
8 changed files with 230 additions and 15 deletions

View File

@@ -146,7 +146,7 @@ func (g *defaultGenerator) createFile(modelList map[string]string) error {
return err
}
name := modelFilename + ".go"
name := util.SafeString(modelFilename) + ".go"
filename := filepath.Join(dirAbs, name)
if util.FileExists(filename) {
g.Warning("%s already exists, ignored.", name)

View File

@@ -6,6 +6,7 @@ import (
"strings"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/parser"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
)
@@ -59,9 +60,16 @@ func genCacheKey(db, table stringx.String, in []*parser.Field) Key {
keyLeft, keyRight, dataKeyRight, keyExpression, dataKeyExpression string
)
varLeftJoin = append(varLeftJoin, "cache", db.Source(), table.Source())
varRightJon = append(varRightJon, "cache", db.Source(), table.Source())
keyLeftJoin = append(keyLeftJoin, db.Source(), table.Source())
dbName, tableName := util.SafeString(db.Source()), util.SafeString(table.Source())
if len(dbName) > 0 {
varLeftJoin = append(varLeftJoin, "cache", dbName, tableName)
varRightJon = append(varRightJon, "cache", dbName, tableName)
keyLeftJoin = append(keyLeftJoin, dbName, tableName)
} else {
varLeftJoin = append(varLeftJoin, "cache", tableName)
varRightJon = append(varRightJon, "cache", tableName)
keyLeftJoin = append(keyLeftJoin, tableName)
}
for _, each := range in {
varLeftJoin = append(varLeftJoin, each.Name.Source())
@@ -75,11 +83,11 @@ func genCacheKey(db, table stringx.String, in []*parser.Field) Key {
varLeftJoin = append(varLeftJoin, "prefix")
keyLeftJoin = append(keyLeftJoin, "key")
varLeft = varLeftJoin.Camel().With("").Untitle()
varLeft = util.SafeString(varLeftJoin.Camel().With("").Untitle())
varRight = fmt.Sprintf(`"%s"`, varRightJon.Camel().Untitle().With(":").Source()+":")
varExpression = fmt.Sprintf(`%s = %s`, varLeft, varRight)
keyLeft = keyLeftJoin.Camel().With("").Untitle()
keyLeft = util.SafeString(keyLeftJoin.Camel().With("").Untitle())
keyRight = fmt.Sprintf(`fmt.Sprintf("%s%s", %s, %s)`, "%s", keyRightArgJoin.With(":").Source(), varLeft, keyRightJoin.With(", ").Source())
dataKeyRight = fmt.Sprintf(`fmt.Sprintf("%s%s", %s, %s)`, "%s", keyRightArgJoin.With(":").Source(), varLeft, dataRightJoin.With(", ").Source())
keyExpression = fmt.Sprintf("%s := %s", keyLeft, keyRight)