Skip to content

Commit

Permalink
day day up
Browse files Browse the repository at this point in the history
  • Loading branch information
xxjwxc committed Jan 8, 2020
1 parent 0a32dfe commit 204164e
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 20 deletions.
2 changes: 1 addition & 1 deletion config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 # 是否导出外键关联
Expand Down
1 change: 1 addition & 0 deletions data/view/cnf/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.精确匹配类型
Expand Down
19 changes: 19 additions & 0 deletions data/view/genfunc/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}"
}
`
)
5 changes: 2 additions & 3 deletions data/view/genfunc/gen.logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion data/view/genfunc/genfunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ func GetGenBaseTemp() string {

// GetGenLogicTemp get gen logic template str
func GetGenLogicTemp() string {
return ""
return genlogic
}
5 changes: 5 additions & 0 deletions data/view/genstruct/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 6 additions & 9 deletions data/view/gtools/gtools.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package gtools

import (
"fmt"
"os/exec"

"github.com/xxjwxc/gormt/data/view/model"

"github.com/xxjwxc/gormt/data/config"
Expand Down Expand Up @@ -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))
}
}
22 changes: 22 additions & 0 deletions data/view/model/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
39 changes: 33 additions & 6 deletions data/view/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 204164e

Please sign in to comment.