Code optimized (#523)

* optimized markdown generator

* optimized markdown generator

* optimized markdown generator

* add more comment

* add comment

* add comment

* add comments for rpc tool

* add comments for model tool

* add comments for model tool

* add comments for model tool

* add comments for config tool

* add comments for config tool

* add comments

* add comments

* add comments

* add comments

* add comment

* remove rpc main head info

* add comment

* optimized

Co-authored-by: anqiansong <anqiansong@xiaoheiban.cn>
This commit is contained in:
kingxt
2021-02-26 16:11:47 +08:00
committed by GitHub
parent ef146cf5ba
commit e6ef1fca12
104 changed files with 651 additions and 375 deletions

View File

@@ -1,5 +0,0 @@
package gen
import "errors"
var ErrCircleQuery = errors.New("circle query with other fields")

View File

@@ -33,6 +33,7 @@ type (
cfg *config.Config
}
// Option defines a function with argument defaultGenerator
Option func(generator *defaultGenerator)
code struct {
@@ -48,6 +49,7 @@ type (
}
)
// NewDefaultGenerator creates an instance for defaultGenerator
func NewDefaultGenerator(dir string, cfg *config.Config, opt ...Option) (*defaultGenerator, error) {
if dir == "" {
dir = pwd
@@ -75,6 +77,7 @@ func NewDefaultGenerator(dir string, cfg *config.Config, opt ...Option) (*defaul
return generator, nil
}
// WithConsoleOption creates a console option
func WithConsoleOption(c console.Console) Option {
return func(generator *defaultGenerator) {
generator.Console = c
@@ -189,6 +192,7 @@ func (g *defaultGenerator) genFromDDL(source string, withCache bool) (map[string
return m, nil
}
// Table defines mysql table
type Table struct {
parser.Table
CacheKey map[string]Key

View File

@@ -94,7 +94,7 @@ func TestWrapWithRawString(t *testing.T) {
func TestFields(t *testing.T) {
type Student struct {
Id int64 `db:"id"`
ID int64 `db:"id"`
Name string `db:"name"`
Age sql.NullInt64 `db:"age"`
Score sql.NullFloat64 `db:"score"`

View File

@@ -8,17 +8,22 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
)
// tableName:user
// {{prefix}}=cache
// key:id
// Key defines cache key variable for generating code
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)
// VarExpression likes cacheUserIdPrefix = "cache#User#id#"
VarExpression string
// Left likes cacheUserIdPrefix
Left string
// Right likes cache#user#id#
Right string
// Variable likes userIdKey
Variable string
// KeyExpression likes userIdKey: = fmt.Sprintf("cache#user#id#%v", userId)
KeyExpression string
// DataKeyExpression likes userIdKey: = fmt.Sprintf("cache#user#id#%v", data.userId)
DataKeyExpression string
// RespKeyExpression likes userIdKey: = fmt.Sprintf("cache#user#id#%v", resp.userId)
RespKeyExpression string
}
// key-数据库原始字段名,value-缓存key相关数据

View File

@@ -54,18 +54,22 @@ var templates = map[string]string{
errTemplateFile: template.Error,
}
// Category returns model const value
func Category() string {
return category
}
// Clean deletes all template files
func Clean() error {
return util.Clean(category)
}
// GenTemplates creates template files if not exists
func GenTemplates(_ *cli.Context) error {
return util.InitTemplates(category, templates)
}
// RevertTemplate recovers the delete template files
func RevertTemplate(name string) error {
content, ok := templates[name]
if !ok {
@@ -75,6 +79,7 @@ func RevertTemplate(name string) error {
return util.CreateTemplate(category, name, content)
}
// Update provides template clean and init
func Update() error {
err := Clean()
if err != nil {