Skip to content

Commit

Permalink
feat(generate): specify field generate with gorm type tag
Browse files Browse the repository at this point in the history
  • Loading branch information
tr1v3r committed Nov 8, 2021
1 parent 706cc55 commit d3fd714
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 19 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,10 @@ func main() {
/* Mode: gen.WithoutContext|gen.WithDefaultQuery*/
//if you want the nullable field generation property to be pointer type, set FieldNullable true
/* FieldNullable: true,*/
//if you want to generate index tags from the database, set FieldWithIndexTag true
//if you want to generate index tags from database, set FieldWithIndexTag true
/* FieldWithIndexTag: true,*/
//if you want to generate type tags from database, set FieldWithTypeTag true
/* FieldWithTypeTag: true,*/
//if you need unit tests for query code, set WithUnitTest true
/* WithUnitTest: true, */
})
Expand Down
32 changes: 19 additions & 13 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,15 @@ const (
type Config struct {
db *gorm.DB // db connection

OutPath string // query code path
OutFile string // query code file name, default: gen.go
ModelPkgPath string // generated model code's package name
FieldNullable bool // generate pointer when field is nullable
FieldWithIndexTag bool // generate with gorm index tag
WithUnitTest bool // generate unit test for query code
OutPath string // query code path
OutFile string // query code file name, default: gen.go
ModelPkgPath string // generated model code's package name
WithUnitTest bool // generate unit test for query code

// generate model global configuration
FieldNullable bool // generate pointer when field is nullable
FieldWithIndexTag bool // generate with gorm index tag
FieldWithTypeTag bool // generate with gorm column type ta

Mode GenerateMode // generate mode

Expand Down Expand Up @@ -153,13 +156,16 @@ func (g *Generator) GenerateModel(tableName string, opts ...model.MemberOpt) *ch
// GenerateModel catch table info from db, return a BaseStruct
func (g *Generator) GenerateModelAs(tableName string, modelName string, fieldOpts ...model.MemberOpt) *check.BaseStruct {
s, err := check.GenBaseStructs(g.db, model.DBConf{
ModelPkg: g.Config.ModelPkgPath,
TableName: tableName,
ModelName: modelName,
SchemaNameOpts: g.dbNameOpts,
MemberOpts: fieldOpts,
FieldNullable: g.FieldNullable,
FieldWithIndexTag: g.FieldWithIndexTag,
ModelPkg: g.Config.ModelPkgPath,
TableName: tableName,
ModelName: modelName,
SchemaNameOpts: g.dbNameOpts,
MemberOpts: fieldOpts,
GenerateModelConfig: model.GenerateModelConfig{
FieldNullable: g.FieldNullable,
FieldWithIndexTag: g.FieldWithIndexTag,
FieldWithTypeTag: g.FieldWithTypeTag,
},
})
if err != nil {
g.db.Logger.Error(context.Background(), "generate struct from table fail: %s", err)
Expand Down
4 changes: 4 additions & 0 deletions internal/check/gen_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ func GenBaseStructs(db *gorm.DB, conf model.DBConf) (bases *BaseStruct, err erro
continue
}

if !conf.FieldWithTypeTag { // remove type tag if FieldWithTypeTag == false
m.GORMTag = strings.ReplaceAll(m.GORMTag, ";type:"+field.ColumnType, "")
}

m = modifyMember(m, modifyOpts)
if ns, ok := db.NamingStrategy.(schema.NamingStrategy); ok {
ns.SingularTable = true
Expand Down
9 changes: 7 additions & 2 deletions internal/model/db_conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ type DBConf struct {
SchemaNameOpts []SchemaNameOpt
MemberOpts []MemberOpt

FieldNullable bool
FieldWithIndexTag bool
GenerateModelConfig
}

type GenerateModelConfig struct {
FieldNullable bool // generate pointer when field is nullable
FieldWithIndexTag bool // generate with gorm index tag
FieldWithTypeTag bool // generate with gorm column type tagl
}

func (cf *DBConf) SortOpt() (modifyOpts []MemberOpt, filterOpts []MemberOpt, createOpts []MemberOpt) {
Expand Down
6 changes: 3 additions & 3 deletions internal/template/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ const (
_{{$.NewStructName}}.fieldMap = make(map[string]field.Expr, {{len .Members}})
{{range .Members -}}
{{if not .IsRelation -}}
_{{$.NewStructName}}.fieldMap["{{.ColumnName}}"] = _{{$.NewStructName}}.{{.Name}}
{{end -}}
{{if not .IsRelation -}}{{if .ColumnName -}}
_{{$.NewStructName}}.fieldMap["{{.ColumnName}}"] = _{{$.NewStructName}}.{{.Name}}
{{end -}}{{end -}}
{{end}}
return _{{.NewStructName}}
}
Expand Down

0 comments on commit d3fd714

Please sign in to comment.