Skip to content

Commit

Permalink
support custom sqlx package name
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Aug 19, 2023
1 parent f8ef20c commit c4f1554
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
7 changes: 7 additions & 0 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Namespace struct{}
type generateOption struct {
goFileName string
defaultStructName string
sqlxPkgName string
}

type option interface {
Expand Down Expand Up @@ -197,3 +198,9 @@ func GoFileNameOpt(name string) OptionFunc {
opt.goFileName = name
}
}

func SqlxPkgName(name string) OptionFunc {
return func(opt *generateOption) {
opt.sqlxPkgName = name
}
}
7 changes: 7 additions & 0 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ import (
"github.com/jmoiron/sqlx"
)

const (
_defaultSqlxPkgName = "github.com/jmoiron/sqlx"
)

var (
_ Generator = (*sqlGenerator)(nil)
)

type tmplCtx struct {
PkgName string
SqlxPkgName string
DefaultStructName string
AllQuery []*Query
DefaultQueryMap QueryMap
Expand Down Expand Up @@ -77,12 +82,14 @@ func (s *sqlGenerator) Generate(dstPath string, pkgName string, query SQLQuery,
opt := &generateOption{
goFileName: "yesql.go",
defaultStructName: "Yesql",
sqlxPkgName: _defaultSqlxPkgName,
}
for _, arg := range opts {
arg.apply(opt)
}
data := &tmplCtx{
PkgName: pkgName,
SqlxPkgName: opt.sqlxPkgName,
DefaultStructName: naming.Naming(opt.defaultStructName),
AllQuery: query.AllQuery(),
ScopeQuery: query.ListScope(),
Expand Down
2 changes: 1 addition & 1 deletion template/sqlx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"context"

"github.com/alimy/yesql"
"github.com/jmoiron/sqlx"
"{{ .SqlxPkgName }}"
)

const (
Expand Down
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.5.0"
var Version = "v1.6.0"

0 comments on commit c4f1554

Please sign in to comment.