Skip to content

Commit 1cbb2b0

Browse files
committed
feat: add DISABLE_UNSIGNED service and disable_unsigned config
1 parent 380f4f7 commit 1cbb2b0

File tree

10 files changed

+77
-55
lines changed

10 files changed

+77
-55
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ Generate grom configuration file like this:
8181
"enable_xorm_tag": false,
8282
"enable_beego_tag": false,
8383
"enable_gorose_tag": false,
84-
"enable_gorm_v2_tag": true
84+
"enable_gorm_v2_tag": true,
85+
"disable_unsigned": false
8586
}
8687

8788
Usage:
@@ -106,7 +107,7 @@ Examples:
106107

107108
Flags:
108109
-d, --database string the database of mysql
109-
-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])
110+
-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,DISABLE_UNSIGNED])
110111
-h, --help help for convert
111112
-H, --host string the host of mysql
112113
-n, --name string the name of the grom configuration file
@@ -212,7 +213,8 @@ $ vim grom.json
212213
"enable_xorm_tag": false,
213214
"enable_beego_tag": false,
214215
"enable_gorose_tag": false,
215-
"enable_gorm_v2_tag": true
216+
"enable_gorm_v2_tag": true,
217+
"disable_unsigned": false
216218
}
217219
$ grom convert -n grom.json -o output.go
218220
```

README_zh-CN.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ $ grom generate -h
7878
"enable_xorm_tag": false, // 是否启用 xorm 标签
7979
"enable_beego_tag": false, // 是否启用 beego orm 标签
8080
"enable_gorose_tag": false, // 是否启用 gorose 标签
81-
"enable_gorm_v2_tag": true // 是否启用 gorm v2 标签
81+
"enable_gorm_v2_tag": true, // 是否启用 gorm v2 标签
82+
"disable_unsigned": false // 是否禁用无符号整数类型
8283
}
8384

8485
用法:
@@ -103,7 +104,7 @@ $ grom convert -h
103104

104105
标记:
105106
-d, --database string 将要连接的 mysql 数据库
106-
-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] 之中)
107+
-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,DISABLE_UNSIGNED] 之中)
107108
-h, --help 获取有关 convert 命令的帮助
108109
-H, --host string 将要连接的 mysql 主机
109110
-n, --name string 指定的 grom 配置文件的名称
@@ -209,7 +210,8 @@ $ vim grom.json
209210
"enable_xorm_tag": false,
210211
"enable_beego_tag": false,
211212
"enable_gorose_tag": false,
212-
"enable_gorm_v2_tag": true
213+
"enable_gorm_v2_tag": true,
214+
"disable_unsigned": false
213215
}
214216
$ grom convert -n grom.json -o output.go
215217
```

cmd/convert.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,18 @@ var (
2626
enable []string
2727

2828
validServices = map[string]struct{}{
29-
"INITIALISM": {},
30-
"FIELD_COMMENT": {},
31-
"SQL_NULL": {},
32-
"GUREGU_NULL": {},
33-
"JSON_TAG": {},
34-
"XML_TAG": {},
35-
"GORM_TAG": {},
36-
"XORM_TAG": {},
37-
"BEEGO_TAG": {},
38-
"GOROSE_TAG": {},
39-
"GORM_V2_TAG": {},
29+
"INITIALISM": {},
30+
"FIELD_COMMENT": {},
31+
"SQL_NULL": {},
32+
"GUREGU_NULL": {},
33+
"JSON_TAG": {},
34+
"XML_TAG": {},
35+
"GORM_TAG": {},
36+
"XORM_TAG": {},
37+
"BEEGO_TAG": {},
38+
"GOROSE_TAG": {},
39+
"GORM_V2_TAG": {},
40+
"DISABLE_UNSIGNED": {},
4041
}
4142
)
4243

@@ -60,7 +61,7 @@ func init() {
6061
convertCmd.Flags().StringVarP(&password, "password", "p", "", "the password of mysql")
6162
convertCmd.Flags().StringVarP(&database, "database", "d", "", "the database of mysql")
6263
convertCmd.Flags().StringVarP(&table, "table", "t", "", "the table of mysql")
63-
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])")
64+
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,DISABLE_UNSIGNED])")
6465

