Skip to content

Commit 0780b46

Browse files
authored
Merge pull request #1438 from go-gorm/feat/gentool-default-tag
feat(gentool): add fieldWithDefaultTag option
2 parents 6c12c70 + 1d4be8a commit 0780b46

File tree

4 files changed

+44
-24
lines changed

4 files changed

+44
-24
lines changed

tools/gentool/README.ZH_CN.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
generate field with gorm index tag
3030
-fieldWithTypeTag
3131
generate field with gorm column type tag
32+
-fieldWithDefaultTag
33+
generate field with gorm default tag
3234
-modelPkgName string
3335
generated model code's package name
3436
-outFile string
@@ -82,6 +84,10 @@ default ""
8284

8385
使用gorm列类型标记生成字段
8486

87+
#### fieldWithDefaultTag
88+
89+
使用gorm默认值标记生成字段
90+
8591
#### modelPkgName
8692

8793
默认值是数据表名称。
@@ -131,4 +137,4 @@ Value : False / True
131137

132138
```shell
133139
gentool -dsn "user:pwd@tcp(127.0.0.1:3306)/database?charset=utf8mb4&parseTime=True&loc=Local" -tables "orders,doctor"
134-
```
140+
```

tools/gentool/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Install GEN as a binary tool
2727
generate field with gorm index tag
2828
-fieldWithTypeTag
2929
generate field with gorm column type tag
30+
-fieldWithDefaultTag
31+
generate field with gorm default tag
3032
-modelPkgName string
3133
generated model code's package name
3234
-outFile string
@@ -80,6 +82,10 @@ generate field with gorm index tag
8082

8183
generate field with gorm column type tag
8284

85+
#### fieldWithDefaultTag
86+
87+
generate field with gorm default tag
88+
8389
#### modelPkgName
8490

8591
defalut table name.
@@ -126,4 +132,4 @@ detect integer field's unsigned type, adjust generated data type
126132

127133
```shell
128134
gentool -dsn "user:pwd@tcp(127.0.0.1:3306)/database?charset=utf8mb4&parseTime=True&loc=Local" -tables "orders,doctor"
129-
```
135+
```

tools/gentool/gen.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,7 @@ database:
2828
fieldWithIndexTag : false
2929
# generate field with gorm column type tag
3030
fieldWithTypeTag : false
31+
# generate field with gorm default tag
32+
fieldWithDefaultTag : false
3133
# detect integer field's unsigned type, adjust generated data type
3234
fieldSignable : false

