diff --git a/README.ZH_CN.md b/README.ZH_CN.md index 99bca7c5..6369d947 100644 --- a/README.ZH_CN.md +++ b/README.ZH_CN.md @@ -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 diff --git a/README.md b/README.md index c1530f71..3be61760 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/config.go b/config.go index c4d85873..68dbbd13 100644 --- a/config.go +++ b/config.go @@ -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 @@ -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 diff --git a/generator.go b/generator.go index 0c0a0184..018fb43d 100644 --- a/generator.go +++ b/generator.go @@ -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, diff --git a/internal/check/gen_structs.go b/internal/check/gen_structs.go index d43a6821..94cbd6f0 100644 --- a/internal/check/gen_structs.go +++ b/internal/check/gen_structs.go @@ -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 diff --git a/internal/model/base.go b/internal/model/base.go index a2194681..6b4062a9 100644 --- a/internal/model/base.go +++ b/internal/model/base.go @@ -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" }, diff --git a/internal/model/conf.go b/internal/model/conf.go index 674064b0..c3c1e3a7 100644 --- a/internal/model/conf.go +++ b/internal/model/conf.go @@ -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 diff --git a/internal/model/tbl_column.go b/internal/model/tbl_column.go index f3fe63c0..5184f1fa 100644 --- a/internal/model/tbl_column.go +++ b/internal/model/tbl_column.go @@ -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" @@ -55,7 +58,13 @@ 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(), @@ -63,11 +72,8 @@ func (c *Column) ToField(nullable, coverable bool) *Field { 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 {