Skip to content

Commit

Permalink
feat(generate): allow user specify tag name stragety (go-gorm#239)
Browse files Browse the repository at this point in the history
* feat(generate): allow user specify tag name stragety
close go-gorm#234

* style(generate): name return value
  • Loading branch information
tr1v3r authored Nov 22, 2021
1 parent 35c9603 commit 29e55ba
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 8 deletions.
9 changes: 9 additions & 0 deletions field_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ var (
return m
}
}
// FieldJSONTagWithNS specify JSON tag with name strategy
FieldJSONTagWithNS = func(schemaName func(columnName string) (tagContent string)) model.ModifyMemberOpt {
return func(m *model.Member) *model.Member {
if schemaName != nil {
m.JSONTag = schemaName(m.ColumnName)
}
return m
}
}
// FieldGORMTag specify GORM tag
FieldGORMTag = func(columnName string, gormTag string) model.ModifyMemberOpt {
return func(m *model.Member) *model.Member {
Expand Down
25 changes: 18 additions & 7 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ type Config struct {
// 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
FieldWithTypeTag bool // generate with gorm column type tag

Mode GenerateMode // generate mode

queryPkgName string // generated query code's package name
dbNameOpts []model.SchemaNameOpt
dataTypeMap map[string]func(detailType string) (dataType string)
queryPkgName string // generated query code's package name
dbNameOpts []model.SchemaNameOpt
dataTypeMap map[string]func(detailType string) (dataType string)
fieldJSONTagNS func(columnName string) string
fieldNewTagNS func(columnName string) string
}

// WithDbNameOpts set get database name function
Expand All @@ -86,9 +88,15 @@ func (cfg *Config) WithDbNameOpts(opts ...model.SchemaNameOpt) {
}

func (cfg *Config) WithDataTypeMap(newMap map[string]func(detailType string) (dataType string)) {
if newMap != nil {
cfg.dataTypeMap = newMap
}
cfg.dataTypeMap = newMap
}

func (cfg *Config) WithJSONTagNameStrategy(ns func(columnName string) (tagContent string)) {
cfg.fieldJSONTagNS = ns
}

func (cfg *Config) WithNewTagNameStrategy(ns func(columnName string) (tagContent string)) {
cfg.fieldNewTagNS = ns
}

func (cfg *Config) Revise() (err error) {
Expand Down Expand Up @@ -172,6 +180,9 @@ func (g *Generator) GenerateModelAs(tableName string, modelName string, fieldOpt
FieldNullable: g.FieldNullable,
FieldWithIndexTag: g.FieldWithIndexTag,
FieldWithTypeTag: g.FieldWithTypeTag,

FieldJSONTagNS: g.fieldJSONTagNS,
FieldNewTagNS: g.fieldNewTagNS,
},
})
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/check/gen_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func GenBaseStructs(db *gorm.DB, conf model.DBConf) (bases *BaseStruct, err erro
modifyOpts, filterOpts, createOpts := conf.SortOpt()
for _, field := range columns {
field.SetDataTypeMap(conf.DataTypeMap)
field.WithNS(conf.FieldJSONTagNS, conf.FieldNewTagNS)
m := field.ToMember(conf.FieldNullable)

if filterMember(m, filterOpts) == nil {
Expand Down
3 changes: 3 additions & 0 deletions internal/model/db_conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ 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

FieldJSONTagNS func(columnName string) string
FieldNewTagNS func(columnName string) string
}

func (cf *DBConf) SortOpt() (modifyOpts []MemberOpt, filterOpts []MemberOpt, createOpts []MemberOpt) {
Expand Down
15 changes: 14 additions & 1 deletion internal/model/tb_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type Column struct {
Indexes []*Index `gorm:"-"`

dataTypeMap map[string]func(detailType string) (dataType string) `gorm:"-"`
jsonTagNS func(columnName string) string `gorm:"-"`
newTagNS func(columnName string) string `gorm:"-"`
}

func (c *Column) IsPrimaryKey() bool {
Expand All @@ -41,6 +43,16 @@ func (c *Column) GetDataType() (memberType string) {
return dataType.Get(c.DataType, c.ColumnType)
}

func (c *Column) WithNS(jsonTagNS, newTagNS func(columnName string) string) {
c.jsonTagNS, c.newTagNS = jsonTagNS, newTagNS
if c.jsonTagNS == nil {
c.jsonTagNS = func(n string) string { return n }
}
if c.newTagNS == nil {
c.newTagNS = func(string) string { return "" }
}
}

func (c *Column) ToMember(nullable bool) *Member {
memberType := c.GetDataType()
if c.ColumnName == "deleted_at" && memberType == "time.Time" {
Expand All @@ -55,7 +67,8 @@ func (c *Column) ToMember(nullable bool) *Member {
ColumnComment: c.ColumnComment,
MultilineComment: c.multilineComment(),
GORMTag: c.buildGormTag(),
JSONTag: c.ColumnName,
JSONTag: c.jsonTagNS(c.ColumnName),
NewTag: c.newTagNS(c.ColumnName),
}
}

Expand Down

0 comments on commit 29e55ba

Please sign in to comment.