Skip to content

Commit 1872447

Browse files
committed
Add gorm v2 tag generate method
1 parent 9f35c08 commit 1872447

File tree

9 files changed

+132
-64
lines changed

9 files changed

+132
-64
lines changed

README.md

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,11 @@ Generate grom configuration file like this:
7171
"enable_guregu_null": false,
7272
"enable_json_tag": true,
7373
"enable_xml_tag": false,
74-
"enable_gorm_tag": true,
74+
"enable_gorm_tag": false,
7575
"enable_xorm_tag": false,
7676
"enable_beego_tag": false,
77-
"enable_gorose_tag": false
77+
"enable_gorose_tag": false,
78+
"enable_gorm_v2_tag": true
7879
}
7980

8081
Usage:
@@ -95,11 +96,11 @@ Usage:
9596

9697
Examples:
9798
grom convert -n ./grom.json
98-
grom convert -H localhost -P 3306 -u user -p password -d database -t table -e INITIALISM,FIELD_COMMENT,JSON_TAG,GORM_TAG --package PACKAGE_NAME --struct STRUCT_NAME
99+
grom convert -H localhost -P 3306 -u user -p password -d database -t table -e INITIALISM,FIELD_COMMENT,JSON_TAG,GORM_V2_TAG --package PACKAGE_NAME --struct STRUCT_NAME
99100

100101
Flags:
101102
-d, --database string the database of mysql
102-
-e, --enable strings enable services (must in [INITIALISM,FIELD_COMMENT,SQL_NULL,GUREGU_NULL,JSON_TAG,XML_TAG,GORM_TAG,XORM_TAG,BEEGO_TAG,GOROSE_TAG])
103+
-e, --enable strings enable services (must in [INITIALISM,FIELD_COMMENT,SQL_NULL,GUREGU_NULL,JSON_TAG,XML_TAG,GORM_TAG,XORM_TAG,BEEGO_TAG,GOROSE_TAG,GORM_V2_TAG])
103104
-h, --help help for convert
104105
-H, --host string the host of mysql
105106
-n, --name string the name of the grom configuration file
@@ -131,32 +132,35 @@ Tags:
131132

