From 86a8b0e064c92fd9af92a1357a4064629796fa2f Mon Sep 17 00:00:00 2001 From: Richard Parker Date: Mon, 16 Jun 2025 10:37:57 +0100 Subject: [PATCH 1/4] Add ColumnTypeMap which allows bespoke customisation of column types. --- config.go | 6 ++++++ generator.go | 3 ++- helper/dataTypes.go | 15 +++++++++++++++ internal/generate/generate.go | 1 + internal/model/base.go | 10 ++++++++++ internal/model/config.go | 3 ++- internal/model/tbl_column.go | 19 ++++++++++++++----- 7 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 helper/dataTypes.go diff --git a/config.go b/config.go index 1f4c00ee..7b075059 100644 --- a/config.go +++ b/config.go @@ -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 @@ -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 diff --git a/generator.go b/generator.go index b90bbc79..4a5cca3d 100644 --- a/generator.go +++ b/generator.go @@ -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, diff --git a/helper/dataTypes.go b/helper/dataTypes.go new file mode 100644 index 00000000..a8fcb94f --- /dev/null +++ b/helper/dataTypes.go @@ -0,0 +1,15 @@ +package helper + +import ( + model "gorm.io/gen/internal/model" +) + +// Gets the code Type for the sqlDataType. +func GetDataType(sqlDataType string) string { + return model.GetDataType(sqlDataType) +} + +// Add or override values in the override map. +func SetDataType(dbType string, getTypeFunc func(detailType string) (finalType string)) { + model.SetDataType(dbType, getTypeFunc) +} diff --git a/internal/generate/generate.go b/internal/generate/generate.go index b2b23df3..3139bc3f 100644 --- a/internal/generate/generate.go +++ b/internal/generate/generate.go @@ -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) diff --git a/internal/model/base.go b/internal/model/base.go index e5cdcfd3..d267dfc9 100644 --- a/internal/model/base.go +++ b/internal/model/base.go @@ -162,6 +162,16 @@ func (m dataTypeMap) Get(dataType, detailType string) string { return defaultDataType } +// Gets the code Type for the sqlDataType. +func GetDataType(sqlDataType string) string { + return dataType.Get(sqlDataType, "") +} + +// Add or override values in the override map. +func SetDataType(dbType string, getTypeFunc dataTypeMapping) { + dataType[dbType] = getTypeFunc +} + // Field user input structures type Field struct { Name string diff --git a/internal/model/config.go b/internal/model/config.go index 2678082b..949bcbb5 100644 --- a/internal/model/config.go +++ b/internal/model/config.go @@ -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 diff --git a/internal/model/tbl_column.go b/internal/model/tbl_column.go index 7afb15ba..3b4f00bb 100644 --- a/internal/model/tbl_column.go +++ b/internal/model/tbl_column.go @@ -12,11 +12,12 @@ 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 @@ -24,6 +25,11 @@ func (c *Column) SetDataTypeMap(m map[string]func(columnType gorm.ColumnType) (d 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 { @@ -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() From 0c8896f3ae0bcb1224984fb5447fcc488494e394 Mon Sep 17 00:00:00 2001 From: Richard Parker Date: Mon, 16 Jun 2025 11:22:58 +0100 Subject: [PATCH 2/4] Add comments. --- helper/dataTypes.go | 41 ++++++++++++++++++++++++++--------------- internal/model/base.go | 18 ++++++++++++++++-- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/helper/dataTypes.go b/helper/dataTypes.go index a8fcb94f..ff01d3b9 100644 --- a/helper/dataTypes.go +++ b/helper/dataTypes.go @@ -1,15 +1,26 @@ -package helper - -import ( - model "gorm.io/gen/internal/model" -) - -// Gets the code Type for the sqlDataType. -func GetDataType(sqlDataType string) string { - return model.GetDataType(sqlDataType) -} - -// Add or override values in the override map. -func SetDataType(dbType string, getTypeFunc func(detailType string) (finalType string)) { - model.SetDataType(dbType, getTypeFunc) -} +package helper + +import ( + model "gorm.io/gen/internal/model" +) + +// GetDataType returns the corresponding Go data type for a given SQL data type string. +// It delegates the type mapping logic to the model.GetDataType function. +// +// Parameters: +// +// sqlDataType - the SQL data type as a string. +// +// Returns: +// +// The Go data type as a string. +func GetDataType(sqlDataType string) string { + return model.GetDataType(sqlDataType) +} + +// SetDataType registers a mapping between a database type and a function that determines the final type. +// The getTypeFunc parameter is a function that takes a detailType string and returns the corresponding finalType string. +// This function delegates the registration to the model.SetDataType function. +func SetDataType(dbType string, getTypeFunc func(detailType string) (finalType string)) { + model.SetDataType(dbType, getTypeFunc) +} diff --git a/internal/model/base.go b/internal/model/base.go index d267dfc9..c2732911 100644 --- a/internal/model/base.go +++ b/internal/model/base.go @@ -162,12 +162,26 @@ func (m dataTypeMap) Get(dataType, detailType string) string { return defaultDataType } -// Gets the code Type for the sqlDataType. +// GetDataType returns the corresponding Go data type for a given SQL data type string. +// It uses the dataType mapping to look up the Go type associated with the provided SQL data type. +// If the SQL data type is not found in the mapping, it returns an empty string. +// +// Parameters: +// - sqlDataType: the SQL data type as a string. +// +// Returns: +// - The Go data type as a string, or an empty string if not found. func GetDataType(sqlDataType string) string { return dataType.Get(sqlDataType, "") } -// Add or override values in the override map. +// SetDataType registers a mapping function for a specific database type. +// It associates the provided dbType string with the getTypeFunc, which defines +// how to map the database type to the application's data type. +// +// Parameters: +// - dbType: the name of the database type to map. +// - getTypeFunc: a function that returns the application's data type for the given dbType. func SetDataType(dbType string, getTypeFunc dataTypeMapping) { dataType[dbType] = getTypeFunc } From cbf1b4451f2c47300263678416103b8c41b4d0b3 Mon Sep 17 00:00:00 2001 From: Richard Parker Date: Mon, 16 Jun 2025 11:25:10 +0100 Subject: [PATCH 3/4] Fix comment formatting. --- helper/dataTypes.go | 6 ++---- internal/generate/interface.go | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/helper/dataTypes.go b/helper/dataTypes.go index ff01d3b9..46856712 100644 --- a/helper/dataTypes.go +++ b/helper/dataTypes.go @@ -8,12 +8,10 @@ import ( // It delegates the type mapping logic to the model.GetDataType function. // // Parameters: -// -// sqlDataType - the SQL data type as a string. +// - sqlDataType: the SQL data type as a string. // // Returns: -// -// The Go data type as a string. +// - The Go data type as a string. func GetDataType(sqlDataType string) string { return model.GetDataType(sqlDataType) } diff --git a/internal/generate/interface.go b/internal/generate/interface.go index f43bf56f..7c76ff24 100644 --- a/internal/generate/interface.go +++ b/internal/generate/interface.go @@ -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) } From fa0562722272547bb37642ec60dda62f41222b1e Mon Sep 17 00:00:00 2001 From: Richard Parker Date: Tue, 17 Jun 2025 08:48:03 +0100 Subject: [PATCH 4/4] Remove GetDataType and SetDataType. --- helper/dataTypes.go | 24 ------------------------ internal/model/base.go | 24 ------------------------ 2 files changed, 48 deletions(-) delete mode 100644 helper/dataTypes.go diff --git a/helper/dataTypes.go b/helper/dataTypes.go deleted file mode 100644 index 46856712..00000000 --- a/helper/dataTypes.go +++ /dev/null @@ -1,24 +0,0 @@ -package helper - -import ( - model "gorm.io/gen/internal/model" -) - -// GetDataType returns the corresponding Go data type for a given SQL data type string. -// It delegates the type mapping logic to the model.GetDataType function. -// -// Parameters: -// - sqlDataType: the SQL data type as a string. -// -// Returns: -// - The Go data type as a string. -func GetDataType(sqlDataType string) string { - return model.GetDataType(sqlDataType) -} - -// SetDataType registers a mapping between a database type and a function that determines the final type. -// The getTypeFunc parameter is a function that takes a detailType string and returns the corresponding finalType string. -// This function delegates the registration to the model.SetDataType function. -func SetDataType(dbType string, getTypeFunc func(detailType string) (finalType string)) { - model.SetDataType(dbType, getTypeFunc) -} diff --git a/internal/model/base.go b/internal/model/base.go index c2732911..e5cdcfd3 100644 --- a/internal/model/base.go +++ b/internal/model/base.go @@ -162,30 +162,6 @@ func (m dataTypeMap) Get(dataType, detailType string) string { return defaultDataType } -// GetDataType returns the corresponding Go data type for a given SQL data type string. -// It uses the dataType mapping to look up the Go type associated with the provided SQL data type. -// If the SQL data type is not found in the mapping, it returns an empty string. -// -// Parameters: -// - sqlDataType: the SQL data type as a string. -// -// Returns: -// - The Go data type as a string, or an empty string if not found. -func GetDataType(sqlDataType string) string { - return dataType.Get(sqlDataType, "") -} - -// SetDataType registers a mapping function for a specific database type. -// It associates the provided dbType string with the getTypeFunc, which defines -// how to map the database type to the application's data type. -// -// Parameters: -// - dbType: the name of the database type to map. -// - getTypeFunc: a function that returns the application's data type for the given dbType. -func SetDataType(dbType string, getTypeFunc dataTypeMapping) { - dataType[dbType] = getTypeFunc -} - // Field user input structures type Field struct { Name string