Skip to content

Commit

Permalink
Add ability to set custom column types in the gorm tag, so things suc…
Browse files Browse the repository at this point in the history
…h as lengths can be added.

This allows customisation in the same was as already implemented for data types using WithDataTypeMap.
  • Loading branch information
RParkerAvantia committed Jul 12, 2024
1 parent 5360707 commit 31d19c5
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
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 @@ -174,7 +174,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
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 @@ -155,6 +161,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

0 comments on commit 31d19c5

Please sign in to comment.