132133
- [x] json
133134
- [x] xml
134-
- [x] [gorm](https://gorm.io/docs/models.html)
135+
- [x] [gorm v1](https://v1.gorm.io/docs/models.html)
135136
- [x] [xorm](https://gobook.io/read/gitea.com/xorm/manual-en-US/chapter-02/4.columns.html)
136137
- [x] [beego orm](https://beego.me/docs/mvc/model/models.md)
137138
- [x] [gorose](https://www.kancloud.cn/fizz/gorose-2/1135839)
139+
- [x] [gorm v2](https://gorm.io/docs/models.html)
138140

139141
## Supported Tag Generation Rules
140142

141-
| Tag | PrimaryKey | AutoIncrement | ColumnName | Type | IsNullable | Indexes | Uniques | Default | Comment | ForeignKey |
143+
| Tag | PrimaryKey | AutoIncrement | ColumnName | Type | IsNullable | Indexes | Uniques | Default | Comment | ForeignKey |
142144
|-----------|------------|---------------|------------|------|------------|---------|---------|---------|---------|------------|
143-
| json | × | × || × | × | × | × | × | × | × |
144-
| xml | × | × || × | × | × | × | × | × | × |
145-
| gorm |||||||||| × |
146-
| xorm |||||||||| × |
147-
| beego orm |||||| × | × ||| × |
148-
| gorose | × | × || × | × | × | × | × | × | × |
145+
| json | × | × || × | × | × | × | × | × | × |
146+
| xml | × | × || × | × | × | × | × | × | × |
147+
| gorm v1 |||||||||| × |
148+
| xorm |||||||||| × |
149+
| beego orm |||||| × | × ||| × |
150+
| gorose | × | × || × | × | × | × | × | × | × |
151+
| gorm v2 |||||||||| × |
149152

150153
## Supported Function Generation Rules
151154

152-
| Tag | TableName | TableIndex | TableUnique |
155+
| Tag | TableName | TableIndex | TableUnique |
153156
|-----------|-----------|------------|-------------|
154-
| json | × | × | × |
155-
| xml | × | × | × |
156-
| gorm || × | × |
157-
| xorm || × | × |
158-
| beego orm ||||
159-
| gorose || × | × |
157+
| json | × | × | × |
158+
| xml | × | × | × |
159+
| gorm v1 || × | × |
160+
| xorm || × | × |
161+
| beego orm ||||
162+
| gorose || × | × |
163+
| gorm v2 || × | × |
160164

161165
## Usage Example
162166

@@ -197,18 +201,19 @@ $ vim grom.json
197201
"enable_guregu_null": false,
198202
"enable_json_tag": true,
199203
"enable_xml_tag": false,
200-
"enable_gorm_tag": true,
204+
"enable_gorm_tag": false,
201205
"enable_xorm_tag": false,
202206
"enable_beego_tag": false,
203-
"enable_gorose_tag": false
207+
"enable_gorose_tag": false,
208+
"enable_gorm_v2_tag": true
204209
}
205210
$ grom convert -n grom.json
206211
```
207212

208213
You can also fill in the parameters on the command line without generating a configuration file:
209214

210215
```shell script
211-
$ grom convert -H localhost -P 3306 -u user -p password -d database -t api -e INITIALISM,FIELD_COMMENT,JSON_TAG,GORM_TAG
216+
$ grom convert -H localhost -P 3306 -u user -p password -d database -t api -e INITIALISM,FIELD_COMMENT,JSON_TAG,GORM_V2_TAG
212217
```
213218

214219
Then you will get the generated code:
@@ -217,13 +222,13 @@ Then you will get the generated code:
217222
package model
218223

219224
type API struct {
220-
ID int `json:"id" gorm:"primary_key;column:id;type:int(11) auto_increment;comment:'接口id'"` // 接口id
221-
Path string `json:"path" gorm:"column:path;type:varchar(255);unique_index:path_method;comment:'接口路径'"` // 接口路径
222-
Description string `json:"description" gorm:"column:description;type:varchar(255);comment:'接口描述'"` // 接口描述
223-
Group string `json:"group" gorm:"column:group;type:varchar(255);index:group;comment:'接口属组'"` // 接口属组
224-
Method string `json:"method" gorm:"column:method;type:varchar(255);unique_index:path_method;default:'POST';comment:'接口方法'"` // 接口方法
225-
CreateTime int64 `json:"create_time" gorm:"column:create_time;type:bigint(20);comment:'创建时间'"` // 创建时间
226-
UpdateTime int64 `json:"update_time" gorm:"column:update_time;type:bigint(20);comment:'更新时间'"` // 更新时间
225+
ID int `json:"id" gorm:"primaryKey;column:id;type:int(11) auto_increment;comment:接口id"` // 接口id
226+
Path string `json:"path" gorm:"column:path;type:varchar(255);uniqueIndex:path_method;comment:接口路径"` // 接口路径
227+
Description string `json:"description" gorm:"column:description;type:varchar(255);comment:接口描述"` // 接口描述
228+
Group string `json:"group" gorm:"column:group;type:varchar(255);index:group;comment:接口属组"` // 接口属组
229+
Method string `json:"method" gorm:"column:method;type:varchar(255);uniqueIndex:path_method;default:POST;comment:接口方法"` // 接口方法
230+
CreateTime int64 `json:"create_time" gorm:"column:create_time;type:bigint(20);comment:创建时间"` // 创建时间
231+
UpdateTime int64 `json:"update_time" gorm:"column:update_time;type:bigint(20);comment:更新时间"` // 更新时间
227232
}
228233

229234
// TableName returns the table name of the API model

README_zh-CN.md

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@ $ grom generate -h
7070
"enable_guregu_null": false, // 是否启用 null.Null 类型
7171
"enable_json_tag": true, // 是否启用 json 标签
7272
"enable_xml_tag": false, // 是否启用 xml 标签
73-
"enable_gorm_tag": true, // 是否启用 gorm 标签
73+
"enable_gorm_tag": false, // 是否启用 gorm v1 标签
7474
"enable_xorm_tag": false, // 是否启用 xorm 标签
7575
"enable_beego_tag": false, // 是否启用 beego orm 标签
76-
"enable_gorose_tag": false // 是否启用 gorose 标签
76+
"enable_gorose_tag": false, // 是否启用 gorose 标签
77+
"enable_gorm_v2_tag": true // 是否启用 gorm v2 标签
7778
}
7879

7980
用法:
@@ -94,11 +95,11 @@ $ grom convert -h
9495

9596
例子:
9697
grom convert -n ./grom.json
97-
grom convert -H localhost -P 3306 -u user -p password -d database -t table -e INITIALISM,FIELD_COMMENT,JSON_TAG,GORM_TAG --package PACKAGE_NAME --struct STRUCT_NAME
98+
grom convert -H localhost -P 3306 -u user -p password -d database -t table -e INITIALISM,FIELD_COMMENT,JSON_TAG,GORM_V2_TAG --package PACKAGE_NAME --struct STRUCT_NAME
9899

99100
标记:
100101
-d, --database string 将要连接的 mysql 数据库
101-
-e, --enable strings 启用的服务(必须包含在 [INITIALISM,FIELD_COMMENT,SQL_NULL,GUREGU_NULL,JSON_TAG,XML_TAG,GORM_TAG,XORM_TAG,BEEGO_TAG,GOROSE_TAG] 之中)
102+
-e, --enable strings 启用的服务(必须包含在 [INITIALISM,FIELD_COMMENT,SQL_NULL,GUREGU_NULL,JSON_TAG,XML_TAG,GORM_TAG,XORM_TAG,BEEGO_TAG,GOROSE_TAG,GORM_V2_TAG] 之中)
102103
-h, --help 获取有关 convert 命令的帮助
103104
-H, --host string 将要连接的 mysql 主机
104105
-n, --name string 指定的 grom 配置文件的名称
@@ -130,32 +131,35 @@ $ grom version -h
130131

131132
- [x] json
132133
- [x] xml
133-
- [x] [gorm](https://gorm.io/zh_CN/docs/models.html)
134+
- [x] [gorm v1](https://v1.gorm.io/zh_CN/docs/models.html)
134135
- [x] [xorm](https://gobook.io/read/gitea.com/xorm/manual-zh-CN/chapter-02/4.columns.html)
135136
- [x] [beego orm](https://beego.me/docs/mvc/model/models.md)
136137
- [x] [gorose](https://www.kancloud.cn/fizz/gorose-2/1135839)
138+
- [x] [gorm v2](https://gorm.io/zh_CN/docs/models.html)
137139

138140
## 支持的标签生成规则
139141

140-
| 标签 | 主键 | 自增 | 列名 | 类型 | 是否为 null | normal 索引 | unique 索引 | 默认值 | 注释 | 外键 |
142+
| 标签 | 主键 | 自增 | 列名 | 类型 | 是否为 null | normal 索引 | unique 索引 | 默认值 | 注释 | 外键 |
141143
|-----------|------|------|------|------|-------------|-------------|-------------|--------|------|------|
142-
| json | × | × || × | × | × | × | × | × | × |
143-
| xml | × | × || × | × | × | × | × | × | × |
144-
| gorm |||||||||| × |
145-
| xorm |||||||||| × |
146-
| beego orm |||||| × | × ||| × |
147-
| gorose | × | × || × | × | × | × | × | × | × |
144+
| json | × | × || × | × | × | × | × | × | × |
145+
| xml | × | × || × | × | × | × | × | × | × |
146+
| gorm v1 |||||||||| × |
147+
| xorm |||||||||| × |
148+
| beego orm |||||| × | × ||| × |
149+
| gorose | × | × || × | × | × | × | × | × | × |
150+
| gorm v2 |||||||||| × |
148151

149152
## 支持的函数生成规则
150153

151-
| 标签 | 表名函数(TableName) | 表 normal 索引函数(TableIndex) | 表 unique 索引函数(TableUnique) |
154+
| 标签 | 表名函数(TableName) | 表 normal 索引函数(TableIndex) | 表 unique 索引函数(TableUnique) |
152155
|-----------|-----------------------|----------------------------------|-----------------------------------|
153-
| json | × | × | × |
154-
| xml | × | × | × |
155-
| gorm || × | × |
156-
| xorm || × | × |
157-
| beego orm ||||
158-
| gorose || × | × |
156+
| json | × | × | × |
157+
| xml | × | × | × |
158+
| gorm v1 || × | × |
159+
| xorm || × | × |
160+
| beego orm ||||
161+
| gorose || × | × |
162+
| gorm v2 || × | × |
159163

160164
## 用法举例
161165

@@ -196,18 +200,19 @@ $ vim grom.json
196200
"enable_guregu_null": false,
197201
"enable_json_tag": true,
198202
"enable_xml_tag": false,
199-
"enable_gorm_tag": true,
203+
"enable_gorm_tag": false,
200204
"enable_xorm_tag": false,
201205
"enable_beego_tag": false,
202-
"enable_gorose_tag": false
206+
"enable_gorose_tag": false,
207+
"enable_gorm_v2_tag": true
203208
}
204209
$ grom convert -n grom.json
205210
```
206211

207212
你也可以在命令行中填写参数,而不生成配置文件:
208213

209214
```shell script
210-
$ grom convert -H localhost -P 3306 -u user -p password -d database -t api -e INITIALISM,FIELD_COMMENT,JSON_TAG,GORM_TAG
215+
$ grom convert -H localhost -P 3306 -u user -p password -d database -t api -e INITIALISM,FIELD_COMMENT,JSON_TAG,GORM_V2_TAG
211216
```
212217

213218
然后你将会得到生成的代码:
@@ -216,13 +221,13 @@ $ grom convert -H localhost -P 3306 -u user -p password -d database -t api -e IN
216221
package model
217222

218223
type API struct {
219-
ID int `json:"id" gorm:"primary_key;column:id;type:int(11) auto_increment;comment:'接口id'"` // 接口id
220-
Path string `json:"path" gorm:"column:path;type:varchar(255);unique_index:path_method;comment:'接口路径'"` // 接口路径
221-
Description string `json:"description" gorm:"column:description;type:varchar(255);comment:'接口描述'"` // 接口描述
222-
Group string `json:"group" gorm:"column:group;type:varchar(255);index:group;comment:'接口属组'"` // 接口属组
223-
Method string `json:"method" gorm:"column:method;type:varchar(255);unique_index:path_method;default:'POST';comment:'接口方法'"` // 接口方法
224-
CreateTime int64 `json:"create_time" gorm:"column:create_time;type:bigint(20);comment:'创建时间'"` // 创建时间
225-
UpdateTime int64 `json:"update_time" gorm:"column:update_time;type:bigint(20);comment:'更新时间'"` // 更新时间
224+
ID int `json:"id" gorm:"primaryKey;column:id;type:int(11) auto_increment;comment:接口id"` // 接口id
225+
Path string `json:"path" gorm:"column:path;type:varchar(255);uniqueIndex:path_method;comment:接口路径"` // 接口路径
226+
Description string `json:"description" gorm:"column:description;type:varchar(255);comment:接口描述"` // 接口描述
227+
Group string `json:"group" gorm:"column:group;type:varchar(255);index:group;comment:接口属组"` // 接口属组
228+
Method string `json:"method" gorm:"column:method;type:varchar(255);uniqueIndex:path_method;default:POST;comment:接口方法"` // 接口方法
229+
CreateTime int64 `json:"create_time" gorm:"column:create_time;type:bigint(20);comment:创建时间"` // 创建时间
230+
UpdateTime int64 `json:"update_time" gorm:"column:update_time;type:bigint(20);comment:更新时间"` // 更新时间
226231
}
227232

228233
// TableName returns the table name of the API model

cmd/convert.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var (
3535
"XORM_TAG": {},
3636
"BEEGO_TAG": {},
3737
"GOROSE_TAG": {},
38+
"GORM_V2_TAG": {},
3839
}
3940
)
4041

@@ -43,7 +44,7 @@ var convertCmd = &cobra.Command{
4344
Short: "Convert mysql table fields to golang model structure",
4445
Long: "Convert mysql table fields to golang model structure by information_schema.columns and information_schema.statistics",
4546
Example: " grom convert -n ./grom.json\n" +
46-
" grom convert -H localhost -P 3306 -u user -p password -d database -t table -e INITIALISM,FIELD_COMMENT,JSON_TAG,GORM_TAG --package PACKAGE_NAME --struct STRUCT_NAME",
47+
" grom convert -H localhost -P 3306 -u user -p password -d database -t table -e INITIALISM,FIELD_COMMENT,JSON_TAG,GORM_V2_TAG --package PACKAGE_NAME --struct STRUCT_NAME",
4748
Run: convertFunc,
4849
}
4950

@@ -57,7 +58,7 @@ func init() {
5758
convertCmd.Flags().StringVarP(&password, "password", "p", "", "the password of mysql")
5859
convertCmd.Flags().StringVarP(&database, "database", "d", "", "the database of mysql")
5960
convertCmd.Flags().StringVarP(&table, "table", "t", "", "the table of mysql")
60-
convertCmd.Flags().StringSliceVarP(&enable, "enable", "e", nil, "enable services (must in [INITIALISM,FIELD_COMMENT,SQL_NULL,GUREGU_NULL,JSON_TAG,XML_TAG,GORM_TAG,XORM_TAG,BEEGO_TAG,GOROSE_TAG])")
61+
convertCmd.Flags().StringSliceVarP(&enable, "enable", "e", nil, "enable services (must in [INITIALISM,FIELD_COMMENT,SQL_NULL,GUREGU_NULL,JSON_TAG,XML_TAG,GORM_TAG,XORM_TAG,BEEGO_TAG,GOROSE_TAG,GORM_V2_TAG])")
6162

6263
rootCmd.AddCommand(convertCmd)
6364
}
@@ -148,6 +149,8 @@ func getCmdConfig() (util.CMDConfig, error) {
148149
config.EnableBeegoTag = true
149150
case "GOROSE_TAG":
150151
config.EnableGoroseTag = true
152+
case "GORM_V2_TAG":
153+
config.EnableGormV2Tag = true
151154
}
152155
}
153156
}

cmd/generate.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@ func generateFileInfo() string {
6464
EnableGureguNull: false,
6565
EnableJsonTag: true,
6666
EnableXmlTag: false,
67-
EnableGormTag: true,
67+
EnableGormTag: false,
6868
EnableXormTag: false,
6969
EnableBeegoTag: false,
7070
EnableGoroseTag: false,
71+
EnableGormV2Tag: true,
7172
}
7273

7374
b, _ := json.MarshalIndent(&c, "", " ")

cmd/version.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ var (
1010
projectName = "Grom"
1111
projectVersion = "1.0.1"
1212
goVersion = "go1.14.3"
13-
gitCommit = "940cbeb0f2"
14-
buildTime = "2020-08-25 10:25:11"
13+
gitCommit = "9f35c08031"
14+
buildTime = "2020-10-12 10:11:49"
1515
)
1616

1717
var versionCmd = &cobra.Command{

util/define.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ type CMDConfig struct {
110110
EnableXormTag bool `json:"enable_xorm_tag"`
111111
EnableBeegoTag bool `json:"enable_beego_tag"`
112112
EnableGoroseTag bool `json:"enable_gorose_tag"`
113+
EnableGormV2Tag bool `json:"enable_gorm_v2_tag"`
113114
}
114115

115116
// DBConfig represents the config of the connected database.

0 commit comments

Comments
 (0)