format code (#386)

This commit is contained in:
Kevin Wan
2021-01-14 13:24:24 +08:00
committed by GitHub
parent 744c18b7cb
commit eca4ed2cc0
16 changed files with 114 additions and 96 deletions

View File

@@ -58,5 +58,6 @@ func genDelete(table Table, withCache bool) (string, string, error) {
if err != nil {
return "", "", err
}
return output.String(), deleteMethodOut.String(), nil
}

View File

@@ -10,6 +10,7 @@ import (
func genFields(fields []parser.Field) (string, error) {
var list []string
for _, field := range fields {
result, err := genField(field)
if err != nil {
@@ -18,6 +19,7 @@ func genFields(fields []parser.Field) (string, error) {
list = append(list, result)
}
return strings.Join(list, "\n"), nil
}

View File

@@ -44,5 +44,6 @@ func genFindOne(table Table, withCache bool) (string, string, error) {
if err != nil {
return "", "", err
}
return output.String(), findOneMethod.String(), nil
}

View File

@@ -25,7 +25,7 @@ const (
type (
defaultGenerator struct {
//source string
// source string
dir string
console.Console
pkg string
@@ -57,6 +57,7 @@ func NewDefaultGenerator(dir string, cfg *config.Config, opt ...Option) (*defaul
for _, fn := range optionList {
fn(generator)
}
return generator, nil
}
@@ -96,6 +97,7 @@ func (g *defaultGenerator) StartFromInformationSchema(db string, columns map[str
m[table.Name.Source()] = code
}
return g.createFile(m)
}
@@ -130,6 +132,7 @@ func (g *defaultGenerator) createFile(modelList map[string]string) error {
return err
}
}
// generate error file
varFilename, err := format.FileNamingFormat(g.cfg.NamingFormat, "vars")
if err != nil {
@@ -168,16 +171,15 @@ func (g *defaultGenerator) genFromDDL(source string, withCache bool) (map[string
}
m[table.Name.Source()] = code
}
return m, nil
}
type (
Table struct {
parser.Table
CacheKey map[string]Key
ContainsUniqueKey bool
}
)
type Table struct {
parser.Table
CacheKey map[string]Key
ContainsUniqueKey bool
}
func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, error) {
if len(in.PrimaryKey.Name.Source()) == 0 {
@@ -292,5 +294,6 @@ func wrapWithRawString(v string) string {
} else if len(v) == 1 {
v = v + "`"
}
return v
}

View File

@@ -8,20 +8,18 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
)
type (
// tableName:user
// {{prefix}}=cache
// key:id
Key struct {
VarExpression string // cacheUserIdPrefix = "cache#User#id#"
Left string // cacheUserIdPrefix
Right string // cache#user#id#
Variable string // userIdKey
KeyExpression string // userIdKey: = fmt.Sprintf("cache#user#id#%v", userId)
DataKeyExpression string // userIdKey: = fmt.Sprintf("cache#user#id#%v", data.userId)
RespKeyExpression string // userIdKey: = fmt.Sprintf("cache#user#id#%v", resp.userId)
}
)
// tableName:user
// {{prefix}}=cache
// key:id
type Key struct {
VarExpression string // cacheUserIdPrefix = "cache#User#id#"
Left string // cacheUserIdPrefix
Right string // cache#user#id#
Variable string // userIdKey
KeyExpression string // userIdKey: = fmt.Sprintf("cache#user#id#%v", userId)
DataKeyExpression string // userIdKey: = fmt.Sprintf("cache#user#id#%v", data.userId)
RespKeyExpression string // userIdKey: = fmt.Sprintf("cache#user#id#%v", resp.userId)
}
// key-数据库原始字段名,value-缓存key相关数据
func genCacheKeys(table parser.Table) (map[string]Key, error) {
@@ -42,6 +40,7 @@ func genCacheKeys(table parser.Table) (map[string]Key, error) {
if strings.ToLower(lowerStartCamelTableName) == strings.ToLower(camelFieldName) {
variable = fmt.Sprintf("%sKey", lowerStartCamelTableName)
}
m[field.Name.Source()] = Key{
VarExpression: fmt.Sprintf(`%s = "%s"`, left, right),
Left: left,
@@ -53,5 +52,6 @@ func genCacheKeys(table parser.Table) (map[string]Key, error) {
}
}
}
return m, nil
}

View File

@@ -68,5 +68,4 @@ func TestGenCacheKeys(t *testing.T) {
assert.Equal(t, fmt.Sprintf(`user%sKey`, name.ToCamel()), key.Variable)
assert.Equal(t, `user`+name.ToCamel()+`Key := fmt.Sprintf("%s%v", cacheUser`+name.ToCamel()+`Prefix,`+name.Untitle()+`)`, key.KeyExpression)
}
}

View File

@@ -1,13 +1,12 @@
package gen
import (
"regexp"
)
import "regexp"
func (g *defaultGenerator) split(source string) []string {
reg := regexp.MustCompile(createTableFlag)
index := reg.FindAllStringIndex(source, -1)
list := make([]string, 0)
for i := len(index) - 1; i >= 0; i-- {
subIndex := index[i]
if len(subIndex) == 0 {
@@ -18,5 +17,6 @@ func (g *defaultGenerator) split(source string) []string {
list = append(list, ddl)
source = source[:start]
}
return list
}

View File

@@ -9,16 +9,15 @@ func genTag(in string) (string, error) {
if in == "" {
return in, nil
}
text, err := util.LoadTemplate(category, tagTemplateFile, template.Tag)
if err != nil {
return "", err
}
output, err := util.With("tag").
Parse(text).
Execute(map[string]interface{}{
"field": in,
})
output, err := util.With("tag").Parse(text).Execute(map[string]interface{}{
"field": in,
})
if err != nil {
return "", err
}

View File

@@ -71,6 +71,7 @@ func RevertTemplate(name string) error {
if !ok {
return fmt.Errorf("%s: no such file name", name)
}
return util.CreateTemplate(category, name, content)
}
@@ -79,5 +80,6 @@ func Update() error {
if err != nil {
return err
}
return util.InitTemplates(category, templates)
}

View File

@@ -15,11 +15,14 @@ func genUpdate(table Table, withCache bool) (string, string, error) {
if camel == "CreateTime" || camel == "UpdateTime" {
continue
}
if field.IsPrimaryKey {
continue
}
expressionValues = append(expressionValues, "data."+camel)
}
expressionValues = append(expressionValues, "data."+table.PrimaryKey.Name.ToCamel())
camelTableName := table.Name.ToCamel()
text, err := util.LoadTemplate(category, updateTemplateFile, template.Update)