Skip to content

Commit

Permalink
fix: gentool config ignored bug
Browse files Browse the repository at this point in the history
  • Loading branch information
yama-6 committed Aug 3, 2023
1 parent c4d0b25 commit b7e5f72
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module gorm.io/gen
go 1.18

require (
github.com/creasty/defaults v1.7.0
golang.org/x/tools v0.6.0
gopkg.in/yaml.v3 v3.0.1
gorm.io/datatypes v1.1.1-0.20230130040222-c43177d3cf8c
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMe
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/creasty/defaults v1.7.0 h1:eNdqZvc5B509z18lD8yc212CAqJNvfT1Jq6L8WowdBA=
github.com/creasty/defaults v1.7.0/go.mod h1:iGzKe6pbEHnpMPtfDXZEr0NVxWnPTjb1bbDy08fPzYM=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
41 changes: 27 additions & 14 deletions tools/gentool/gentool.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"gorm.io/driver/sqlserver"
"gorm.io/gen"
"gorm.io/gorm"

"github.com/creasty/defaults"
)

// DBType database type
Expand All @@ -31,18 +33,29 @@ 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
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" default:"mysql"` // 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" default:"./dao/query"` // 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
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
}

func (c *CmdParams) UnmarshalYAML(unmarshal func(any) error) error {
defaults.Set(c)

type plain CmdParams
if err := unmarshal((*plain)(c)); err != nil {
return err
}

return nil
}

// YamlConfig is yaml config struct
Expand Down Expand Up @@ -113,10 +126,10 @@ func argParse() *CmdParams {
// choose is file or flag
genPath := flag.String("c", "", "is path for gen.yml")
dsn := flag.String("dsn", "", "consult[https://gorm.io/docs/connecting_to_the_database.html]")
db := flag.String("db", "mysql", "input mysql|postgres|sqlite|sqlserver|clickhouse. consult[https://gorm.io/docs/connecting_to_the_database.html]")
db := flag.String("db", "", "input mysql|postgres|sqlite|sqlserver|clickhouse. consult[https://gorm.io/docs/connecting_to_the_database.html]")
tableList := flag.String("tables", "", "enter the required data table or leave it blank")
onlyModel := flag.Bool("onlyModel", false, "only generate models (without query file)")
outPath := flag.String("outPath", "./dao/query", "specify a directory for output")
outPath := flag.String("outPath", "", "specify a directory for output")
outFile := flag.String("outFile", "", "query code file name, default: gen.go")
withUnitTest := flag.Bool("withUnitTest", false, "generate unit test for query code")
modelPkgName := flag.String("modelPkgName", "", "generated model code's package name")
Expand Down

0 comments on commit b7e5f72

Please sign in to comment.