Skip to content

Commit a6b2df4

Browse files
authored
Merge pull request #6 from luohuixi/main
111
2 parents fb25fb2 + 751de45 commit a6b2df4

16 files changed

Lines changed: 382 additions & 469 deletions

File tree

tool/curd/create/create.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import (
66
"strings"
77
"text/template"
88
"unicode"
9+
10+
"github.com/muxi-Infra/muxi-micro/tool/curd/parse"
911
)
1012

11-
func CreateVar(pkg, dir string, open, cover bool) error {
13+
func CreateVar(pkg, table, dir string, open, cover bool) error {
1214
if cover == false && !CheckExist(dir, "var.go") {
1315
return nil
1416
}
@@ -34,8 +36,10 @@ func CreateVar(pkg, dir string, open, cover bool) error {
3436

3537
data := struct {
3638
PackageName string
39+
ModelName string
3740
}{
3841
PackageName: pkg,
42+
ModelName: table,
3943
}
4044

4145
if err := t.ExecuteTemplate(file, "var", data); err != nil {
@@ -84,7 +88,7 @@ func CreateExample(pkg, dir, table string, open, cover bool) error {
8488
return nil
8589
}
8690

87-
func CreateExample_gen(pkg, dir, table string, fields []string, open bool) error {
91+
func CreateExample_gen(pkg, dir, table string, fields []parse.FieldInfo, open bool) error {
8892
var tmplPath []string
8993
if open {
9094
tmplPath = []string{
@@ -115,15 +119,15 @@ func CreateExample_gen(pkg, dir, table string, fields []string, open bool) error
115119
data := struct {
116120
PackageName string
117121
ModelName string
118-
Fields []string
119-
NotPrs []string
122+
Fields []parse.FieldInfo
123+
NotPrs []parse.FieldInfo
120124
Pr string
121125
}{
122126
PackageName: pkg,
123127
ModelName: table,
124128
Fields: fields,
125129
NotPrs: fields[:len(fields)-1],
126-
Pr: fields[len(fields)-1],
130+
Pr: fields[len(fields)-1].Name,
127131
}
128132

129133
if err := t.ExecuteTemplate(file, "header", data); err != nil {
@@ -133,6 +137,38 @@ func CreateExample_gen(pkg, dir, table string, fields []string, open bool) error
133137
return nil
134138
}
135139

140+
func CreateTranscation(pkg, dir string, transcation bool) error {
141+
if transcation == false {
142+
return nil
143+
}
144+
var tmplPath string
145+
tmplPath = filepath.Join("curd", "template", "transaction.tpl")
146+
t, err := template.New("header").ParseFiles(tmplPath)
147+
if err != nil {
148+
return err
149+
}
150+
151+
outputPath := filepath.Join(dir, "transaction.go")
152+
file, err := os.Create(outputPath)
153+
if err != nil {
154+
return err
155+
}
156+
157+
defer file.Close()
158+
159+
data := struct {
160+
PackageName string
161+
}{
162+
PackageName: pkg,
163+
}
164+
165+
if err := t.ExecuteTemplate(file, "transaction", data); err != nil {
166+
return err
167+
}
168+
169+
return nil
170+
}
171+
136172
func safeFilename(tableName string) string {
137173
return strings.Map(func(r rune) rune {
138174
if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_' || r == '-' {

tool/curd/curd.go

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package curd
22

33
import (
4-
"fmt"
4+
"os"
5+
"path/filepath"
6+
57
"github.com/muxi-Infra/muxi-micro/tool/curd/create"
68
"github.com/muxi-Infra/muxi-micro/tool/curd/parse"
79
"github.com/spf13/cobra"
8-
"os"
9-
"path/filepath"
1010
)
1111

1212
func InitCurdCobra() *cobra.Command {
@@ -21,27 +21,39 @@ 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")
2425

2526
modelPath := filepath.Join(dir, "model.go")
2627
if _, err := os.Stat(modelPath); err != nil {
2728
return err
2829
}
29-
table, index, err := parse.ParseStruct(modelPath)
30+
structs, err := parse.ParseStructs(modelPath)
3031
if err != nil {
3132
return err
3233
}
3334

34-
err = create.CreateExample_gen(pkg, dir, table, index, cache)
35-
if err != nil {
36-
return err
37-
}
38-
err = create.CreateExample(pkg, dir, table, cache, cover)
39-
if err != nil {
40-
return err
41-
}
42-
err = create.CreateVar(pkg, dir, cache, cover)
43-
if err != nil {
44-
fmt.Println(err)
35+
for _, v := range structs {
36+
var index []parse.FieldInfo
37+
for _, vv := range v.Index {
38+
index = append(index, vv)
39+
}
40+
index = append(index, v.Primary[0])
41+
err = create.CreateExample_gen(pkg, dir, v.Name, index, cache)
42+
if err != nil {
43+
return err
44+
}
45+
err = create.CreateExample(pkg, dir, v.Name, cache, cover)
46+
if err != nil {
47+
return err
48+
}
49+
err = create.CreateVar(pkg, v.Name, dir, cache, cover)
50+
if err != nil {
51+
return err
52+
}
53+
err = create.CreateTranscation(pkg, dir, transcation)
54+
if err != nil {
55+
return err
56+
}
4557
}
4658
return nil
4759
},
@@ -51,6 +63,7 @@ func InitCurdCobra() *cobra.Command {
5163
curdCmd.Flags().String("dir", ".", "model文件以及文件生成目录")
5264
curdCmd.Flags().Bool("cache", false, "是否开启缓存")
5365
curdCmd.Flags().Bool("cover", false, "是否覆盖除 _gen.go 外的另外两个文件")
66+
curdCmd.Flags().Bool("transcation", false, "是否生成事务代码")
5467

5568
return curdCmd
5669
}

tool/curd/example/UserModel.go

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

0 commit comments

Comments
 (0)