tools/gentool/gentool.go

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,20 @@ const (
3434

3535
// CmdParams is command line parameters
3636
type CmdParams struct {
37-
DSN string `yaml:"dsn"` // consult[https://gorm.io/docs/connecting_to_the_database.html]"
38-
DB string `yaml:"db"` // input mysql or postgres or sqlite or sqlserver. consult[https://gorm.io/docs/connecting_to_the_database.html]
39-
Tables []string `yaml:"tables"` // enter the required data table or leave it blank
40-
OnlyModel bool `yaml:"onlyModel"` // only generate model
41-
OutPath string `yaml:"outPath"` // specify a directory for output
42-
OutFile string `yaml:"outFile"` // query code file name, default: gen.go
43-
WithUnitTest bool `yaml:"withUnitTest"` // generate unit test for query code
44-
ModelPkgName string `yaml:"modelPkgName"` // generated model code's package name
45-
FieldNullable bool `yaml:"fieldNullable"` // generate with pointer when field is nullable
46-
FieldCoverable bool `yaml:"fieldCoverable"` // generate with pointer when field has default value
47-
FieldWithIndexTag bool `yaml:"fieldWithIndexTag"` // generate field with gorm index tag
48-
FieldWithTypeTag bool `yaml:"fieldWithTypeTag"` // generate field with gorm column type tag
49-
FieldSignable bool `yaml:"fieldSignable"` // detect integer field's unsigned type, adjust generated data type
37+
DSN string `yaml:"dsn"` // consult[https://gorm.io/docs/connecting_to_the_database.html]"
38+
DB string `yaml:"db"` // input mysql or postgres or sqlite or sqlserver. consult[https://gorm.io/docs/connecting_to_the_database.html]
39+
Tables []string `yaml:"tables"` // enter the required data table or leave it blank
40+
OnlyModel bool `yaml:"onlyModel"` // only generate model
41+
OutPath string `yaml:"outPath"` // specify a directory for output
42+
OutFile string `yaml:"outFile"` // query code file name, default: gen.go
43+
WithUnitTest bool `yaml:"withUnitTest"` // generate unit test for query code
44+
ModelPkgName string `yaml:"modelPkgName"` // generated model code's package name
45+
FieldNullable bool `yaml:"fieldNullable"` // generate with pointer when field is nullable
46+
FieldCoverable bool `yaml:"fieldCoverable"` // generate with pointer when field has default value
47+
FieldWithIndexTag bool `yaml:"fieldWithIndexTag"` // generate field with gorm index tag
48+
FieldWithTypeTag bool `yaml:"fieldWithTypeTag"` // generate field with gorm column type tag
49+
FieldWithDefaultTag bool `yaml:"fieldWithDefaultTag"` // generate field with gorm default tag
50+
FieldSignable bool `yaml:"fieldSignable"` // detect integer field's unsigned type, adjust generated data type
5051
}
5152

5253
func (c *CmdParams) revise() *CmdParams {
@@ -153,6 +154,7 @@ func argParse() *CmdParams {
153154
fieldCoverable := flag.Bool("fieldCoverable", false, "generate with pointer when field has default value")
154155
fieldWithIndexTag := flag.Bool("fieldWithIndexTag", false, "generate field with gorm index tag")
155156
fieldWithTypeTag := flag.Bool("fieldWithTypeTag", false, "generate field with gorm column type tag")
157+
fieldWithDefaultTag := flag.Bool("fieldWithDefaultTag", false, "generate field with gorm default tag")
156158
fieldSignable := flag.Bool("fieldSignable", false, "detect integer field's unsigned type, adjust generated data type")
157159
flag.Parse()
158160

@@ -198,6 +200,9 @@ func argParse() *CmdParams {
198200
if *fieldWithTypeTag {
199201
cmdParse.FieldWithTypeTag = *fieldWithTypeTag
200202
}
203+
if *fieldWithDefaultTag {
204+
cmdParse.FieldWithDefaultTag = *fieldWithDefaultTag
205+
}
201206
if *fieldSignable {
202207
cmdParse.FieldSignable = *fieldSignable
203208
}
@@ -217,15 +222,16 @@ func main() {
217222
}
218223

219224
g := gen.NewGenerator(gen.Config{
220-
OutPath: config.OutPath,
221-
OutFile: config.OutFile,
222-
ModelPkgPath: config.ModelPkgName,
223-
WithUnitTest: config.WithUnitTest,
224-
FieldNullable: config.FieldNullable,
225-
FieldCoverable: config.FieldCoverable,
226-
FieldWithIndexTag: config.FieldWithIndexTag,
227-
FieldWithTypeTag: config.FieldWithTypeTag,
228-
FieldSignable: config.FieldSignable,
225+
OutPath: config.OutPath,
226+
OutFile: config.OutFile,
227+
ModelPkgPath: config.ModelPkgName,
228+
WithUnitTest: config.WithUnitTest,
229+
FieldNullable: config.FieldNullable,
230+
FieldCoverable: config.FieldCoverable,
231+
FieldWithIndexTag: config.FieldWithIndexTag,
232+
FieldWithTypeTag: config.FieldWithTypeTag,
233+
FieldWithDefaultTag: config.FieldWithDefaultTag,
234+
FieldSignable: config.FieldSignable,
229235
})
230236

231237
g.UseDB(db)

0 commit comments

Comments
 (0)