day day up

master
谢小军 5 years ago
parent 0a32dfe0e3
commit 204164e8b9

@ -2,7 +2,7 @@ base:
is_dev : false is_dev : false
out_dir : ./model # 输出目录 out_dir : ./model # 输出目录
singular_table : false # 单表模式:true:禁用表名复数,false:采用表明复数 参考:gorm.SingularTable singular_table : false # 单表模式:true:禁用表名复数,false:采用表明复数 参考:gorm.SingularTable
simple : false # 简单输出(默认gorm标签不输出) simple : true # 简单输出(默认gorm标签不输出)
is_out_sql : false # 是否输出 sql 原信息 is_out_sql : false # 是否输出 sql 原信息
is_json_tag : true # 是否打json标记 is_json_tag : true # 是否打json标记
is_foreign_key : true # 是否导出外键关联 is_foreign_key : true # 是否导出外键关联

@ -5,6 +5,7 @@ var EImportsHead = map[string]string{
"stirng": `"string"`, "stirng": `"string"`,
"time.Time": `"time"`, "time.Time": `"time"`,
"gorm.Model": `"github.com/jinzhu/gorm"`, "gorm.Model": `"github.com/jinzhu/gorm"`,
"fmt": `"fmt"`,
} }
// TypeMysqlDicMp Accurate matching type.精确匹配类型 // TypeMysqlDicMp Accurate matching type.精确匹配类型

@ -46,4 +46,23 @@ func (f optionFunc) apply(o *options) {
f(o) 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}}"
}
`
) )

@ -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 err = obj.DB.Table(obj.GetTableName()).Where(options.query).Find(&results).Error
if err == nil && obj.isRelated { if err == nil && obj.isRelated {
for i := 0; i < len(results); i++ { for i := 0; i < len(results); i++ {
var userList []User var userList []User
@ -162,9 +163,7 @@ func (obj *_ExampleMgr) GetByOptions(opts ...Option) (results []*Example, err er
// WithID id获取 // WithID id获取
func (obj *_ExampleMgr) WithID(id int64) Option { func (obj *_ExampleMgr) WithID(id int64) Option {
return optionFunc(func(o *options) { return optionFunc(func(o *options) { o.query["id"] = id })
o.query["id"] = id
})
} }
func (obj *_ExampleMgr) WithUserID(id int64) Option { func (obj *_ExampleMgr) WithUserID(id int64) Option {

@ -7,5 +7,5 @@ func GetGenBaseTemp() string {
// GetGenLogicTemp get gen logic template str // GetGenLogicTemp get gen logic template str
func GetGenLogicTemp() string { func GetGenLogicTemp() string {
return "" return genlogic
} }

@ -176,6 +176,11 @@ func (p *GenPackage) Generate() string {
return strOut return strOut
} }
// AddFuncStr add func coding string.添加函数串
func (p *GenPackage) AddFuncStr(src string) {
p.FuncStrList = append(p.FuncStrList, src)
}
// compensate and import .获取结果数据 // compensate and import .获取结果数据
func (p *GenPackage) genimport() { func (p *GenPackage) genimport() {
for _, v := range p.Structs { for _, v := range p.Structs {

@ -1,9 +1,6 @@
package gtools package gtools
import ( import (
"fmt"
"os/exec"
"github.com/xxjwxc/gormt/data/view/model" "github.com/xxjwxc/gormt/data/view/model"
"github.com/xxjwxc/gormt/data/config" "github.com/xxjwxc/gormt/data/config"
@ -31,12 +28,12 @@ func Execute() {
path := config.GetOutDir() + "/" + v.FileName path := config.GetOutDir() + "/" + v.FileName
tools.WriteFile(path, []string{v.FileCtx}, true) tools.WriteFile(path, []string{v.FileCtx}, true)
fmt.Println("formatting differs from goimport's:") // fmt.Println("formatting differs from goimport's:")
cmd, _ := exec.Command("goimports", "-l", "-w", path).Output() // cmd, _ := exec.Command("goimports", "-l", "-w", path).Output()
fmt.Println(string(cmd)) // fmt.Println(string(cmd))
fmt.Println("formatting differs from gofmt's:") // fmt.Println("formatting differs from gofmt's:")
cmd, _ = exec.Command("gofmt", "-l", "-w", path).Output() // cmd, _ = exec.Command("gofmt", "-l", "-w", path).Output()
fmt.Println(string(cmd)) // fmt.Println(string(cmd))
} }
} }

@ -54,3 +54,25 @@ func getUninStr(left, middle, right string) string {
} }
return re 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
}

@ -6,6 +6,8 @@ import (
"strings" "strings"
"text/template" "text/template"
"github.com/xxjwxc/gormt/data/view/cnf"
"github.com/xxjwxc/public/mybigcamel" "github.com/xxjwxc/public/mybigcamel"
"github.com/xxjwxc/gormt/data/config" "github.com/xxjwxc/gormt/data/config"
@ -203,14 +205,39 @@ func (m *_Model) generateFunc() (genOut []GenOutInfo) {
for _, tab := range m.info.TabList { for _, tab := range m.info.TabList {
var pkg genstruct.GenPackage var pkg genstruct.GenPackage
pkg.SetPackage(m.info.PackageName) //package name 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()) for _, el := range tab.Em {
// if err != nil { if strings.EqualFold(el.Type, "gorm.Model") {
// panic(err) data.Em = append(data.Em, getGormModelElement()...)
// } } else {
// var buf bytes.Buffer data.Em = append(data.Em, el)
// tmpl.Execute(&buf, m.info) 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{ genOut = append(genOut, GenOutInfo{
FileName: fmt.Sprintf("gen.%v.go", tab.Name), FileName: fmt.Sprintf("gen.%v.go", tab.Name),
FileCtx: pkg.Generate(), FileCtx: pkg.Generate(),

Loading…
Cancel
Save