Skip to content

Commit cf7fee2

Browse files
committed
Add option WithNullableNameStrategy to use custom nullable column type formater
1 parent f534623 commit cf7fee2

File tree

5 files changed

+33
-14
lines changed

5 files changed

+33
-14
lines changed

config.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ type Config struct {
5454
modelNameNS func(tableName string) (modelName string)
5555
fileNameNS func(tableName string) (fileName string)
5656

57-
dataTypeMap map[string]func(columnType gorm.ColumnType) (dataType string)
58-
fieldJSONTagNS func(columnName string) (tagContent string)
57+
dataTypeMap map[string]func(columnType gorm.ColumnType) (dataType string)
58+
fieldJSONTagNS func(columnName string) (tagContent string)
59+
fieldNullableNS func(columnType string) (nullableColumnType string)
5960

6061
modelOpts []ModelOpt
6162
}
@@ -103,6 +104,11 @@ func (cfg *Config) WithJSONTagNameStrategy(ns func(columnName string) (tagConten
103104
cfg.fieldJSONTagNS = ns
104105
}
105106

107+
// WithNullableNameStrategy specify nullable type name strategy
108+
func (cfg *Config) WithNullableNameStrategy(ns func(columnType string) (nullableColumnType string)) {
109+
cfg.fieldNullableNS = ns
110+
}
111+
106112
// WithImportPkgPath specify import package path
107113
func (cfg *Config) WithImportPkgPath(paths ...string) {
108114
for i, path := range paths {

generator.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ func (g *Generator) genModelConfig(tableName string, modelName string, modelOpts
183183
FieldWithIndexTag: g.FieldWithIndexTag,
184184
FieldWithTypeTag: g.FieldWithTypeTag,
185185

186-
FieldJSONTagNS: g.fieldJSONTagNS,
186+
FieldNullableNS: g.fieldNullableNS,
187+
FieldJSONTagNS: g.fieldJSONTagNS,
187188
},
188189
}
189190
}

internal/generate/generate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func getFields(db *gorm.DB, conf *model.Config, columns []*model.Column) (fields
2020
for _, col := range columns {
2121
col.SetDataTypeMap(conf.DataTypeMap)
2222
col.WithNS(conf.FieldJSONTagNS)
23+
col.WithNullableFieldTypeNS(conf.FieldNullableNS)
2324

2425
m := col.ToField(conf.FieldNullable, conf.FieldCoverable, conf.FieldSignable)
2526

internal/model/config.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,18 @@ type NameStrategy struct {
3535
type FieldConfig struct {
3636
DataTypeMap map[string]func(columnType gorm.ColumnType) (dataType string)
3737

38-
FieldNullable bool // generate pointer when field is nullable
38+
FieldNullable bool // Use FieldNullableFormater when field is nullable
3939
FieldCoverable bool // generate pointer when field has default value
4040
FieldSignable bool // detect integer field's unsigned type, adjust generated data type
4141
FieldWithIndexTag bool // generate with gorm index tag
4242
FieldWithTypeTag bool // generate with gorm column type tag
4343

4444
FieldJSONTagNS func(columnName string) string
45-
46-
ModifyOpts []FieldOption
47-
FilterOpts []FieldOption
48-
CreateOpts []FieldOption
45+
// Formater used when field is nullable and FieldNullable is true. Default : generate pointer
46+
FieldNullableNS func(columnType string) string
47+
ModifyOpts []FieldOption
48+
FilterOpts []FieldOption
49+
CreateOpts []FieldOption
4950
}
5051

5152
// MethodConfig method configuration

internal/model/tbl_column.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ import (
1212
// Column table column's info
1313
type Column struct {
1414
gorm.ColumnType
15-
TableName string `gorm:"column:TABLE_NAME"`
16-
Indexes []*Index `gorm:"-"`
17-
UseScanType bool `gorm:"-"`
18-
dataTypeMap map[string]func(columnType gorm.ColumnType) (dataType string) `gorm:"-"`
19-
jsonTagNS func(columnName string) string `gorm:"-"`
15+
TableName string `gorm:"column:TABLE_NAME"`
16+
Indexes []*Index `gorm:"-"`
17+
UseScanType bool `gorm:"-"`
18+
dataTypeMap map[string]func(columnType gorm.ColumnType) (dataType string) `gorm:"-"`
19+
jsonTagNS func(columnName string) string `gorm:"-"`
20+
nullableTypeFormater func(columnType string) string `gorm:"-"`
2021
}
2122

2223
// SetDataTypeMap set data type map
@@ -43,6 +44,15 @@ func (c *Column) WithNS(jsonTagNS func(columnName string) string) {
4344
}
4445
}
4546

47+
// WithNullableFieldTypeNS with nullable type name strategy
48+
// If nil use pointer, the default
49+
func (c *Column) WithNullableFieldTypeNS(formater func(columnType string) string) {
50+
c.nullableTypeFormater = formater
51+
if c.nullableTypeFormater == nil {
52+
c.nullableTypeFormater = func(fType string) string { return "*" + fType }
53+
}
54+
}
55+
4656
// ToField convert to field
4757
func (c *Column) ToField(nullable, coverable, signable bool) *Field {
4858
fieldType := c.GetDataType()
@@ -56,7 +66,7 @@ func (c *Column) ToField(nullable, coverable, signable bool) *Field {
5666
fieldType = "*" + fieldType
5767
case nullable && !strings.HasPrefix(fieldType, "*"):
5868
if n, ok := c.Nullable(); ok && n {
59-
fieldType = "*" + fieldType
69+
fieldType = c.nullableTypeFormater(fieldType)
6070
}
6171
}
6272

0 commit comments

Comments
 (0)