Skip to content

Commit

Permalink
feat: generate unsigned integer type (go-gorm#426)
Browse files Browse the repository at this point in the history
* feat: generate unsigned integer type

* feat: field signable

* docs: field signable
  • Loading branch information
tr1v3r authored Apr 20, 2022
1 parent 97c3326 commit 0a07059
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.ZH_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ func main() {
/* FieldNullable: true,*/
//if you want to assign field which has default value in `Create` API, set FieldCoverable true, reference: https://gorm.io/docs/create.html#Default-Values
/* FieldCoverable: true,*/
// if you want generate field with unsigned integer type, set FieldSignable true
/* FieldSignable: true,*/
//if you want to generate index tags from database, set FieldWithIndexTag true
/* FieldWithIndexTag: true,*/
//if you want to generate type tags from database, set FieldWithTypeTag true
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ func main() {
/* FieldNullable: true,*/
//if you want to assign field which has default value in `Create` API, set FieldCoverable true, reference: https://gorm.io/docs/create.html#Default-Values
/* FieldCoverable: true,*/
// if you want generate field with unsigned integer type, set FieldSignable true
/* FieldSignable: true,*/
//if you want to generate index tags from database, set FieldWithIndexTag true
/* FieldWithIndexTag: true,*/
//if you want to generate type tags from database, set FieldWithTypeTag true
Expand Down
6 changes: 4 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"path/filepath"
"strings"

"gorm.io/gen/internal/check"
"gorm.io/gen/internal/model"
"gorm.io/gorm"
"gorm.io/gorm/utils/tests"

"gorm.io/gen/internal/check"
"gorm.io/gen/internal/model"
)

type GenerateMode uint
Expand All @@ -33,6 +34,7 @@ type Config struct {
// generate model global configuration
FieldNullable bool // generate pointer when field is nullable
FieldCoverable bool // generate pointer when field has default value, to fix problem zero value cannot be assign: https://gorm.io/docs/create.html#Default-Values
FieldSignable bool // detect integer field's unsigned type, adjust generated data type
FieldWithIndexTag bool // generate with gorm index tag
FieldWithTypeTag bool // generate with gorm column type tag

Expand Down
1 change: 1 addition & 0 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func (g *Generator) GenerateModelAs(tableName string, modelName string, fieldOpt
FieldConf: model.FieldConf{
DataTypeMap: g.dataTypeMap,

FieldSignable: g.FieldSignable,
FieldNullable: g.FieldNullable,
FieldCoverable: g.FieldCoverable,
FieldWithIndexTag: g.FieldWithIndexTag,
Expand Down
2 changes: 1 addition & 1 deletion internal/check/gen_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func GenBaseStruct(db *gorm.DB, conf model.Conf) (base *BaseStruct, err error) {
col.SetDataTypeMap(conf.DataTypeMap)
col.WithNS(conf.FieldJSONTagNS, conf.FieldNewTagNS)

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

if filterField(m, filterOpts) == nil {
continue
Expand Down
2 changes: 1 addition & 1 deletion internal/model/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ func (g *KeyWords) Contain(text string) bool {
var (
defaultDataType = "string"
dataType dataTypeMap = map[string]dataTypeMapping{
"int": func(string) string { return "int32" },
"numeric": func(string) string { return "int32" },
"integer": func(string) string { return "int32" },
"int": func(string) string { return "int32" },
"smallint": func(string) string { return "int32" },
"mediumint": func(string) string { return "int32" },
"bigint": func(string) string { return "int64" },
Expand Down
1 change: 1 addition & 0 deletions internal/model/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type FieldConf struct {

FieldNullable bool // generate pointer when field is nullable
FieldCoverable bool // generate pointer when field has default value
FieldSignable bool // detect integer field's unsigned type, adjust generated data type
FieldWithIndexTag bool // generate with gorm index tag
FieldWithTypeTag bool // generate with gorm column type tagl

Expand Down
18 changes: 12 additions & 6 deletions internal/model/tbl_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ func (c *Column) WithNS(jsonTagNS, newTagNS func(columnName string) string) {
}
}

func (c *Column) ToField(nullable, coverable bool) *Field {
func (c *Column) ToField(nullable, coverable, signable bool) *Field {
fieldType := c.GetDataType()
if signable && strings.Contains(c.columnType(), "unsigned") && strings.HasPrefix(fieldType, "int") {
fieldType = "u" + fieldType
}
switch {
case c.Name() == "deleted_at" && fieldType == "time.Time":
fieldType = "gorm.DeletedAt"
Expand All @@ -55,19 +58,22 @@ func (c *Column) ToField(nullable, coverable bool) *Field {
case coverable && c.withDefaultValue():
fieldType = "*" + fieldType
}
f := &Field{

var comment string
if c, ok := c.Comment(); ok {
comment = c
}

return &Field{
Name: c.Name(),
Type: fieldType,
ColumnName: c.Name(),
MultilineComment: c.multilineComment(),
GORMTag: c.buildGormTag(),
JSONTag: c.jsonTagNS(c.Name()),
NewTag: c.newTagNS(c.Name()),
ColumnComment: comment,
}
if c, ok := c.Comment(); ok {
f.ColumnComment = c
}
return f
}

func (c *Column) multilineComment() bool {
Expand Down

0 comments on commit 0a07059

Please sign in to comment.