Skip to content

Commit 8111851

Browse files
committed
fix: 将curd的封装删除,太冗余了
1 parent 508ce78 commit 8111851

15 files changed

Lines changed: 240 additions & 395 deletions

File tree

pkg/sql/cache.go

Lines changed: 0 additions & 56 deletions
This file was deleted.

pkg/sql/sql.go

Lines changed: 0 additions & 77 deletions
This file was deleted.

tool/curd/create/create.go

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,26 @@ import (
88
"unicode"
99

1010
"github.com/muxi-Infra/muxi-micro/tool/curd/parse"
11+
"gorm.io/gorm/schema"
1112
)
1213

13-
func CreateVar(pkg, table, dir string, open, cover bool) error {
14-
if cover == false && !CheckExist(dir, "var.go") {
14+
func CreateExample(pkg, dir, table string, open, cover bool) error {
15+
if cover == false && !CheckExist(dir, safeFilename(table)+"model.go") {
1516
return nil
1617
}
1718
var tmplPath string
1819
if open {
19-
tmplPath = filepath.Join("curd", "template", "with_cache", "var.tpl")
20+
tmplPath = filepath.Join("curd", "template", "with_cache", "example.tpl")
2021
} else {
21-
tmplPath = filepath.Join("curd", "template", "no_cache", "var.tpl")
22+
tmplPath = filepath.Join("curd", "template", "no_cache", "example.tpl")
2223
}
2324

24-
t, err := template.New("var").ParseFiles(tmplPath)
25+
t, err := template.New("example").ParseFiles(tmplPath)
2526
if err != nil {
2627
return err
2728
}
2829

29-
outputPath := filepath.Join(dir, "var.go")
30+
outputPath := filepath.Join(dir, safeFilename(table)+"model.go")
3031
file, err := os.Create(outputPath)
3132
if err != nil {
3233
return err
@@ -42,73 +43,80 @@ func CreateVar(pkg, table, dir string, open, cover bool) error {
4243
ModelName: table,
4344
}
4445

45-
if err := t.ExecuteTemplate(file, "var", data); err != nil {
46+
if err := t.ExecuteTemplate(file, "example", data); err != nil {
4647
return err
4748
}
4849

4950
return nil
5051
}
5152

52-
func CreateExample(pkg, dir, table string, open, cover bool) error {
53-
if cover == false && !CheckExist(dir, safeFilename(table)+"model.go") {
54-
return nil
55-
}
56-
var tmplPath string
53+
func CreateExample_gen(pkg, dir, table string, fields []parse.FieldInfo, open bool) error {
54+
var tmplPath []string
5755
if open {
58-
tmplPath = filepath.Join("curd", "template", "with_cache", "example.tpl")
56+
tmplPath = []string{
57+
filepath.Join("curd", "template", "with_cache", "header.tpl"),
58+
filepath.Join("curd", "template", "with_cache", "cache.tpl"),
59+
filepath.Join("curd", "template", "with_cache", "db.tpl"),
60+
}
5961
} else {
60-
tmplPath = filepath.Join("curd", "template", "no_cache", "example.tpl")
62+
tmplPath = []string{
63+
filepath.Join("curd", "template", "no_cache", "header.tpl"),
64+
filepath.Join("curd", "template", "no_cache", "db.tpl"),
65+
}
6166
}
6267

63-
t, err := template.New("example").ParseFiles(tmplPath)
68+
t, err := template.New("header").ParseFiles(tmplPath...)
6469
if err != nil {
6570
return err
6671
}
6772

68-
outputPath := filepath.Join(dir, safeFilename(table)+"model.go")
73+
outputPath := filepath.Join(dir, safeFilename(table)+"model_gen.go")
6974
file, err := os.Create(outputPath)
7075
if err != nil {
7176
return err
7277
}
7378

7479
defer file.Close()
7580

81+
gormNames := gormName(fields)
82+
7683
data := struct {
7784
PackageName string
7885
ModelName string
86+
Fields []parse.FieldInfo
87+
NotPrs []parse.FieldInfo
88+
Pr string
89+
GNotPrs []string
90+
GPr string
7991
}{
8092
PackageName: pkg,
8193
ModelName: table,
94+
Fields: fields,
95+
NotPrs: fields[:len(fields)-1],
96+
Pr: fields[len(fields)-1].Name,
97+
GNotPrs: gormNames[:len(gormNames)-1],
98+
GPr: gormNames[len(gormNames)-1],
8299
}
83100

84-
if err := t.ExecuteTemplate(file, "example", data); err != nil {
101+
if err := t.ExecuteTemplate(file, "header", data); err != nil {
85102
return err
86103
}
87104

88105
return nil
89106
}
90107

91-
func CreateExample_gen(pkg, dir, table string, fields []parse.FieldInfo, open bool) error {
92-
var tmplPath []string
93-
if open {
94-
tmplPath = []string{
95-
filepath.Join("curd", "template", "with_cache", "header.tpl"),
96-
filepath.Join("curd", "template", "with_cache", "cache.tpl"),
97-
filepath.Join("curd", "template", "with_cache", "db.tpl"),
98-
}
99-
} else {
100-
tmplPath = []string{
101-
filepath.Join("curd", "template", "no_cache", "header.tpl"),
102-
filepath.Join("curd", "template", "no_cache", "db.tpl"),
103-
}
108+
func CreateCache(pkg, dir, table string, fields []parse.FieldInfo, open bool) error {
109+
if !open {
110+
return nil
104111
}
112+
tmplPath := filepath.Join("curd", "template", "with_cache", "cache.tpl")
105113

106-
t, err := template.New("header").ParseFiles(tmplPath...)
114+
t, err := template.New("cache").ParseFiles(tmplPath)
107115
if err != nil {
108116
return err
109117
}
110118

111-
outputPath := filepath.Join(dir, safeFilename(table)+"model_gen.go")
119+
outputPath := filepath.Join(dir, safeFilename(table)+"cache.go")
112120
file, err := os.Create(outputPath)
113121
if err != nil {
114122
return err
@@ -119,18 +127,16 @@ func CreateExample_gen(pkg, dir, table string, fields []parse.FieldInfo, open bo
119127
data := struct {
120128
PackageName string
121129
ModelName string
122-
Fields []parse.FieldInfo
123130
NotPrs []parse.FieldInfo
124131
Pr string
125132
}{
126133
PackageName: pkg,
127134
ModelName: table,
128-
Fields: fields,
129135
NotPrs: fields[:len(fields)-1],
130136
Pr: fields[len(fields)-1].Name,
131137
}
132138

133-
if err := t.ExecuteTemplate(file, "header", data); err != nil {
139+
if err := t.ExecuteTemplate(file, "cache", data); err != nil {
134140
return err
135141
}
136142

@@ -185,3 +191,15 @@ func CheckExist(dir, filename string) bool {
185191
}
186192
return false
187193
}
194+
195+
// 自动迁移特殊字段处理
196+
func gormName(f []parse.FieldInfo) []string {
197+
var gormNames []string
198+
namingStrategy := schema.NamingStrategy{}
199+
200+
for _, fi := range f {
201+
gormNames = append(gormNames, namingStrategy.ColumnName("", fi.Name))
202+
}
203+
204+
return gormNames
205+
}

tool/curd/curd.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func InitCurdCobra() *cobra.Command {
2121
dir, _ := cmd.Flags().GetString("dir")
2222
cache, _ := cmd.Flags().GetBool("cache")
2323
cover, _ := cmd.Flags().GetBool("cover")
24-
transcation, _ := cmd.Flags().GetBool("transcation")
24+
transaction, _ := cmd.Flags().GetBool("transaction")
2525

2626
modelPath := filepath.Join(dir, "model.go")
2727
if _, err := os.Stat(modelPath); err != nil {
@@ -46,11 +46,11 @@ func InitCurdCobra() *cobra.Command {
4646
if err != nil {
4747
return err
4848
}
49-
err = create.CreateVar(pkg, v.Name, dir, cache, cover)
49+
err = create.CreateCache(pkg, dir, v.Name, index, cache)
5050
if err != nil {
5151
return err
5252
}
53-
err = create.CreateTranscation(pkg, dir, transcation)
53+
err = create.CreateTranscation(pkg, dir, transaction)
5454
if err != nil {
5555
return err
5656
}
@@ -63,7 +63,7 @@ func InitCurdCobra() *cobra.Command {
6363
curdCmd.Flags().String("dir", ".", "model文件以及文件生成目录")
6464
curdCmd.Flags().Bool("cache", false, "是否开启缓存")
6565
curdCmd.Flags().Bool("cover", false, "是否覆盖除 _gen.go 外的另外两个文件")
66-
curdCmd.Flags().Bool("transcation", false, "是否生成事务代码")
66+
curdCmd.Flags().Bool("transaction", false, "是否生成事务代码")
6767

6868
return curdCmd
6969
}

tool/curd/example/model.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package repository
22

33
type User struct {
4-
Id int64 `gorm:"primaryKey;autoIncrement"`
5-
Username string `gorm:"size:50;unique;"`
4+
ID int64 `gorm:"primaryKey;autoIncrement"`
5+
UserName string `gorm:"size:50;unique;"`
66
Password string `db:"password"`
77
Mobile string `gorm:"size:30;index:idx_name"`
88
}

0 commit comments

Comments
 (0)