Skip to content

Commit

Permalink
add --db_type_mapping flag
Browse files Browse the repository at this point in the history
  • Loading branch information
wubin1989 committed Oct 16, 2024
1 parent b8f47c7 commit f612a01
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
3 changes: 3 additions & 0 deletions cmd/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var dbTableGlob string
var dbTableExcludeGlob string
var dbGenGenGo bool
var dbOmitempty bool
var dbTypeMapping string

var crudCmd = &cobra.Command{
Use: "crud",
Expand All @@ -42,6 +43,7 @@ var crudCmd = &cobra.Command{
Soft: dbSoft,
Service: dbService,
Omitempty: dbOmitempty,
TypeMapping: dbTypeMapping,
}))
fn := strcase.ToLowerCamel
switch naming {
Expand All @@ -68,6 +70,7 @@ func init() {
crudCmd.Flags().StringVar(&dbTableGlob, "db_table_glob", "", `used to filter glob-matched tables`)
crudCmd.Flags().StringVar(&dbTableExcludeGlob, "db_table_exclude_glob", "", `used to filter glob-matched tables`)
crudCmd.Flags().StringVar(&naming, "case", "lowerCamel", `protobuf message field and json tag case, only support "lowerCamel" and "snake"`)
crudCmd.Flags().StringVar(&dbTypeMapping, "db_type_mapping", "", `Specify custom column type to go type mapping, multiple mappings can be joined by comma, eg. bigint:string,tinyint:bool`)
crudCmd.Flags().StringVar(&protocCmd, "grpc_gen_cmd", "protoc --proto_path=. --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative --go-json_out=. --go-json_opt=paths=source_relative,allow_unknown=true", `command to generate grpc service and message code`)
crudCmd.Flags().BoolVar(&dbOmitempty, "db_omitempty", false, `whether add omitempty json tag to generated model field"`)
}
1 change: 1 addition & 0 deletions cmd/internal/svc/codegen/database/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type OrmGeneratorConfig struct {
Soft string
ProtoGenerator v3.ProtoGenerator
Omitempty bool
TypeMapping string
}

type IOrmGenerator interface {
Expand Down
16 changes: 16 additions & 0 deletions cmd/internal/svc/codegen/database/gorm.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package database

import (
"github.com/samber/lo"
"github.com/unionj-cloud/toolkit/astutils"
"path/filepath"
"strings"
Expand Down Expand Up @@ -155,6 +156,21 @@ func (gg *GormGenerator) Initialize(conf OrmGeneratorConfig) {
}
return tagContent
})
if stringutils.IsNotEmpty(conf.TypeMapping) {
typeMapping := make(map[string]func(gorm.ColumnType) (dataType string))
lo.ForEach(strings.Split(conf.TypeMapping, ","), func(item string, index int) {
kv := strings.Split(item, ":")
columnType := kv[0]
goType := kv[1]
typeMapping[columnType] = func(columnType gorm.ColumnType) (dataType string) {
if n, ok := columnType.Nullable(); ok && n {
return "*" + goType
}
return goType
}
})
g.WithDataTypeMap(typeMapping)
}
g.UseDB(db)
g.GenGenGo = gg.GenGenGo
var models []interface{}
Expand Down
12 changes: 7 additions & 5 deletions cmd/internal/svc/svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,12 @@ type DbConfig struct {
TableGlob string
TableExcludeGlob string
// whether generate gen.go file
GenGenGo bool
Orm string
Soft string
Service string
Omitempty bool
GenGenGo bool
Orm string
Soft string
Service string
Omitempty bool
TypeMapping string
}

func (receiver *Svc) SetWatcher(w *watcher.Watcher) {
Expand Down Expand Up @@ -201,6 +202,7 @@ func (receiver *Svc) Crud() {
Soft: receiver.DbConfig.Soft,
ProtoGenerator: receiver.protoGenerator,
Omitempty: receiver.DbConfig.Omitempty,
TypeMapping: receiver.DbConfig.TypeMapping,
})
if stringutils.IsNotEmpty(receiver.DbConfig.Service) {
switch receiver.DbConfig.Service {
Expand Down
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package version

const Release = "v2.5.2"
const Release = "v2.5.3"

0 comments on commit f612a01

Please sign in to comment.