model template fix (#169)

* replace quote

* rpc disable override main.go

* reactor template

* add model flag -style

* add model flag -style

* reactor model  template name of error
This commit is contained in:
Keson
2020-10-27 22:42:53 +08:00
committed by GitHub
parent 4287877b74
commit 607fc3297a
10 changed files with 80 additions and 31 deletions

View File

@@ -17,6 +17,9 @@ import (
const (
pwd = "."
createTableFlag = `(?m)^(?i)CREATE\s+TABLE` // ignore case
NamingLower = "lower"
NamingCamel = "camel"
NamingUnderline = "underline"
)
type (
@@ -24,16 +27,17 @@ type (
source string
dir string
console.Console
pkg string
pkg string
namingStyle string
}
Option func(generator *defaultGenerator)
)
func NewDefaultGenerator(source, dir string, opt ...Option) *defaultGenerator {
func NewDefaultGenerator(source, dir, namingStyle string, opt ...Option) *defaultGenerator {
if dir == "" {
dir = pwd
}
generator := &defaultGenerator{source: source, dir: dir}
generator := &defaultGenerator{source: source, dir: dir, namingStyle: namingStyle}
var optionList []Option
optionList = append(optionList, newDefaultOption())
optionList = append(optionList, opt...)
@@ -72,7 +76,14 @@ func (g *defaultGenerator) Start(withCache bool) error {
}
for tableName, code := range modelList {
name := fmt.Sprintf("%smodel.go", strings.ToLower(stringx.From(tableName).ToCamel()))
tn := stringx.From(tableName)
name := fmt.Sprintf("%smodel.go", strings.ToLower(tn.ToCamel()))
switch g.namingStyle {
case NamingCamel:
name = fmt.Sprintf("%sModel.go", tn.ToCamel())
case NamingUnderline:
name = fmt.Sprintf("%s_model.go", tn.ToSnake())
}
filename := filepath.Join(dirAbs, name)
if util.FileExists(filename) {
g.Warning("%s already exists, ignored.", name)
@@ -85,7 +96,7 @@ func (g *defaultGenerator) Start(withCache bool) error {
}
// generate error file
filename := filepath.Join(dirAbs, "vars.go")
text, err := util.LoadTemplate(category, modelTemplateFile, template.Error)
text, err := util.LoadTemplate(category, errTemplateFile, template.Error)
if err != nil {
return err
}

View File

@@ -8,13 +8,27 @@ import (
)
var (
source = "CREATE TABLE `test` (\n `id` bigint NOT NULL AUTO_INCREMENT,\n `nanosecond` bigint NOT NULL DEFAULT '0',\n `data` varchar(255) DEFAULT '',\n `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`),\n UNIQUE KEY `nanosecond_unique` (`nanosecond`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;"
source = "CREATE TABLE `test_user_info` (\n `id` bigint NOT NULL AUTO_INCREMENT,\n `nanosecond` bigint NOT NULL DEFAULT '0',\n `data` varchar(255) DEFAULT '',\n `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`),\n UNIQUE KEY `nanosecond_unique` (`nanosecond`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;"
)
func TestCacheModel(t *testing.T) {
logx.Disable()
_ = Clean()
g := NewDefaultGenerator(source, "./testmodel")
g := NewDefaultGenerator(source, "./testmodel/cache", NamingLower)
err := g.Start(true)
assert.Nil(t, err)
g = NewDefaultGenerator(source, "./testmodel/nocache", NamingLower)
err = g.Start(false)
assert.Nil(t, err)
}
func TestNamingModel(t *testing.T) {
logx.Disable()
_ = Clean()
g := NewDefaultGenerator(source, "./testmodel/camel", NamingCamel)
err := g.Start(true)
assert.Nil(t, err)
g = NewDefaultGenerator(source, "./testmodel/snake", NamingUnderline)
err = g.Start(true)
assert.Nil(t, err)
}