From 204164e8b9375ed26bb4fd25018da5dbdf80e74a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E5=B0=8F=E5=86=9B?= <346944475@qq.com> Date: Wed, 8 Jan 2020 20:21:30 +0800 Subject: [PATCH] day day up --- config.yml | 2 +- data/view/cnf/def.go | 1 + data/view/genfunc/def.go | 19 +++++++++++++++++ data/view/genfunc/gen.logic.go | 5 ++--- data/view/genfunc/genfunc.go | 2 +- data/view/genstruct/common.go | 5 +++++ data/view/gtools/gtools.go | 15 ++++++------- data/view/model/common.go | 22 +++++++++++++++++++ data/view/model/model.go | 39 ++++++++++++++++++++++++++++------ 9 files changed, 90 insertions(+), 20 deletions(-) diff --git a/config.yml b/config.yml index f75f49a..ddf146a 100644 --- a/config.yml +++ b/config.yml @@ -2,7 +2,7 @@ base: is_dev : false out_dir : ./model # 输出目录 singular_table : false # 单表模式:true:禁用表名复数,false:采用表明复数 参考:gorm.SingularTable -simple : false # 简单输出(默认gorm标签不输出) +simple : true # 简单输出(默认gorm标签不输出) is_out_sql : false # 是否输出 sql 原信息 is_json_tag : true # 是否打json标记 is_foreign_key : true # 是否导出外键关联 diff --git a/data/view/cnf/def.go b/data/view/cnf/def.go index 0cc21c5..c30e4db 100644 --- a/data/view/cnf/def.go +++ b/data/view/cnf/def.go @@ -5,6 +5,7 @@ var EImportsHead = map[string]string{ "stirng": `"string"`, "time.Time": `"time"`, "gorm.Model": `"github.com/jinzhu/gorm"`, + "fmt": `"fmt"`, } // TypeMysqlDicMp Accurate matching type.精确匹配类型 diff --git a/data/view/genfunc/def.go b/data/view/genfunc/def.go index 7e3a6e1..3d1fa32 100644 --- a/data/view/genfunc/def.go +++ b/data/view/genfunc/def.go @@ -46,4 +46,23 @@ func (f optionFunc) apply(o *options) { f(o) } ` + + genlogic = `{{$obj := .}}{{$list := $obj.Em}} +type _{{$obj.StructName}}Mgr struct { + *_BaseMgr +} + +// {{$obj.StructName}}Mgr open func +func {{$obj.StructName}}Mgr(db *gorm.DB) *_{{$obj.StructName}}Mgr { + if db == nil { + panic(fmt.Errorf("{{$obj.StructName}}Mgr init need db")) + } + return &_{{$obj.StructName}}Mgr{_BaseMgr: &_BaseMgr{DB: db}} +} + +// GetTableName get sql table name.获取数据库名字 +func (obj *_{{$obj.StructName}}Mgr) GetTableName() string { + return "{{$obj.TableName}}" +} +` ) diff --git a/data/view/genfunc/gen.logic.go b/data/view/genfunc/gen.logic.go index 9d26043..ef51574 100644 --- a/data/view/genfunc/gen.logic.go +++ b/data/view/genfunc/gen.logic.go @@ -147,6 +147,7 @@ func (obj *_ExampleMgr) GetByOptions(opts ...Option) (results []*Example, err er } err = obj.DB.Table(obj.GetTableName()).Where(options.query).Find(&results).Error + if err == nil && obj.isRelated { for i := 0; i < len(results); i++ { var userList []User @@ -162,9 +163,7 @@ func (obj *_ExampleMgr) GetByOptions(opts ...Option) (results []*Example, err er // WithID id获取 func (obj *_ExampleMgr) WithID(id int64) Option { - return optionFunc(func(o *options) { - o.query["id"] = id - }) + return optionFunc(func(o *options) { o.query["id"] = id }) } func (obj *_ExampleMgr) WithUserID(id int64) Option { diff --git a/data/view/genfunc/genfunc.go b/data/view/genfunc/genfunc.go index 0b7b416..791e89c 100644 --- a/data/view/genfunc/genfunc.go +++ b/data/view/genfunc/genfunc.go @@ -7,5 +7,5 @@ func GetGenBaseTemp() string { // GetGenLogicTemp get gen logic template str func GetGenLogicTemp() string { - return "" + return genlogic } diff --git a/data/view/genstruct/common.go b/data/view/genstruct/common.go index f82d735..a655c41 100644 --- a/data/view/genstruct/common.go +++ b/data/view/genstruct/common.go @@ -176,6 +176,11 @@ func (p *GenPackage) Generate() string { return strOut } +// AddFuncStr add func coding string.添加函数串 +func (p *GenPackage) AddFuncStr(src string) { + p.FuncStrList = append(p.FuncStrList, src) +} + // compensate and import .获取结果数据 func (p *GenPackage) genimport() { for _, v := range p.Structs { diff --git a/data/view/gtools/gtools.go b/data/view/gtools/gtools.go index ad3a4e6..320f0f1 100644 --- a/data/view/gtools/gtools.go +++ b/data/view/gtools/gtools.go @@ -1,9 +1,6 @@ package gtools import ( - "fmt" - "os/exec" - "github.com/xxjwxc/gormt/data/view/model" "github.com/xxjwxc/gormt/data/config" @@ -31,12 +28,12 @@ func Execute() { path := config.GetOutDir() + "/" + v.FileName tools.WriteFile(path, []string{v.FileCtx}, true) - fmt.Println("formatting differs from goimport's:") - cmd, _ := exec.Command("goimports", "-l", "-w", path).Output() - fmt.Println(string(cmd)) + // fmt.Println("formatting differs from goimport's:") + // cmd, _ := exec.Command("goimports", "-l", "-w", path).Output() + // fmt.Println(string(cmd)) - fmt.Println("formatting differs from gofmt's:") - cmd, _ = exec.Command("gofmt", "-l", "-w", path).Output() - fmt.Println(string(cmd)) + // fmt.Println("formatting differs from gofmt's:") + // cmd, _ = exec.Command("gofmt", "-l", "-w", path).Output() + // fmt.Println(string(cmd)) } } diff --git a/data/view/model/common.go b/data/view/model/common.go index be046a8..4ab1eac 100644 --- a/data/view/model/common.go +++ b/data/view/model/common.go @@ -54,3 +54,25 @@ func getUninStr(left, middle, right string) string { } return re } + +func getGormModelElement() []ColumusInfo { + var result []ColumusInfo + result = append(result, ColumusInfo{ + BaseInfo: BaseInfo{Name: "id", Notes: "Primary key"}, + Type: "int64", // Type.类型标记 + Index: []KList{KList{Key: ColumusKeyPrimary}}, // index list.index列表 + }) + result = append(result, ColumusInfo{ + BaseInfo: BaseInfo{Name: "created_at", Notes: "created time"}, + Type: "time.Time", // Type.类型标记 + }) + result = append(result, ColumusInfo{ + BaseInfo: BaseInfo{Name: "updated_at", Notes: "updated time"}, + Type: "time.Time", // Type.类型标记 + }) + result = append(result, ColumusInfo{ + BaseInfo: BaseInfo{Name: "deleted_at", Notes: "deleted time"}, + Type: "time.Time", // Type.类型标记 + }) + return result +} diff --git a/data/view/model/model.go b/data/view/model/model.go index d00e2ca..9ac0312 100644 --- a/data/view/model/model.go +++ b/data/view/model/model.go @@ -6,6 +6,8 @@ import ( "strings" "text/template" + "github.com/xxjwxc/gormt/data/view/cnf" + "github.com/xxjwxc/public/mybigcamel" "github.com/xxjwxc/gormt/data/config" @@ -203,14 +205,39 @@ func (m *_Model) generateFunc() (genOut []GenOutInfo) { for _, tab := range m.info.TabList { var pkg genstruct.GenPackage pkg.SetPackage(m.info.PackageName) //package name + pkg.AddImport(`"github.com/jinzhu/gorm"`) + pkg.AddImport(`"fmt"`) + + data := struct { + StructName string + TableName string + Em []ColumusInfo + }{ + StructName: getCamelName(tab.Name), + TableName: tab.Name, + } - // tmpl, err := template.New("gen_logic").Funcs(template.FuncMap{"GetStringList": GetStringList}).Parse(genfunc.GetGenBaseTemp()) - // if err != nil { - // panic(err) - // } - // var buf bytes.Buffer - // tmpl.Execute(&buf, m.info) + for _, el := range tab.Em { + if strings.EqualFold(el.Type, "gorm.Model") { + data.Em = append(data.Em, getGormModelElement()...) + } else { + data.Em = append(data.Em, el) + if v2, ok := cnf.EImportsHead[el.Type]; ok { + if len(v2) > 0 { + pkg.AddImport(v2) + } + } + } + } + + tmpl, err := template.New("gen_logic").Parse(genfunc.GetGenLogicTemp()) + if err != nil { + panic(err) + } + var buf bytes.Buffer + tmpl.Execute(&buf, data) + pkg.AddFuncStr(buf.String()) genOut = append(genOut, GenOutInfo{ FileName: fmt.Sprintf("gen.%v.go", tab.Name), FileCtx: pkg.Generate(),