Skip to content

Commit

Permalink
add db_gen_gen
Browse files Browse the repository at this point in the history
  • Loading branch information
wubin1989 committed Apr 10, 2024
1 parent bbf8274 commit 138d6d1
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 6 deletions.
3 changes: 3 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var dbService bool
var dbTablePrefix string
var dbTableGlob string
var module bool
var dbGenGenGo bool

// initCmd initializes the service
var initCmd = &cobra.Command{
Expand All @@ -41,6 +42,7 @@ var initCmd = &cobra.Command{
Dsn: dbDsn,
TablePrefix: dbTablePrefix,
TableGlob: dbTableGlob,
GenGenGo: dbGenGenGo,
Orm: dbOrm,
Soft: dbSoft,
Grpc: dbGrpc,
Expand Down Expand Up @@ -72,6 +74,7 @@ func init() {
initCmd.Flags().StringVar(&dbSoft, "db_soft", "deleted_at", `Specify database soft delete column name`)
initCmd.Flags().BoolVar(&dbGrpc, "db_grpc", false, `If true, grpc code will also be generated`)
initCmd.Flags().BoolVar(&dbService, "db_service", false, `If false, service will not be generated, and db_grpc will be ignored. Only dao layer code will be generated.`)
initCmd.Flags().BoolVar(&dbGenGenGo, "db_gen_gen", false, `whether generate gen.go file`)
initCmd.Flags().StringVar(&dbTablePrefix, "db_table_prefix", "", `table prefix or schema name for pg`)
initCmd.Flags().StringVar(&dbTableGlob, "db_table_glob", "", `used to filter glob-matched tables`)
initCmd.Flags().StringVar(&naming, "case", "lowerCamel", `protobuf message field and json tag case, only support "lowerCamel" and "snake"`)
Expand Down
2 changes: 2 additions & 0 deletions cmd/internal/svc/codegen/database/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type OrmGeneratorConfig struct {
Dsn string
TablePrefix string
TableGlob string
GenGenGo bool
CaseConverter func(string) string
Dir string
Soft string
Expand All @@ -62,6 +63,7 @@ type AbstractBaseGenerator struct {
Dsn string
TablePrefix string
TableGlob string
GenGenGo bool
Dir string
g *gormgen.Generator
CaseConverter func(string) string
Expand Down
2 changes: 2 additions & 0 deletions cmd/internal/svc/codegen/database/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func (gg *GormGenerator) Initialize(conf OrmGeneratorConfig) {
gg.Grpc = conf.Grpc
gg.TablePrefix = strings.TrimSuffix(conf.TablePrefix, ".")
gg.TableGlob = conf.TableGlob
gg.GenGenGo = conf.GenGenGo
gg.CaseConverter = conf.CaseConverter
var db *gorm.DB
var err error
Expand Down Expand Up @@ -157,6 +158,7 @@ func (gg *GormGenerator) Initialize(conf OrmGeneratorConfig) {
g.WithJSONTagNameStrategy(func(n string) string { return gg.CaseConverter(n) + ",omitempty" })
g.WithImportPkgPath("github.com/unionj-cloud/go-doudou/v2/toolkit/customtypes")
g.UseDB(db)
g.GenGenGo = gg.GenGenGo
var models []interface{}
if stringutils.IsNotEmpty(gg.TableGlob) {
g.FilterTableGlob = glob.MustCompile(gg.TableGlob)
Expand Down
11 changes: 7 additions & 4 deletions cmd/internal/svc/svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,12 @@ type DbConfig struct {
// or schema for pg
TablePrefix string
TableGlob string
Orm string
Soft string
Grpc bool
Service bool
// whether generate gen.go file
GenGenGo bool
Orm string
Soft string
Grpc bool
Service bool
}

func (receiver *Svc) SetWatcher(w *watcher.Watcher) {
Expand Down Expand Up @@ -188,6 +190,7 @@ func (receiver *Svc) Init() {
Dsn: receiver.DbConfig.Dsn,
TablePrefix: receiver.DbConfig.TablePrefix,
TableGlob: receiver.DbConfig.TableGlob,
GenGenGo: receiver.DbConfig.GenGenGo,
CaseConverter: receiver.CaseConverter,
Dir: receiver.dir,
Soft: receiver.DbConfig.Soft,
Expand Down
1 change: 1 addition & 0 deletions toolkit/gormgen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type Config struct {
ConfigPackage string

FilterTableGlob glob.Glob
GenGenGo bool
}

// WithOpts set global model options
Expand Down
2 changes: 1 addition & 1 deletion toolkit/gormgen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ func (g *Generator) generateQueryFile() (err error) {
case <-pool.AsyncWaitAll():
}

if g.FilterTableGlob != nil {
if !g.GenGenGo {
return nil
}

Expand Down
1 change: 0 additions & 1 deletion toolkit/pagination/gorm/paginate.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ func (r *resContext) Response(res interface{}) Page {
hasAdapter = true
if cKey != "" && adapter.IsValid(cKey) {
if cache, err := adapter.Get(cKey); nil == err {
page.Items, _ = sliceutils.ConvertAny2Interface(res)
if err := p.Config.JSONUnmarshal([]byte(cache), &page); nil == err {
return page
}
Expand Down

0 comments on commit 138d6d1

Please sign in to comment.