Skip to content

Commit

Permalink
feat(generate): diy data mapping
Browse files Browse the repository at this point in the history
close #190 #219
  • Loading branch information
tr1v3r committed Nov 14, 2021
1 parent 3b198cf commit debee82
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 7 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ A safer orm base on [GORM](https://github.com/go-gorm/gorm), aims to be develope
- [API Examples](#api-examples)
- [Generate](#generate)
- [Generate Model](#generate-model)
- [Data Mapping](#data-mapping)
- [Field Expression](#field-expression)
- [Create Field](#create-field)
- [CRUD API](#crud-api)
Expand Down Expand Up @@ -257,6 +258,25 @@ FieldRelate // specify relationship with other tables
FieldRelateModel // specify relationship with exist models
```

#### Data Mapping

Specify data mapping relationship to be whatever you want.

```go
dataMap := map[string]func(detailType string) (dataType string){
"int": func(detailType string) (dataType string) { return "int64" },
// bool mapping
"tinyint": func(detailType string) (dataType string) {
if strings.HasPrefix(detailType, "tinyint(1)") {
return "bool"
}
return "int8"
},
}

g.WithDataTypeMap(dataMap)
```

### Field Expression

#### Create Field
Expand Down
2 changes: 0 additions & 2 deletions field/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ var (
}
)

// TODO implement validator options

// ======================== generic field =======================

func NewField(table, column string, opts ...FieldOption) Field {
Expand Down
8 changes: 8 additions & 0 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type Config struct {

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

// WithDbNameOpts set get database name function
Expand All @@ -84,6 +85,12 @@ 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
}
}

func (cfg *Config) Revise() (err error) {
if cfg.ModelPkgPath == "" {
cfg.ModelPkgPath = check.DefaultModelPkg
Expand Down Expand Up @@ -160,6 +167,7 @@ func (g *Generator) GenerateModelAs(tableName string, modelName string, fieldOpt
ModelName: modelName,
SchemaNameOpts: g.dbNameOpts,
MemberOpts: fieldOpts,
DataTypeMap: g.dataTypeMap,
GenerateModelConfig: model.GenerateModelConfig{
FieldNullable: g.FieldNullable,
FieldWithIndexTag: g.FieldWithIndexTag,
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 @@ -58,6 +58,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)
m := field.ToMember(conf.FieldNullable)

if filterMember(m, filterOpts) == nil {
Expand Down
9 changes: 5 additions & 4 deletions internal/model/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ var keywords = []string{

var (
defaultDataType = "string"
dataType dataTypeMap = map[string]func(detailType string) string{
"int": func(string) string { return "int32" },
dataType dataTypeMap = map[string]dataTypeMapping{
"int": (func(string) string { return "int32" }),
"integer": func(string) string { return "int32" },
"smallint": func(string) string { return "int32" },
"mediumint": func(string) string { return "int32" },
Expand Down Expand Up @@ -93,9 +93,10 @@ var (
}
)

type dataTypeMap map[string]func(string) string
type dataTypeMapping func(detailType string) (finalType string)

type dataTypeMap map[string]dataTypeMapping

// TODO diy type map global or single
func (m dataTypeMap) Get(dataType, detailType string) string {
if convert, ok := m[dataType]; ok {
return convert(detailType)
Expand Down
2 changes: 2 additions & 0 deletions internal/model/db_conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type DBConf struct {
SchemaNameOpts []SchemaNameOpt
MemberOpts []MemberOpt

DataTypeMap map[string]func(detailType string) (dataType string)

GenerateModelConfig
}

Expand Down
18 changes: 17 additions & 1 deletion internal/model/tb_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type Column struct {
Extra string `gorm:"column:EXTRA"`
IsNullable string `gorm:"column:IS_NULLABLE"`
Indexes []*Index `gorm:"-"`

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

func (c *Column) IsPrimaryKey() bool {
Expand All @@ -28,8 +30,22 @@ func (c *Column) AutoIncrement() bool {
return c != nil && c.Extra == "auto_increment"
}

func (c *Column) SetDataTypeMap(m map[string]func(detailType string) (dataType string)) {
c.dataTypeMap = m
}

func (c *Column) GetDataType() (memberType string) {
if c.dataTypeMap == nil {
return dataType.Get(c.DataType, c.ColumnType)
}
if mapping, ok := c.dataTypeMap[c.DataType]; ok {
return mapping(c.ColumnType)
}
return dataType.Get(c.DataType, c.ColumnType)
}

func (c *Column) ToMember(nullable bool) *Member {
memberType := dataType.Get(c.DataType, c.ColumnType)
memberType := c.GetDataType()
if c.ColumnName == "deleted_at" && memberType == "time.Time" {
memberType = "gorm.DeletedAt"
}
Expand Down

0 comments on commit debee82

Please sign in to comment.