Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enhance supports for 'mode' of generations in gentool #1182

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/mattn/go-sqlite3 v1.14.17 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/tools v0.15.0 // indirect
golang.org/x/tools v0.17.0 // indirect
gorm.io/datatypes v1.1.1-0.20230130040222-c43177d3cf8c // indirect
gorm.io/hints v1.1.0 // indirect
gorm.io/plugin/dbresolver v1.5.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/tools v0.15.0 h1:zdAyfUGbYmuVokhzVmghFl2ZJh5QhcfebBgmVPFYA+8=
golang.org/x/tools v0.15.0/go.mod h1:hpksKq4dtpQWS1uQ61JkdqWM3LscIS6Slf+VVkm+wQk=
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/datatypes v1.1.1-0.20230130040222-c43177d3cf8c h1:jWdr7cHgl8c/ua5vYbR2WhSp+NQmzhsj0xoY3foTzW8=
Expand Down
25 changes: 24 additions & 1 deletion tools/gentool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ Install GEN as a binary tool
generate unit test for query code
-fieldSignable
detect integer field's unsigned type, adjust generated data type

-mode
enter the genreate mode or leave it blank
```
#### c
default ""
Expand Down Expand Up @@ -120,7 +121,29 @@ Value : False / True

detect integer field's unsigned type, adjust generated data type

#### mode

Value: enter the required data table or leave it blank

Items to be selected:

- WithDefaultQuery
- WithoutContext
- WithQUeryInterface

eg:

```shell
gentool -mode="WithDefaultQuery"
```

```shell
gentool -mode="WithDefaultQuery,WithoutContext"
```

```shell
gentool -mode="WithDefaultQuery,WithoutContext,WithQUeryInterface"
```

### example

Expand Down
6 changes: 6 additions & 0 deletions tools/gentool/gen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ database:
fieldWithTypeTag : false
# detect integer field's unsigned type, adjust generated data type
fieldSignable : false
# generate mode
# mode :
# - WithDefaultQuery
# - WithoutContext
# - WithQueryInterface
mode :
68 changes: 55 additions & 13 deletions tools/gentool/gentool.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,21 @@ const (

// CmdParams is command line parameters
type CmdParams struct {
DSN string `yaml:"dsn"` // consult[https://gorm.io/docs/connecting_to_the_database.html]"
DB string `yaml:"db"` // input mysql or postgres or sqlite or sqlserver. consult[https://gorm.io/docs/connecting_to_the_database.html]
Tables []string `yaml:"tables"` // enter the required data table or leave it blank
OnlyModel bool `yaml:"onlyModel"` // only generate model
OutPath string `yaml:"outPath"` // specify a directory for output
OutFile string `yaml:"outFile"` // query code file name, default: gen.go
WithUnitTest bool `yaml:"withUnitTest"` // generate unit test for query code
ModelPkgName string `yaml:"modelPkgName"` // generated model code's package name
FieldNullable bool `yaml:"fieldNullable"` // generate with pointer when field is nullable
FieldCoverable bool `yaml:"fieldCoverable"` // generate with pointer when field has default value
FieldWithIndexTag bool `yaml:"fieldWithIndexTag"` // generate field with gorm index tag
FieldWithTypeTag bool `yaml:"fieldWithTypeTag"` // generate field with gorm column type tag
FieldSignable bool `yaml:"fieldSignable"` // detect integer field's unsigned type, adjust generated data type
DSN string `yaml:"dsn"` // consult[https://gorm.io/docs/connecting_to_the_database.html]"
DB string `yaml:"db"` // input mysql or postgres or sqlite or sqlserver. consult[https://gorm.io/docs/connecting_to_the_database.html]
Tables []string `yaml:"tables"` // enter the required data table or leave it blank
OnlyModel bool `yaml:"onlyModel"` // only generate model
OutPath string `yaml:"outPath"` // specify a directory for output
OutFile string `yaml:"outFile"` // query code file name, default: gen.go
WithUnitTest bool `yaml:"withUnitTest"` // generate unit test for query code
ModelPkgName string `yaml:"modelPkgName"` // generated model code's package name
FieldNullable bool `yaml:"fieldNullable"` // generate with pointer when field is nullable
FieldCoverable bool `yaml:"fieldCoverable"` // generate with pointer when field has default value
FieldWithIndexTag bool `yaml:"fieldWithIndexTag"` // generate field with gorm index tag
FieldWithTypeTag bool `yaml:"fieldWithTypeTag"` // generate field with gorm column type tag
FieldSignable bool `yaml:"fieldSignable"` // detect integer field's unsigned type, adjust generated data type
Mode []string `yaml:"mode"` // generate mode
modeGenerateMode gen.GenerateMode // generate mode has been converted to gen.GenerateMode
}

func (c *CmdParams) revise() *CmdParams {
Expand All @@ -72,6 +74,8 @@ func (c *CmdParams) revise() *CmdParams {
tableList = append(tableList, _tableName)
}
c.Tables = tableList

c.modeGenerateMode = parseMode(c.Mode...)
return c
}

Expand Down Expand Up @@ -154,6 +158,7 @@ func argParse() *CmdParams {
fieldWithIndexTag := flag.Bool("fieldWithIndexTag", false, "generate field with gorm index tag")
fieldWithTypeTag := flag.Bool("fieldWithTypeTag", false, "generate field with gorm column type tag")
fieldSignable := flag.Bool("fieldSignable", false, "detect integer field's unsigned type, adjust generated data type")
mode := flag.String("mode", "", "generate mode")
flag.Parse()

if *genPath != "" { //use yml config
Expand Down Expand Up @@ -201,9 +206,45 @@ func argParse() *CmdParams {
if *fieldSignable {
cmdParse.FieldSignable = *fieldSignable
}
if *mode != "" {
cmdParse.Mode = strings.Split(*mode, ",")
}
return &cmdParse
}

// parseModeCode parse mode string to GenerateMode
func parseModeCode(mode string) (gen.GenerateMode, error) {
switch mode {
case "WithDefaultQuery":
return gen.WithDefaultQuery, nil
case "WithoutContext":
return gen.WithoutContext, nil
case "WithQueryInterface":
return gen.WithQueryInterface, nil
}

return 0, fmt.Errorf("unknown mode %q", mode)
}

// parseMode parse generate mode string slice to GenerateMode
func parseMode(mode ...string) gen.GenerateMode {
if len(mode) == 0 {
return gen.GenerateMode(0)
}

g := gen.GenerateMode(0)
for i := 0; i < len(mode); i++ {
modeCode, err := parseModeCode(mode[0])
if err != nil {
log.Fatalln("failed to parse generate mode: ", err)
}

g |= modeCode
}

return g
}

func main() {
// cmdParse
config := argParse().revise()
Expand All @@ -226,6 +267,7 @@ func main() {
FieldWithIndexTag: config.FieldWithIndexTag,
FieldWithTypeTag: config.FieldWithTypeTag,
FieldSignable: config.FieldSignable,
Mode: config.modeGenerateMode,
})

g.UseDB(db)
Expand Down
Loading