forked from muxi-Infra/muxi-micro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.go
More file actions
187 lines (156 loc) · 3.73 KB
/
create.go
File metadata and controls
187 lines (156 loc) · 3.73 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package create
import (
"os"
"path/filepath"
"strings"
"text/template"
"unicode"
"github.com/muxi-Infra/muxi-micro/tool/curd/parse"
)
func CreateVar(pkg, table, dir string, open, cover bool) error {
if cover == false && !CheckExist(dir, "var.go") {
return nil
}
var tmplPath string
if open {
tmplPath = filepath.Join("curd", "template", "with_cache", "var.tpl")
} else {
tmplPath = filepath.Join("curd", "template", "no_cache", "var.tpl")
}
t, err := template.New("var").ParseFiles(tmplPath)
if err != nil {
return err
}
outputPath := filepath.Join(dir, "var.go")
file, err := os.Create(outputPath)
if err != nil {
return err
}
defer file.Close()
data := struct {
PackageName string
ModelName string
}{
PackageName: pkg,
ModelName: table,
}
if err := t.ExecuteTemplate(file, "var", data); err != nil {
return err
}
return nil
}
func CreateExample(pkg, dir, table string, open, cover bool) error {
if cover == false && !CheckExist(dir, safeFilename(table)+"model.go") {
return nil
}
var tmplPath string
if open {
tmplPath = filepath.Join("curd", "template", "with_cache", "example.tpl")
} else {
tmplPath = filepath.Join("curd", "template", "no_cache", "example.tpl")
}
t, err := template.New("example").ParseFiles(tmplPath)
if err != nil {
return err
}
outputPath := filepath.Join(dir, safeFilename(table)+"model.go")
file, err := os.Create(outputPath)
if err != nil {
return err
}
defer file.Close()
data := struct {
PackageName string
ModelName string
}{
PackageName: pkg,
ModelName: table,
}
if err := t.ExecuteTemplate(file, "example", data); err != nil {
return err
}
return nil
}
func CreateExample_gen(pkg, dir, table string, fields []parse.FieldInfo, open bool) error {
var tmplPath []string
if open {
tmplPath = []string{
filepath.Join("curd", "template", "with_cache", "header.tpl"),
filepath.Join("curd", "template", "with_cache", "cache.tpl"),
filepath.Join("curd", "template", "with_cache", "db.tpl"),
}
} else {
tmplPath = []string{
filepath.Join("curd", "template", "no_cache", "header.tpl"),
filepath.Join("curd", "template", "no_cache", "db.tpl"),
}
}
t, err := template.New("header").ParseFiles(tmplPath...)
if err != nil {
return err
}
outputPath := filepath.Join(dir, safeFilename(table)+"model_gen.go")
file, err := os.Create(outputPath)
if err != nil {
return err
}
defer file.Close()
data := struct {
PackageName string
ModelName string
Fields []parse.FieldInfo
NotPrs []parse.FieldInfo
Pr string
}{
PackageName: pkg,
ModelName: table,
Fields: fields,
NotPrs: fields[:len(fields)-1],
Pr: fields[len(fields)-1].Name,
}
if err := t.ExecuteTemplate(file, "header", data); err != nil {
return err
}
return nil
}
func CreateTranscation(pkg, dir string, transcation bool) error {
if transcation == false {
return nil
}
var tmplPath string
tmplPath = filepath.Join("curd", "template", "transaction.tpl")
t, err := template.New("header").ParseFiles(tmplPath)
if err != nil {
return err
}
outputPath := filepath.Join(dir, "transaction.go")
file, err := os.Create(outputPath)
if err != nil {
return err
}
defer file.Close()
data := struct {
PackageName string
}{
PackageName: pkg,
}
if err := t.ExecuteTemplate(file, "transaction", data); err != nil {
return err
}
return nil
}
func safeFilename(tableName string) string {
return strings.Map(func(r rune) rune {
if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_' || r == '-' {
return r
}
return '_'
}, tableName)
}
func CheckExist(dir, filename string) bool {
path := filepath.Join(dir, filename)
if _, err := os.Stat(path); err != nil {
return true
}
return false
}