gorm/schema/field.go

202 lines
4.8 KiB
Go
Raw Normal View History

2020-01-31 07:22:37 +03:00
package schema
import (
"database/sql/driver"
"reflect"
"strconv"
"sync"
"time"
)
type DataType string
2020-01-31 07:22:37 +03:00
const (
Bool DataType = "bool"
Int = "int"
Uint = "uint"
Float = "float"
String = "string"
Time = "time"
Bytes = "bytes"
2020-01-31 07:22:37 +03:00
)
type Field struct {
Name string
DBName string
BindNames []string
DataType DataType
2020-01-31 07:22:37 +03:00
DBDataType string
PrimaryKey bool
AutoIncrement bool
Creatable bool
Updatable bool
HasDefaultValue bool
DefaultValue string
NotNull bool
Unique bool
Comment string
Size int
Precision int
FieldType reflect.Type
StructField reflect.StructField
Tag reflect.StructTag
TagSettings map[string]string
Schema *Schema
EmbeddedSchema *Schema
2020-01-31 07:22:37 +03:00
}
func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
field := &Field{
Name: fieldStruct.Name,
BindNames: []string{fieldStruct.Name},
FieldType: fieldStruct.Type,
StructField: fieldStruct,
Creatable: true,
Updatable: true,
Tag: fieldStruct.Tag,
TagSettings: ParseTagSetting(fieldStruct.Tag),
Schema: schema,
2020-01-31 07:22:37 +03:00
}
for field.FieldType.Kind() == reflect.Ptr {
field.FieldType = field.FieldType.Elem()
}
fieldValue := reflect.New(field.FieldType)
// if field is valuer, used its value or first fields as data type
if valuer, isValuer := fieldValue.Interface().(driver.Valuer); isValuer {
var overrideFieldValue bool
if v, err := valuer.Value(); v != nil && err == nil {
overrideFieldValue = true
fieldValue = reflect.ValueOf(v)
}
if field.FieldType.Kind() == reflect.Struct {
for i := 0; i < field.FieldType.NumField(); i++ {
if !overrideFieldValue {
newFieldType := field.FieldType.Field(i).Type
for newFieldType.Kind() == reflect.Ptr {
newFieldType = newFieldType.Elem()
}
fieldValue = reflect.New(newFieldType)
overrideFieldValue = true
}
// copy tag settings from valuer
for key, value := range ParseTagSetting(field.FieldType.Field(i).Tag) {
2020-01-31 07:22:37 +03:00
if _, ok := field.TagSettings[key]; !ok {
field.TagSettings[key] = value
}
}
}
}
}
// setup permission
if _, ok := field.TagSettings["-"]; ok {
field.Creatable = false
field.Updatable = false
}
if dbName, ok := field.TagSettings["COLUMN"]; ok {
field.DBName = dbName
}
2020-02-01 13:02:19 +03:00
if val, ok := field.TagSettings["PRIMARYKEY"]; ok && checkTruth(val) {
2020-01-31 07:22:37 +03:00
field.PrimaryKey = true
}
2020-02-01 13:02:19 +03:00
if val, ok := field.TagSettings["AUTOINCREMENT"]; ok && checkTruth(val) {
2020-01-31 07:22:37 +03:00
field.AutoIncrement = true
field.HasDefaultValue = true
}
if v, ok := field.TagSettings["DEFAULT"]; ok {
field.HasDefaultValue = true
field.DefaultValue = v
}
if num, ok := field.TagSettings["SIZE"]; ok {
field.Size, _ = strconv.Atoi(num)
}
if p, ok := field.TagSettings["PRECISION"]; ok {
field.Precision, _ = strconv.Atoi(p)
}
if val, ok := field.TagSettings["NOT NULL"]; ok && checkTruth(val) {
field.NotNull = true
}
if val, ok := field.TagSettings["UNIQUE"]; ok && checkTruth(val) {
field.Unique = true
}
if val, ok := field.TagSettings["COMMENT"]; ok {
field.Comment = val
}
if val, ok := field.TagSettings["TYPE"]; ok {
field.DBDataType = val
}
switch fieldValue.Elem().Kind() {
2020-01-31 07:22:37 +03:00
case reflect.Bool:
field.DataType = Bool
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
field.DataType = Int
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
field.DataType = Uint
case reflect.Float32, reflect.Float64:
field.DataType = Float
case reflect.String:
field.DataType = String
case reflect.Struct:
if _, ok := fieldValue.Interface().(*time.Time); ok {
2020-01-31 07:22:37 +03:00
field.DataType = Time
}
case reflect.Array, reflect.Slice:
if fieldValue.Type().Elem() == reflect.TypeOf(uint8(0)) {
field.DataType = Bytes
}
}
if field.Size == 0 {
switch fieldValue.Kind() {
case reflect.Int, reflect.Int64, reflect.Uint, reflect.Uint64, reflect.Float64:
field.Size = 64
case reflect.Int8, reflect.Uint8:
field.Size = 8
case reflect.Int16, reflect.Uint16:
field.Size = 16
case reflect.Int32, reflect.Uint32, reflect.Float32:
field.Size = 32
}
}
if _, ok := field.TagSettings["EMBEDDED"]; ok || fieldStruct.Anonymous {
2020-02-01 16:48:06 +03:00
var err error
field.Creatable = false
field.Updatable = false
if field.EmbeddedSchema, err = Parse(fieldValue.Interface(), &sync.Map{}, schema.namer); err != nil {
schema.err = err
}
for _, ef := range field.EmbeddedSchema.Fields {
ef.Schema = schema
2020-01-31 07:22:37 +03:00
ef.BindNames = append([]string{fieldStruct.Name}, ef.BindNames...)
2020-02-01 13:02:19 +03:00
if prefix, ok := field.TagSettings["EMBEDDEDPREFIX"]; ok {
2020-01-31 07:22:37 +03:00
ef.DBName = prefix + ef.DBName
}
for k, v := range field.TagSettings {
ef.TagSettings[k] = v
}
}
}
return field
}