mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-10 08:29:58 +08:00
add console & example
This commit is contained in:
@@ -15,8 +15,7 @@ func genDelete(table Table, withCache bool) (string, error) {
|
||||
for fieldName, key := range table.CacheKey {
|
||||
if fieldName == table.PrimaryKey.Name.Source() {
|
||||
keySet.AddStr(key.KeyExpression)
|
||||
}
|
||||
if fieldName != table.PrimaryKey.Name.Source() {
|
||||
} else {
|
||||
keySet.AddStr(key.DataKeyExpression)
|
||||
}
|
||||
keyVariableSet.AddStr(key.Variable)
|
||||
|
||||
@@ -8,8 +8,9 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/parser"
|
||||
sqltemplate "github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/console"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/templatex"
|
||||
)
|
||||
@@ -24,17 +25,40 @@ type (
|
||||
source string
|
||||
src string
|
||||
dir string
|
||||
console.Console
|
||||
}
|
||||
Option func(generator *defaultGenerator)
|
||||
)
|
||||
|
||||
func NewDefaultGenerator(src, dir string) *defaultGenerator {
|
||||
func NewDefaultGenerator(src, dir string, opt ...Option) *defaultGenerator {
|
||||
if src == "" {
|
||||
src = pwd
|
||||
}
|
||||
if dir == "" {
|
||||
dir = pwd
|
||||
}
|
||||
return &defaultGenerator{src: src, dir: dir}
|
||||
generator := &defaultGenerator{src: src, dir: dir}
|
||||
var optionList []Option
|
||||
optionList = append(optionList, newDefaultOption())
|
||||
optionList = append(optionList, opt...)
|
||||
for _, fn := range optionList {
|
||||
fn(generator)
|
||||
}
|
||||
return generator
|
||||
}
|
||||
|
||||
func WithConsoleOption(idea bool) Option {
|
||||
return func(generator *defaultGenerator) {
|
||||
if idea {
|
||||
generator.Console = console.NewIdeaConsole()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func newDefaultOption() Option {
|
||||
return func(generator *defaultGenerator) {
|
||||
generator.Console = console.NewColorConsole()
|
||||
}
|
||||
}
|
||||
|
||||
func (g *defaultGenerator) Start(withCache bool) error {
|
||||
@@ -61,12 +85,28 @@ func (g *defaultGenerator) Start(withCache bool) error {
|
||||
}
|
||||
|
||||
for tableName, code := range modelList {
|
||||
filename := filepath.Join(dirAbs, fmt.Sprintf("%smodel.go", stringx.From(tableName).Lower()))
|
||||
name := fmt.Sprintf("%smodel.go", strings.ToLower(stringx.From(tableName).Snake2Camel()))
|
||||
filename := filepath.Join(dirAbs, name)
|
||||
if util.FileExists(filename) {
|
||||
g.Warning("%s already exists", name)
|
||||
continue
|
||||
}
|
||||
err = ioutil.WriteFile(filename, []byte(code), os.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// generate error file
|
||||
filename := filepath.Join(dirAbs, "error.go")
|
||||
if util.FileExists(filename) {
|
||||
g.Warning("error.go already exists")
|
||||
} else {
|
||||
err = ioutil.WriteFile(filename, []byte(template.Error), os.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
g.Success("Done.")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -97,7 +137,7 @@ type (
|
||||
|
||||
func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, error) {
|
||||
t := templatex.With("model").
|
||||
Parse(sqltemplate.Model).
|
||||
Parse(template.Model).
|
||||
GoFmt(true)
|
||||
|
||||
m, err := genCacheKeys(in)
|
||||
@@ -112,7 +152,7 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
|
||||
table.Table = in
|
||||
table.CacheKey = m
|
||||
|
||||
varsCode, err := genVars(table)
|
||||
varsCode, err := genVars(table, withCache)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -34,17 +34,17 @@ func genCacheKeys(table parser.Table) (map[string]Key, error) {
|
||||
}
|
||||
camelFieldName := field.Name.Snake2Camel()
|
||||
lowerStartCamelFieldName := stringx.From(camelFieldName).LowerStart()
|
||||
left := fmt.Sprintf("cache%s%sPrefix", lowerStartCamelTableName, camelFieldName)
|
||||
right := fmt.Sprintf("cache#%s#%s#", lowerStartCamelTableName, lowerStartCamelFieldName)
|
||||
left := fmt.Sprintf("cache%s%sPrefix", camelTableName, camelFieldName)
|
||||
right := fmt.Sprintf("cache#%s#%s#", camelTableName, lowerStartCamelFieldName)
|
||||
variable := fmt.Sprintf("%s%sKey", lowerStartCamelTableName, camelFieldName)
|
||||
m[field.Name.Source()] = Key{
|
||||
VarExpression: fmt.Sprintf(`%s = "%s"`, left, right),
|
||||
Left: left,
|
||||
Right: right,
|
||||
Variable: variable,
|
||||
KeyExpression: fmt.Sprintf(`%s := fmt.Sprintf("cache#%s#%s#%s", %s)`, variable, lowerStartCamelTableName, lowerStartCamelFieldName, "%v", lowerStartCamelFieldName),
|
||||
DataKeyExpression: fmt.Sprintf(`%s := fmt.Sprintf("cache#%s#%s#%s", data.%s)`, variable, lowerStartCamelTableName, lowerStartCamelFieldName, "%v", camelFieldName),
|
||||
RespKeyExpression: fmt.Sprintf(`%s := fmt.Sprintf("cache#%s#%s#%s", resp.%s)`, variable, lowerStartCamelTableName, lowerStartCamelFieldName, "%v", camelFieldName),
|
||||
KeyExpression: fmt.Sprintf(`%s := fmt.Sprintf("%s%s", %s,%s)`, variable, "%s", "%v", left, lowerStartCamelFieldName),
|
||||
DataKeyExpression: fmt.Sprintf(`%s := fmt.Sprintf("%s%s",%s, data.%s)`, variable, "%s", "%v", left, camelFieldName),
|
||||
RespKeyExpression: fmt.Sprintf(`%s := fmt.Sprintf("%s%s", %s,resp.%s)`, variable, "%s", "%v", left, camelFieldName),
|
||||
}
|
||||
}
|
||||
return m, nil
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/templatex"
|
||||
)
|
||||
|
||||
func genVars(table Table) (string, error) {
|
||||
func genVars(table Table, withCache bool) (string, error) {
|
||||
keys := make([]string, 0)
|
||||
for _, v := range table.CacheKey {
|
||||
keys = append(keys, v.VarExpression)
|
||||
@@ -23,6 +23,7 @@ func genVars(table Table) (string, error) {
|
||||
"cacheKeys": strings.Join(keys, "\r\n"),
|
||||
"autoIncrement": table.PrimaryKey.AutoIncrement,
|
||||
"originalPrimaryKey": table.PrimaryKey.Name.Source(),
|
||||
"withCache": withCache,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
||||
Reference in New Issue
Block a user