Skip to content

Commit

Permalink
support generate code from yesql.yaml configure
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Aug 23, 2023
1 parent 5ad0fd6 commit 9424375
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 26 deletions.
58 changes: 58 additions & 0 deletions conf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package yesql

import (
"os"

"gopkg.in/yaml.v3"
)

type yesqlConf struct {
Version string `yaml:"version"`
Generator struct {
Engine string `yaml:"engine"`
SqlxPkgName string `yaml:"sqlx_package"`
DefaultStructName string `yaml:"default_struct_name"`
GoFileName string `yaml:"go_file_name"`
}
Sql []struct {
Queries string `yaml:"queries"`
Gen struct {
Package string `yaml:"package"`
Out string `yaml:"out"`
Engine string `yaml:"engine"`
SqlxPkgName string `yaml:"sqlx_package"`
DefaultStructName string `yaml:"default_struct_name"`
GoFileName string `yaml:"go_file_name"`
}
}
}

func (s *yesqlConf) prepare() {
for i := range s.Sql {
if s.Sql[i].Gen.Engine == "" {
s.Sql[i].Gen.Engine = s.Generator.Engine
}
if s.Sql[i].Gen.SqlxPkgName == "" {
s.Sql[i].Gen.SqlxPkgName = s.Generator.SqlxPkgName
}
if s.Sql[i].Gen.DefaultStructName == "" {
s.Sql[i].Gen.DefaultStructName = s.Generator.DefaultStructName
}
if s.Sql[i].Gen.GoFileName == "" {
s.Sql[i].Gen.GoFileName = s.Generator.GoFileName
}
}
}

func yesqlConfFrom(path string) (*yesqlConf, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
res := &yesqlConf{}
decoder := yaml.NewDecoder(file)
err = decoder.Decode(res)
res.prepare()
return res, err
}
6 changes: 5 additions & 1 deletion core.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,25 +179,29 @@ type Generator interface {
Generate(dstPath string, pkgName string, query SQLQuery, opts ...option) error
}

// OptFn option function
type OptFn func(opt *generateOption)

func (f OptFn) apply(opt *generateOption) {
f(opt)
}

// DefaultStructNameOpt set custom default global sql query struct name
func DefaultStructNameOpt(name string) OptFn {
return func(opt *generateOption) {
opt.defaultStructName = name
}
}

// GoFileNameOpt set custom go file name to generate
func GoFileNameOpt(name string) OptFn {
return func(opt *generateOption) {
opt.goFileName = name
}
}

func SqlxPkgName(name string) OptFn {
// SqlxPkgNameOpt set sqlx pakcage name
func SqlxPkgNameOpt(name string) OptFn {
return func(opt *generateOption) {
opt.sqlxPkgName = name
}
Expand Down
2 changes: 1 addition & 1 deletion examples/auto/ac/yesql.go → examples/auto/ac/ac.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/auto/bc/yesql.go → examples/auto/bc/bc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/auto/cc/yesql.go → examples/auto/cc/cc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/auto/yesql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 1 addition & 8 deletions examples/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,7 @@ func main() {
query.Query = strings.TrimRight(query.Query, ";")
return query, nil
})
opt := yesql.SqlxPkgName("github.com/bitbus/sqlx")
sqlInfos := []yesql.SqlInfo{
yesql.NewSqlInfo("yesql.sql", "auto", "yesql"),
yesql.NewSqlInfo("yesql_ac.sql", "auto/ac", "ac"),
yesql.NewSqlInfo("yesql_bc.sql", "auto/bc", "bc"),
yesql.NewSqlInfo("yesql_cc.sql", "auto/cc", "cc"),
}
if err := yesql.GenerateFrom(sqlInfos, opt); err != nil {
if err := yesql.Generate(); err != nil {
log.Fatalf("generate code occurs error: %s", err)
}
log.Println("[Yesql] generate code finish")
Expand Down
4 changes: 3 additions & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ module github.com/alimy/yesql/examples
go 1.18

require (
github.com/alimy/yesql v1.8.5
github.com/alimy/yesql v1.9.0
github.com/bitbus/sqlx v1.6.0
)

require gopkg.in/yaml.v3 v3.0.1 // indirect

replace github.com/alimy/yesql => ../
4 changes: 4 additions & 0 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ github.com/bitbus/sqlx v1.6.0/go.mod h1:MemKLfQ600g6PxUVsIDe48PlY3wOquyW2ApeiXoy
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
28 changes: 28 additions & 0 deletions examples/yesql.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
version: '1'

generator:
engine: sqlx
sqlx_package: 'github.com/bitbus/sqlx'
default_struct_name: yesql
go_file_name: yesql

sql:
- queries: yesql.sql
gen:
package: yesql
out: auto
- queries: yesql_ac.sql
gen:
package: ac
out: auto/ac
go_file_name: ac
- queries: yesql_bc.sql
gen:
package: bc
out: auto/bc
go_file_name: bc
- queries: yesql_cc.sql
gen:
package: cc
out: auto/cc
go_file_name: cc
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ go 1.18

require (
github.com/bitbus/sqlx v1.6.0
gopkg.in/yaml.v3 v3.0.1
)
15 changes: 7 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
github.com/bitbus/sqlx v1.6.0 h1:ewrBydRkyHZqfOqvHVYpiBlqQtLn93B/loa2EwjpZ74=
github.com/bitbus/sqlx v1.6.0/go.mod h1:MemKLfQ600g6PxUVsIDe48PlY3wOquyW2ApeiXoynFo=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package yesql

var Version = "v1.8.5"
var Version = "v1.9.0"
34 changes: 31 additions & 3 deletions yesql.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ func NewSqlInfo(sqlFilePath string, dstPath string, pkgName string) SqlInfo {
}
}

// Generate generate struct type autumatic by sql file with default generator
func Generate(sqlFilePath string, dstPath string, pkgName string, opts ...option) error {
// GenerateBy generate struct type autumatic by sql file with default generator
func GenerateBy(sqlFilePath string, dstPath string, pkgName string, opts ...option) error {
query, err := ParseFile(sqlFilePath)
if err != nil {
return err
Expand All @@ -159,7 +159,35 @@ func Generate(sqlFilePath string, dstPath string, pkgName string, opts ...option
// GenerateFrom generate struct type autumatic from SqlInfo's inforamation with default generator
func GenerateFrom(infos []SqlInfo, opts ...option) (err error) {
for _, s := range infos {
if err = Generate(s.FilePath, s.DestPath, s.PkgName, opts...); err != nil {
if err = GenerateBy(s.FilePath, s.DestPath, s.PkgName, opts...); err != nil {
return
}
}
return
}

// Generate generate struct type autumatic by configFile with default generator
func Generate(conf ...string) (err error) {
cfgFileName := "yesql.yaml"
if len(conf) > 0 && conf[0] != "" {
cfgFileName = conf[0]
}
cfg, xerr := yesqlConfFrom(cfgFileName)
if xerr != nil {
return err
}
for _, q := range cfg.Sql {
opts := []option{}
if q.Gen.DefaultStructName != "" {
opts = append(opts, DefaultStructNameOpt(q.Gen.DefaultStructName))
}
if q.Gen.GoFileName != "" {
opts = append(opts, GoFileNameOpt(q.Gen.GoFileName))
}
if q.Gen.SqlxPkgName != "" {
opts = append(opts, SqlxPkgNameOpt(q.Gen.SqlxPkgName))
}
if err = GenerateBy(q.Queries, q.Gen.Out, q.Gen.Package, opts...); err != nil {
return
}
}
Expand Down

0 comments on commit 9424375

Please sign in to comment.