6566
rootCmd.AddCommand(convertCmd)
6667
}
@@ -166,6 +167,8 @@ func getCmdConfig() (*util.CMDConfig, error) {
166167
config.EnableGoroseTag = true
167168
case "GORM_V2_TAG":
168169
config.EnableGormV2Tag = true
170+
case "DISABLE_UNSIGNED":
171+
config.DisableUnsigned = true
169172
}
170173
}
171174
}

cmd/generate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func generateFileInfo() string {
6363
EnableBeegoTag: false,
6464
EnableGoroseTag: false,
6565
EnableGormV2Tag: true,
66+
DisableUnsigned: false,
6667
}
6768

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

cmd/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
var (
1111
projectName = "grom"
12-
projectVersion = "1.0.5"
12+
projectVersion = "1.0.6"
1313
goVersion = ""
1414
gitCommit = ""
1515
buildTime = ""

util/db.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,17 @@ import (
66
"strings"
77

88
_ "github.com/go-sql-driver/mysql"
9+
"github.com/gookit/color"
910
"github.com/pkg/errors"
1011
)
1112

13+
// CloseDB closes the db connection.
14+
func CloseDB() {
15+
if err := db.Close(); err != nil {
16+
color.Red.Println("db.Close err:", err)
17+
}
18+
}
19+
1220
// getDB returns the opened db connection.
1321
func getDB(c *CMDConfig) (*sql.DB, error) {
1422
if db != nil {
@@ -63,10 +71,6 @@ func getTableComment(c *CMDConfig) (string, error) {
6371

6472
// getColumnInfos returns the details of columns.
6573
func getColumnInfos(c *CMDConfig) ([]*ColumnInfo, error) {
66-
if columnInfos != nil {
67-
return columnInfos, nil
68-
}
69-
7074
db, err := getDB(c)
7175
if err != nil {
7276
return nil, err
@@ -90,7 +94,7 @@ func getColumnInfos(c *CMDConfig) ([]*ColumnInfo, error) {
9094
return nil, errors.New("no rows returned")
9195
}
9296

93-
columnInfos = make([]*ColumnInfo, 0)
97+
columnInfos := make([]*ColumnInfo, 0)
9498
indexInfos, err := getIndexInfos(c)
9599
if err != nil {
96100
return nil, errors.WithMessage(err, "getIndexInfos err")
@@ -117,7 +121,7 @@ func getColumnInfos(c *CMDConfig) ([]*ColumnInfo, error) {
117121
Name: cn, DataType: dt, Type: ct, Default: strings.TrimSpace(cd.String), Comment: strings.TrimSpace(cc),
118122
Length: cml.Int64, Precision: np.Int64, Scale: nc.Int64, Position: op,
119123
IsPrimaryKey: ck == "PRI", IsAutoIncrement: strings.Contains(e, "auto_increment"),
120-
IsUnsigned: strings.Contains(ct, "unsigned"), IsNullable: in == "YES",
124+
IsUnsigned: strings.Contains(ct, "unsigned") && !c.DisableUnsigned, IsNullable: in == "YES",
121125
}
122126

123127
ci.Indexes, ci.UniqueIndexes = getColumnIndexInfos(indexInfos, ci.Name)
@@ -133,10 +137,6 @@ func getColumnInfos(c *CMDConfig) ([]*ColumnInfo, error) {
133137

134138
// getIndexInfos returns the details of indexes.
135139
func getIndexInfos(c *CMDConfig) ([]*IndexInfo, error) {
136-
if indexInfos != nil {
137-
return indexInfos, nil
138-
}
139-
140140
db, err := getDB(c)
141141
if err != nil {
142142
return nil, err
@@ -152,7 +152,7 @@ func getIndexInfos(c *CMDConfig) ([]*IndexInfo, error) {
152152
return nil, errors.WithMessage(err, "db.Query err")
153153
}
154154

155-
indexInfos = make([]*IndexInfo, 0)
155+
indexInfos := make([]*IndexInfo, 0)
156156

157157
if rows != nil {
158158
defer rows.Close()

util/define.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ import (
88
var (
99
db *sql.DB
1010

11-
columnInfos []*ColumnInfo
12-
indexInfos []*IndexInfo
13-
1411
tableIndexes []string
1512
tableUniques []string
1613

@@ -111,6 +108,7 @@ type CMDConfig struct {
111108
EnableBeegoTag bool `json:"enable_beego_tag"`
112109
EnableGoroseTag bool `json:"enable_gorose_tag"`
113110
EnableGormV2Tag bool `json:"enable_gorm_v2_tag"`
111+
DisableUnsigned bool `json:"disable_unsigned"`
114112
EnableGoTime bool `json:"-"`
115113
TableComment string `json:"-"`
116114
}
@@ -127,10 +125,13 @@ type DBConfig struct {
127125

128126
// StructField represents the field of the generated model structure.
129127
type StructField struct {
130-
Name string
131-
Type string
132-
Tag string
133-
Comment string
128+
Name string
129+
Type string
130+
Tag string
131+
Comment string
132+
RawName string
133+
IsPrimaryKey bool
134+
IsNullable bool
134135
}
135136

136137
// ColumnInfo represents the information of the column.

util/template.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,6 @@ func init() {
5454

5555
// generateCode generates the output code by command config and structure fields.
5656
func generateCode(cc *CMDConfig, fields []*StructField) (string, error) {
57-
if cc.PackageName == "" {
58-
cc.PackageName = "model"
59-
}
60-
61-
if cc.StructName == "" {
62-
cc.StructName = convertName(cc.Table, cc.EnableInitialism)
63-
}
64-
6557
buffer := &bytes.Buffer{}
6658
err := generator.ExecuteTemplate(buffer, "out", struct {
6759
Table string

util/util.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,35 @@ import (
1010

1111
// ConvertTable converts mysql table fields to golang model structure by command config.
1212
func ConvertTable(cc CMDConfig) (string, error) {
13-
comment, err := getTableComment(&cc)
13+
defer CloseDB()
14+
15+
fields, err := GetFields(&cc)
1416
if err != nil {
15-
return "", errors.WithMessage(err, "getTableComment err")
17+
return "", err
18+
}
19+
20+
return generateCode(&cc, fields)
21+
}
22+
23+
// GetFields gets golang structure fields converted by mysql table fields.
24+
func GetFields(cc *CMDConfig) ([]*StructField, error) {
25+
if cc.PackageName == "" {
26+
cc.PackageName = "model"
27+
}
28+
if cc.StructName == "" {
29+
cc.StructName = convertName(cc.Table, cc.EnableInitialism)
30+
}
31+
32+
comment, err := getTableComment(cc)
33+
if err != nil {
34+
return nil, errors.WithMessage(err, "getTableComment err")
1635
}
1736
cc.TableComment = comment
1837

19-
cis, err := getColumnInfos(&cc)
38+
cis, err := getColumnInfos(cc)
2039
if err != nil {
21-
return "", errors.WithMessage(err, "getColumnInfos err")
40+
return nil, errors.WithMessage(err, "getColumnInfos err")
2241
}
23-
defer db.Close()
2442

2543
var fields []*StructField
2644
for i := range cis {
@@ -50,9 +68,12 @@ func ConvertTable(cc CMDConfig) (string, error) {
5068
}
5169

5270
field := StructField{
53-
Name: convertName(ci.Name, cc.EnableInitialism),
54-
Type: convertDataType(ci, &cc),
55-
Comment: ci.Comment,
71+
Name: convertName(ci.Name, cc.EnableInitialism),
72+
Type: convertDataType(ci, cc),
73+
Comment: ci.Comment,
74+
RawName: ci.Name,
75+
IsPrimaryKey: ci.IsPrimaryKey,
76+
IsNullable: ci.IsNullable,
5677
}
5778
if len(tags) > 0 {
5879
field.Tag = fmt.Sprintf("`%s`", strings.Join(removeEmpty(tags), " "))
@@ -63,7 +84,7 @@ func ConvertTable(cc CMDConfig) (string, error) {
6384
fields = append(fields, &field)
6485
}
6586

66-
return generateCode(&cc, fields)
87+
return fields, nil
6788
}
6889

6990
// convertDataType converts the mysql data type to golang data type.

util/util_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func TestGetGormV2Tag(t *testing.T) {
197197
Name: "id", Type: "bigint(20)", IsPrimaryKey: true,
198198
IsAutoIncrement: true, IsNullable: false, Default: "", Comment: "用户id",
199199
},
200-
"gorm:\"primaryKey;column:id;type:bigint(20) autoIncrement;comment:用户id\"",
200+
"gorm:\"primaryKey;autoIncrement;column:id;comment:用户id\"",
201201
},
202202
{
203203
ColumnInfo{

0 commit comments

Comments
 (0)