@@ -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+ }
0 commit comments