func export support
支持快捷函数导出。
This commit is contained in:
@@ -55,24 +55,37 @@ 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列表
|
||||
func getGormModelElement() []EmInfo {
|
||||
var result []EmInfo
|
||||
result = append(result, EmInfo{
|
||||
IsMulti: false,
|
||||
Notes: "Primary key",
|
||||
Type: "int64", // Type.类型标记
|
||||
ColName: "id",
|
||||
ColStructName: "ID",
|
||||
})
|
||||
result = append(result, ColumusInfo{
|
||||
BaseInfo: BaseInfo{Name: "created_at", Notes: "created time"},
|
||||
Type: "time.Time", // Type.类型标记
|
||||
result = append(result, EmInfo{
|
||||
IsMulti: false,
|
||||
Notes: "created time",
|
||||
Type: "time.Time", // Type.类型标记
|
||||
ColName: "created_at",
|
||||
ColStructName: "CreatedAt",
|
||||
})
|
||||
result = append(result, ColumusInfo{
|
||||
BaseInfo: BaseInfo{Name: "updated_at", Notes: "updated time"},
|
||||
Type: "time.Time", // Type.类型标记
|
||||
|
||||
result = append(result, EmInfo{
|
||||
IsMulti: false,
|
||||
Notes: "updated at",
|
||||
Type: "time.Time", // Type.类型标记
|
||||
ColName: "updated_at",
|
||||
ColStructName: "UpdatedAt",
|
||||
})
|
||||
result = append(result, ColumusInfo{
|
||||
BaseInfo: BaseInfo{Name: "deleted_at", Notes: "deleted time"},
|
||||
Type: "time.Time", // Type.类型标记
|
||||
|
||||
result = append(result, EmInfo{
|
||||
IsMulti: false,
|
||||
Notes: "deleted time",
|
||||
Type: "time.Time", // Type.类型标记
|
||||
ColName: "deleted_at",
|
||||
ColStructName: "DeletedAt",
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -67,3 +67,34 @@ type GenOutInfo struct {
|
||||
FileName string // output file name .输出文件名
|
||||
FileCtx string // output file context. 输出文件内容
|
||||
}
|
||||
|
||||
// def func sturct
|
||||
|
||||
// PreloadInfo 预加载列表
|
||||
type PreloadInfo struct {
|
||||
IsMulti bool
|
||||
Notes string // 注释
|
||||
ForeignkeyStructName string // 外键类目
|
||||
ForeignkeyTableName string // 外键表名
|
||||
ForeignkeyCol string // 外键列表
|
||||
ColName string // 表名
|
||||
ColStructName string // 表结构体
|
||||
}
|
||||
|
||||
// EmInfo func 表结构定义
|
||||
type EmInfo struct {
|
||||
IsMulti bool
|
||||
Notes string // 注释
|
||||
Type string // 类型
|
||||
ColName string // 表名
|
||||
ColStructName string // 表结构体
|
||||
}
|
||||
|
||||
type funDef struct {
|
||||
StructName string
|
||||
TableName string
|
||||
PreloadList []PreloadInfo
|
||||
Em []EmInfo
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@@ -208,11 +208,7 @@ func (m *_Model) generateFunc() (genOut []GenOutInfo) {
|
||||
pkg.AddImport(`"github.com/jinzhu/gorm"`)
|
||||
pkg.AddImport(`"fmt"`)
|
||||
|
||||
data := struct {
|
||||
StructName string
|
||||
TableName string
|
||||
Em []ColumusInfo
|
||||
}{
|
||||
data := funDef{
|
||||
StructName: getCamelName(tab.Name),
|
||||
TableName: tab.Name,
|
||||
}
|
||||
@@ -220,17 +216,57 @@ func (m *_Model) generateFunc() (genOut []GenOutInfo) {
|
||||
for _, el := range tab.Em {
|
||||
if strings.EqualFold(el.Type, "gorm.Model") {
|
||||
data.Em = append(data.Em, getGormModelElement()...)
|
||||
pkg.AddImport(`"time"`)
|
||||
} else {
|
||||
data.Em = append(data.Em, el)
|
||||
if v2, ok := cnf.EImportsHead[el.Type]; ok {
|
||||
isMulti := true
|
||||
for _, v1 := range el.Index {
|
||||
switch v1.Key {
|
||||
// case ColumusKeyDefault:
|
||||
case ColumusKeyPrimary: // primary key.主键
|
||||
isMulti = false
|
||||
case ColumusKeyUnique: // unique key.唯一索引
|
||||
isMulti = false
|
||||
//case ColumusKeyIndex: // index key.复合索引
|
||||
case ColumusKeyUniqueIndex: // unique index key.唯一复合索引
|
||||
isMulti = false
|
||||
}
|
||||
}
|
||||
|
||||
typeName := getTypeName(el.Type)
|
||||
data.Em = append(data.Em, EmInfo{
|
||||
IsMulti: isMulti,
|
||||
Notes: el.Notes,
|
||||
Type: typeName, // Type.类型标记
|
||||
ColName: el.Name,
|
||||
ColStructName: getCamelName(el.Name),
|
||||
})
|
||||
if v2, ok := cnf.EImportsHead[typeName]; ok {
|
||||
if len(v2) > 0 {
|
||||
pkg.AddImport(v2)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 外键列表
|
||||
for _, v := range el.ForeignKeyList {
|
||||
isMulti, isFind, notes := m.getColumusKeyMulti(v.TableName, v.ColumnName)
|
||||
if isFind {
|
||||
var info PreloadInfo
|
||||
info.IsMulti = isMulti
|
||||
info.Notes = notes
|
||||
info.ForeignkeyTableName = v.TableName
|
||||
info.ForeignkeyCol = v.ColumnName
|
||||
info.ForeignkeyStructName = getCamelName(v.TableName)
|
||||
info.ColName = el.Name
|
||||
info.ColStructName = getCamelName(el.Name)
|
||||
data.PreloadList = append(data.PreloadList, info)
|
||||
}
|
||||
}
|
||||
// ---------end--
|
||||
|
||||
}
|
||||
|
||||
tmpl, err := template.New("gen_logic").Parse(genfunc.GetGenLogicTemp())
|
||||
tmpl, err := template.New("gen_logic").Funcs(template.FuncMap{"GenPreloadList": GenPreloadList}).Parse(genfunc.GetGenLogicTemp())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -239,10 +275,26 @@ func (m *_Model) generateFunc() (genOut []GenOutInfo) {
|
||||
|
||||
pkg.AddFuncStr(buf.String())
|
||||
genOut = append(genOut, GenOutInfo{
|
||||
FileName: fmt.Sprintf("gen.%v.go", tab.Name),
|
||||
FileName: fmt.Sprintf(m.info.DbName+".gen.%v.go", tab.Name),
|
||||
FileCtx: pkg.Generate(),
|
||||
})
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GenPreloadList 生成list
|
||||
func GenPreloadList(list []PreloadInfo, multi bool) string {
|
||||
if len(list) > 0 {
|
||||
tmpl, err := template.New("gen_preload").Parse(genfunc.GetGenPreloadTemp(multi))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
tmpl.Execute(&buf, list)
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user