Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type Config struct {
fileNameNS func(tableName string) (fileName string)

dataTypeMap map[string]func(columnType gorm.ColumnType) (dataType string)
columnTypeMap map[string]func(columnType gorm.ColumnType) (colType string)
fieldJSONTagNS func(columnName string) (tagContent string)

modelOpts []ModelOpt
Expand Down Expand Up @@ -98,6 +99,11 @@ func (cfg *Config) WithDataTypeMap(newMap map[string]func(columnType gorm.Column
cfg.dataTypeMap = newMap
}

// WithColumnTypeMap specify column type mapping relationship, only work when syncing table from db
func (cfg *Config) WithColumnTypeMap(newMap map[string]func(columnType gorm.ColumnType) (colType string)) {
cfg.columnTypeMap = newMap
}

// WithJSONTagNameStrategy specify json tag naming strategy
func (cfg *Config) WithJSONTagNameStrategy(ns func(columnName string) (tagContent string)) {
cfg.fieldJSONTagNS = ns
Expand Down
3 changes: 2 additions & 1 deletion generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ func (g *Generator) genModelConfig(tableName string, modelName string, modelOpts
FileNameNS: g.fileNameNS,
},
FieldConfig: model.FieldConfig{
DataTypeMap: g.dataTypeMap,
DataTypeMap: g.dataTypeMap,
ColumnTypeMap: g.columnTypeMap,

FieldSignable: g.FieldSignable,
FieldNullable: g.FieldNullable,
Expand Down
1 change: 1 addition & 0 deletions internal/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
func getFields(db *gorm.DB, conf *model.Config, columns []*model.Column) (fields []*model.Field) {
for _, col := range columns {
col.SetDataTypeMap(conf.DataTypeMap)
col.SetColumnTypeMap(conf.ColumnTypeMap)
col.WithNS(conf.FieldJSONTagNS)

m := col.ToField(conf.FieldNullable, conf.FieldCoverable, conf.FieldSignable)
Expand Down
2 changes: 1 addition & 1 deletion internal/generate/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (m *InterfaceMethod) IsRepeatFromSameInterface(newMethod *InterfaceMethod)
return m.MethodName == newMethod.MethodName && m.InterfaceName == newMethod.InterfaceName && m.TargetStruct == newMethod.TargetStruct
}

//GetParamInTmpl return param list
// GetParamInTmpl return param list
func (m *InterfaceMethod) GetParamInTmpl() string {
return paramToString(m.Params)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/model/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ type NameStrategy struct {

// FieldConfig field configuration
type FieldConfig struct {
DataTypeMap map[string]func(columnType gorm.ColumnType) (dataType string)
DataTypeMap map[string]func(columnType gorm.ColumnType) (dataType string)
ColumnTypeMap map[string]func(columnType gorm.ColumnType) (colType string)

FieldNullable bool // generate pointer when field is nullable
FieldCoverable bool // generate pointer when field has default value
Expand Down
19 changes: 14 additions & 5 deletions internal/model/tbl_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,24 @@ import (
// Column table column's info
type Column struct {
gorm.ColumnType
TableName string `gorm:"column:TABLE_NAME"`
Indexes []*Index `gorm:"-"`
UseScanType bool `gorm:"-"`
dataTypeMap map[string]func(columnType gorm.ColumnType) (dataType string) `gorm:"-"`
jsonTagNS func(columnName string) string `gorm:"-"`
TableName string `gorm:"column:TABLE_NAME"`
Indexes []*Index `gorm:"-"`
UseScanType bool `gorm:"-"`
dataTypeMap map[string]func(columnType gorm.ColumnType) (dataType string) `gorm:"-"`
columnTypeMap map[string]func(columnType gorm.ColumnType) (colType string) `gorm:"-"`
jsonTagNS func(columnName string) string `gorm:"-"`
}

// SetDataTypeMap set data type map
func (c *Column) SetDataTypeMap(m map[string]func(columnType gorm.ColumnType) (dataType string)) {
c.dataTypeMap = m
}

// SetColumnTypeMap set column type map
func (c *Column) SetColumnTypeMap(m map[string]func(columnType gorm.ColumnType) (colType string)) {
c.columnTypeMap = m
}

// GetDataType get data type
func (c *Column) GetDataType() (fieldtype string) {
if mapping, ok := c.dataTypeMap[c.DatabaseTypeName()]; ok {
Expand Down Expand Up @@ -158,6 +164,9 @@ func (c *Column) defaultTagValue() string {

func (c *Column) columnType() (v string) {
if cl, ok := c.ColumnType.ColumnType(); ok {
if mapping, ok := c.columnTypeMap[cl]; ok {
return mapping(c.ColumnType)
}
return cl
}
return c.DatabaseTypeName()
Expand Down
Loading