diff --git a/examples/go.mod b/examples/go.mod index cc832c8b..f7f7682f 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -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 diff --git a/examples/go.sum b/examples/go.sum index 76607200..2c26347c 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -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= diff --git a/tools/gentool/README.md b/tools/gentool/README.md index a8543b70..121e66c1 100644 --- a/tools/gentool/README.md +++ b/tools/gentool/README.md @@ -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 "" @@ -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 diff --git a/tools/gentool/gen.yml b/tools/gentool/gen.yml index 5fa00f07..7de9c2fa 100644 --- a/tools/gentool/gen.yml +++ b/tools/gentool/gen.yml @@ -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 : diff --git a/tools/gentool/gentool.go b/tools/gentool/gentool.go index 3d438867..4d4b136f 100644 --- a/tools/gentool/gentool.go +++ b/tools/gentool/gentool.go @@ -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 { @@ -72,6 +74,8 @@ func (c *CmdParams) revise() *CmdParams { tableList = append(tableList, _tableName) } c.Tables = tableList + + c.modeGenerateMode = parseMode(c.Mode...) return c } @@ -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 @@ -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() @@ -226,6 +267,7 @@ func main() { FieldWithIndexTag: config.FieldWithIndexTag, FieldWithTypeTag: config.FieldWithTypeTag, FieldSignable: config.FieldSignable, + Mode: config.modeGenerateMode, }) g.UseDB(db)