-
Notifications
You must be signed in to change notification settings - Fork 6
/
golang_session.go
executable file
·109 lines (99 loc) · 2.94 KB
/
golang_session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package tto
import (
"bytes"
"fmt"
"sync"
)
type (
GoSession interface {
// 生成Go仓储代码
GenerateGoRepoCodes(tables []*Table, targetDir string) (err error)
}
)
var _ GoSession = new(sessionImpl)
// 表生成仓储结构,sign:函数后是否带签名,ePrefix:实体是否带前缀
func (s *sessionImpl) tableToGoRepo(table *Table,
sign bool, ePrefix string) (string, string) {
tpl := GoEntityRepTemplate
path, _ := s.predefineTargetPath(tpl, table)
return s.GenerateCode(table, GoEntityRepTemplate, nil), path
}
// 表生成仓库仓储接口
func (s *sessionImpl) tableToGoIRepo(table *Table,
sign bool, ePrefix string) (string, string) {
tpl := GoEntityRepIfceTemplate
path, _ := s.predefineTargetPath(tpl, table)
return s.GenerateCode(table, tpl, nil), path
}
// 表生成结构
func (s *sessionImpl) tableToGoStruct(table *Table) (string, string) {
goPath := fmt.Sprintf("%s/model/%s.go", s.codeVars[PKG], table.Name)
if table == nil {
return "", goPath
}
pkgName := "model"
buf := bytes.NewBufferString("")
buf.WriteString("package ")
buf.WriteString(pkgName)
buf.WriteString("\n// ")
buf.WriteString(table.Comment)
buf.WriteString("\ntype ")
buf.WriteString(title(table.Name, s.useUpperId))
buf.WriteString(" struct{\n")
fn := internalFunc{}
for _, col := range table.Columns {
if col.Comment != "" {
buf.WriteString(" // ")
buf.WriteString(col.Comment)
buf.WriteString("\n")
}
buf.WriteString(" ")
buf.WriteString(title(col.Name, s.useUpperId))
buf.WriteString(" ")
buf.WriteString(fn.langType("go", col.Type))
buf.WriteString(" `")
buf.WriteString("db:\"")
buf.WriteString(col.Name)
buf.WriteString("\"")
if col.IsPk {
buf.WriteString(" pk:\"yes\"")
}
if col.IsAuto {
buf.WriteString(" auto:\"yes\"")
}
buf.WriteString("`")
buf.WriteString("\n")
}
buf.WriteString("}")
return buf.String(), goPath
}
// 生成Go仓储代码
func (s *sessionImpl) GenerateGoRepoCodes(tables []*Table, targetDir string) (err error) {
wg := sync.WaitGroup{}
for _, table := range tables {
wg.Add(1)
go func(wg *sync.WaitGroup, tb *Table) {
defer wg.Done()
//生成实体
str, path := s.tableToGoStruct(tb)
if err = SaveFile(str, targetDir+"/"+path); err != nil {
println(fmt.Sprintf("[ tto][ error]: save file failed! %s", err.Error()))
}
//生成仓储结构
str, path = s.tableToGoRepo(tb, true, "")
if err = SaveFile(str, targetDir+"/"+path); err != nil {
println(fmt.Sprintf("[ tto][ error]: save file failed! %s", err.Error()))
}
//生成仓储接口
str, path = s.tableToGoIRepo(tb, true, "")
if err = SaveFile(str, targetDir+"/"+path); err != nil {
println(fmt.Sprintf("[ tto][ error]: save file failed! %s", err.Error()))
}
}(&wg, table)
}
wg.Wait()
// 生成仓储工厂
code := s.GenerateCodeByTables(tables, GoRepoFactoryTemplate, nil)
path, _ := s.predefineTargetPath(GoRepoFactoryTemplate, nil)
return SaveFile(code, targetDir+"/"+path)
}