-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathformat.go
170 lines (147 loc) · 3.44 KB
/
format.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
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
package genModels
import (
_ "github.com/go-sql-driver/mysql"
"strings"
)
type Format struct {
Framework string
TabFormat string // format must use 3 %s in it, first column name, second property third json name
AutoInfo string
PropertyFormat PropertyFormat // like size s
JsonUseCamel bool
}
type PropertyFormat struct {
Size string
Type string
Index string
Default string
}
//`gorm:"column:beast_id"`
var BeeFormat Format
var DefaultFormat Format
var GormFormat Format
func init() {
BeeFormat = Format{
Framework: "bee",
TabFormat: "`orm:\"column(%s);%s\" json:\"%s\"`",
PropertyFormat: PropertyFormat{
Size: "size(%d)",
Type: "type(%s)",
Index: "%s",
Default: "",
},
AutoInfo: "\nimport \"github.com/astaxie/beego/orm\"\n\nfunc init(){\n\torm.RegisterModel(new({{modelName}}))\n}\n\n",
}
DefaultFormat = Format{
Framework: "default",
TabFormat: "`orm:\"%s;%s\" json:\"%s\"`",
}
GormFormat = Format{
Framework: "gorm",
PropertyFormat: PropertyFormat{
Size: "size:%d",
Type: "type:%s",
Index: "",
Default: "default:%s",
},
TabFormat: "`gorm:\"column:%s;%s\" json:\"%s\"`",
//AutoInfo: "\nimport (\n\t\"fmt\"\n)\n\n",
}
}
func GetFormat(framework string) Format {
switch framework {
case "bee":
return BeeFormat
case "gorm":
return GormFormat
default:
return DefaultFormat
}
}
func (format Format) AutoImport(modelName string) string {
if format.AutoInfo == "" {
return ""
}
return strings.Replace(format.AutoInfo, "{{modelName}}", modelName, -1)
}
func (format Format) GetTabFormat() string {
return format.TabFormat
}
func (format Format) GetPropertyFormat() PropertyFormat {
return format.PropertyFormat
}
func (pf PropertyFormat) GetSizeFormat() string {
return pf.Size
}
func (pf PropertyFormat) GetIndexFormat() string {
return pf.Index
}
func (pf PropertyFormat) GetTypeFormat() string {
return pf.Type
}
func (pf PropertyFormat) GetDefaultFormat() string {
return pf.Default
}
var GormTpl = `
func Get{{object}}ById(id string) ({{entry}} *{{object}}) {
err := Orm.Model({{entry}}).First({{entry}}, {{entry}}.GetKey() + " = '"+id+"'").GetErrors()
if len(err) > 0 {
return nil
}
return
}
func Get{{object}}One(where string, args... interface{}) ({{entry}} *{{object}}) {
err := Orm.Model({{entry}}).First({{entry}}, where, args...).GetErrors()
if len(err) > 0 {
return nil
}
return
}
func Get{{object}}List(page,limit int64, where string, condition interface{}) (list []*{{object}}) {
err := Orm.Model({{entry}}).Limit(limit).Offset((page-1) * limit).Find(list, where, condition).GetErrors()
if err != nil {
return nil
}
return
}
func ({{entry}} *{{object}}) Create() []error {
return Orm.Model({{entry}}).Create({{entry}}).GetErrors()
}
func ({{entry}} *{{object}}) Update(update {{object}}) []error {
return Orm.Model({{entry}}).UpdateColumns(update).GetErrors()
}
func ({{entry}} *{{object}}) Delete() {
Orm.Model({{entry}}).Delete({{entry}})
}
`
var GormInit = `
package {{package}}
import (
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
)
var Orm *gorm.DB
func init() {
db, err := gorm.Open("mysql", "{{dns}}")
if err != nil {
panic("连接数据库失败")
}
Orm = db
}
`
func (format Format) GetFuncTemplate(t string) string {
switch t {
case "gorm":
return GormTpl
default:
return ""
}
}
func (format Format) GetInitTemplate(t string) string {
switch t {
case "gorm":
return GormInit
default:
return ""
}
}