2020-01-31 07:22:37 +03:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
2022-02-16 10:30:43 +03:00
|
|
|
"context"
|
2020-02-15 11:04:21 +03:00
|
|
|
"database/sql"
|
2020-01-31 07:22:37 +03:00
|
|
|
"database/sql/driver"
|
2020-02-15 11:04:21 +03:00
|
|
|
"fmt"
|
2020-01-31 07:22:37 +03:00
|
|
|
"reflect"
|
|
|
|
"strconv"
|
2020-02-18 17:56:37 +03:00
|
|
|
"strings"
|
2020-01-31 07:22:37 +03:00
|
|
|
"sync"
|
|
|
|
"time"
|
2020-02-15 11:04:21 +03:00
|
|
|
|
2020-06-02 04:25:55 +03:00
|
|
|
"github.com/jinzhu/now"
|
2022-03-23 12:24:25 +03:00
|
|
|
"gorm.io/gorm/clause"
|
2020-06-02 04:16:07 +03:00
|
|
|
"gorm.io/gorm/utils"
|
2020-01-31 07:22:37 +03:00
|
|
|
)
|
|
|
|
|
2022-02-16 10:30:43 +03:00
|
|
|
// special types' reflect type
|
|
|
|
var (
|
|
|
|
TimeReflectType = reflect.TypeOf(time.Time{})
|
|
|
|
TimePtrReflectType = reflect.TypeOf(&time.Time{})
|
|
|
|
ByteReflectType = reflect.TypeOf(uint8(0))
|
|
|
|
)
|
2020-02-18 17:56:37 +03:00
|
|
|
|
2022-02-16 10:30:43 +03:00
|
|
|
type (
|
|
|
|
// DataType GORM data type
|
|
|
|
DataType string
|
|
|
|
// TimeType GORM time type
|
|
|
|
TimeType int64
|
|
|
|
)
|
2020-09-24 15:08:24 +03:00
|
|
|
|
2022-02-16 10:30:43 +03:00
|
|
|
// GORM time types
|
2020-02-18 17:56:37 +03:00
|
|
|
const (
|
2021-09-17 10:29:49 +03:00
|
|
|
UnixTime TimeType = 1
|
|
|
|
UnixSecond TimeType = 2
|
|
|
|
UnixMillisecond TimeType = 3
|
|
|
|
UnixNanosecond TimeType = 4
|
2020-02-18 17:56:37 +03:00
|
|
|
)
|
|
|
|
|
2022-02-16 10:30:43 +03:00
|
|
|
// GORM fields types
|
2020-01-31 07:22:37 +03:00
|
|
|
const (
|
2020-02-01 07:46:52 +03:00
|
|
|
Bool DataType = "bool"
|
2020-07-16 06:27:04 +03:00
|
|
|
Int DataType = "int"
|
|
|
|
Uint DataType = "uint"
|
|
|
|
Float DataType = "float"
|
|
|
|
String DataType = "string"
|
|
|
|
Time DataType = "time"
|
|
|
|
Bytes DataType = "bytes"
|
2020-01-31 07:22:37 +03:00
|
|
|
)
|
|
|
|
|
2022-02-16 10:30:43 +03:00
|
|
|
// Field is the representation of model schema's field
|
2020-01-31 07:22:37 +03:00
|
|
|
type Field struct {
|
2020-12-30 06:16:40 +03:00
|
|
|
Name string
|
|
|
|
DBName string
|
|
|
|
BindNames []string
|
|
|
|
DataType DataType
|
|
|
|
GORMDataType DataType
|
|
|
|
PrimaryKey bool
|
|
|
|
AutoIncrement bool
|
|
|
|
AutoIncrementIncrement int64
|
|
|
|
Creatable bool
|
|
|
|
Updatable bool
|
|
|
|
Readable bool
|
|
|
|
AutoCreateTime TimeType
|
|
|
|
AutoUpdateTime TimeType
|
2022-02-16 10:30:43 +03:00
|
|
|
HasDefaultValue bool
|
2020-12-30 06:16:40 +03:00
|
|
|
DefaultValue string
|
|
|
|
DefaultValueInterface interface{}
|
|
|
|
NotNull bool
|
|
|
|
Unique bool
|
|
|
|
Comment string
|
|
|
|
Size int
|
|
|
|
Precision int
|
|
|
|
Scale int
|
2022-02-16 10:30:43 +03:00
|
|
|
IgnoreMigration bool
|
2020-12-30 06:16:40 +03:00
|
|
|
FieldType reflect.Type
|
|
|
|
IndirectFieldType reflect.Type
|
|
|
|
StructField reflect.StructField
|
|
|
|
Tag reflect.StructTag
|
|
|
|
TagSettings map[string]string
|
|
|
|
Schema *Schema
|
|
|
|
EmbeddedSchema *Schema
|
|
|
|
OwnerSchema *Schema
|
2022-02-16 10:30:43 +03:00
|
|
|
ReflectValueOf func(context.Context, reflect.Value) reflect.Value
|
|
|
|
ValueOf func(context.Context, reflect.Value) (value interface{}, zero bool)
|
|
|
|
Set func(context.Context, reflect.Value, interface{}) error
|
|
|
|
Serializer SerializerInterface
|
|
|
|
NewValuePool FieldNewValuePool
|
2020-01-31 07:22:37 +03:00
|
|
|
}
|
|
|
|
|
2022-02-16 10:30:43 +03:00
|
|
|
// ParseField parses reflect.StructField to Field
|
2020-01-31 07:22:37 +03:00
|
|
|
func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
|
2022-02-16 10:30:43 +03:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
tagSetting = ParseTagSetting(fieldStruct.Tag.Get("gorm"), ";")
|
|
|
|
)
|
2020-09-03 13:20:57 +03:00
|
|
|
|
2020-01-31 07:22:37 +03:00
|
|
|
field := &Field{
|
2020-12-30 06:16:40 +03:00
|
|
|
Name: fieldStruct.Name,
|
2022-02-16 10:30:43 +03:00
|
|
|
DBName: tagSetting["COLUMN"],
|
2020-12-30 06:16:40 +03:00
|
|
|
BindNames: []string{fieldStruct.Name},
|
|
|
|
FieldType: fieldStruct.Type,
|
|
|
|
IndirectFieldType: fieldStruct.Type,
|
|
|
|
StructField: fieldStruct,
|
2022-02-16 10:30:43 +03:00
|
|
|
Tag: fieldStruct.Tag,
|
|
|
|
TagSettings: tagSetting,
|
|
|
|
Schema: schema,
|
2020-12-30 06:16:40 +03:00
|
|
|
Creatable: true,
|
|
|
|
Updatable: true,
|
|
|
|
Readable: true,
|
2022-02-16 10:30:43 +03:00
|
|
|
PrimaryKey: utils.CheckTruth(tagSetting["PRIMARYKEY"], tagSetting["PRIMARY_KEY"]),
|
|
|
|
AutoIncrement: utils.CheckTruth(tagSetting["AUTOINCREMENT"]),
|
|
|
|
HasDefaultValue: utils.CheckTruth(tagSetting["AUTOINCREMENT"]),
|
|
|
|
NotNull: utils.CheckTruth(tagSetting["NOT NULL"], tagSetting["NOTNULL"]),
|
|
|
|
Unique: utils.CheckTruth(tagSetting["UNIQUE"]),
|
|
|
|
Comment: tagSetting["COMMENT"],
|
2020-12-30 06:16:40 +03:00
|
|
|
AutoIncrementIncrement: 1,
|
2020-01-31 07:22:37 +03:00
|
|
|
}
|
|
|
|
|
2020-02-15 14:45:27 +03:00
|
|
|
for field.IndirectFieldType.Kind() == reflect.Ptr {
|
|
|
|
field.IndirectFieldType = field.IndirectFieldType.Elem()
|
2020-01-31 07:22:37 +03:00
|
|
|
}
|
|
|
|
|
2020-02-15 14:45:27 +03:00
|
|
|
fieldValue := reflect.New(field.IndirectFieldType)
|
2022-02-16 10:30:43 +03:00
|
|
|
// if field is valuer, used its value or first field as data type
|
2020-06-18 04:15:23 +03:00
|
|
|
valuer, isValuer := fieldValue.Interface().(driver.Valuer)
|
|
|
|
if isValuer {
|
2020-07-17 07:02:00 +03:00
|
|
|
if _, ok := fieldValue.Interface().(GormDataTypeInterface); !ok {
|
2020-08-19 10:47:08 +03:00
|
|
|
if v, err := valuer.Value(); reflect.ValueOf(v).IsValid() && err == nil {
|
2020-07-17 07:02:00 +03:00
|
|
|
fieldValue = reflect.ValueOf(v)
|
|
|
|
}
|
2020-01-31 07:22:37 +03:00
|
|
|
|
2022-02-16 10:30:43 +03:00
|
|
|
// Use the field struct's first field type as data type, e.g: use `string` for sql.NullString
|
2020-08-19 10:47:08 +03:00
|
|
|
var getRealFieldValue func(reflect.Value)
|
|
|
|
getRealFieldValue = func(v reflect.Value) {
|
2022-02-16 10:30:43 +03:00
|
|
|
var (
|
|
|
|
rv = reflect.Indirect(v)
|
|
|
|
rvType = rv.Type()
|
|
|
|
)
|
|
|
|
|
|
|
|
if rv.Kind() == reflect.Struct && !rvType.ConvertibleTo(TimeReflectType) {
|
|
|
|
for i := 0; i < rvType.NumField(); i++ {
|
|
|
|
for key, value := range ParseTagSetting(rvType.Field(i).Tag.Get("gorm"), ";") {
|
|
|
|
if _, ok := field.TagSettings[key]; !ok {
|
|
|
|
field.TagSettings[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < rvType.NumField(); i++ {
|
|
|
|
newFieldType := rvType.Field(i).Type
|
2020-07-17 07:02:00 +03:00
|
|
|
for newFieldType.Kind() == reflect.Ptr {
|
|
|
|
newFieldType = newFieldType.Elem()
|
|
|
|
}
|
2020-01-31 07:22:37 +03:00
|
|
|
|
2020-07-17 07:02:00 +03:00
|
|
|
fieldValue = reflect.New(newFieldType)
|
2022-02-16 10:30:43 +03:00
|
|
|
if rvType != reflect.Indirect(fieldValue).Type() {
|
2020-08-19 10:47:08 +03:00
|
|
|
getRealFieldValue(fieldValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fieldValue.IsValid() {
|
|
|
|
return
|
|
|
|
}
|
2020-01-31 07:22:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-19 10:47:08 +03:00
|
|
|
|
|
|
|
getRealFieldValue(fieldValue)
|
2020-01-31 07:22:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-16 10:30:43 +03:00
|
|
|
if v, isSerializer := fieldValue.Interface().(SerializerInterface); isSerializer {
|
|
|
|
field.DataType = String
|
|
|
|
field.Serializer = v
|
|
|
|
} else {
|
|
|
|
var serializerName = field.TagSettings["JSON"]
|
|
|
|
if serializerName == "" {
|
|
|
|
serializerName = field.TagSettings["SERIALIZER"]
|
|
|
|
}
|
|
|
|
if serializerName != "" {
|
|
|
|
if serializer, ok := GetSerializer(serializerName); ok {
|
|
|
|
// Set default data type to string for serializer
|
|
|
|
field.DataType = String
|
|
|
|
field.Serializer = serializer
|
|
|
|
} else {
|
|
|
|
schema.err = fmt.Errorf("invalid serializer type %v", serializerName)
|
|
|
|
}
|
|
|
|
}
|
2020-01-31 07:22:37 +03:00
|
|
|
}
|
|
|
|
|
2020-12-30 06:16:40 +03:00
|
|
|
if num, ok := field.TagSettings["AUTOINCREMENTINCREMENT"]; ok {
|
|
|
|
field.AutoIncrementIncrement, _ = strconv.ParseInt(num, 10, 64)
|
|
|
|
}
|
|
|
|
|
2020-01-31 07:22:37 +03:00
|
|
|
if v, ok := field.TagSettings["DEFAULT"]; ok {
|
|
|
|
field.HasDefaultValue = true
|
|
|
|
field.DefaultValue = v
|
|
|
|
}
|
|
|
|
|
|
|
|
if num, ok := field.TagSettings["SIZE"]; ok {
|
2020-02-22 12:53:57 +03:00
|
|
|
if field.Size, err = strconv.Atoi(num); err != nil {
|
|
|
|
field.Size = -1
|
|
|
|
}
|
2020-01-31 07:22:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if p, ok := field.TagSettings["PRECISION"]; ok {
|
|
|
|
field.Precision, _ = strconv.Atoi(p)
|
|
|
|
}
|
|
|
|
|
2020-08-23 10:40:19 +03:00
|
|
|
if s, ok := field.TagSettings["SCALE"]; ok {
|
|
|
|
field.Scale, _ = strconv.Atoi(s)
|
|
|
|
}
|
|
|
|
|
2020-09-10 06:59:18 +03:00
|
|
|
// default value is function or null or blank (primary keys)
|
2021-02-09 11:03:02 +03:00
|
|
|
field.DefaultValue = strings.TrimSpace(field.DefaultValue)
|
2020-09-10 06:59:18 +03:00
|
|
|
skipParseDefaultValue := strings.Contains(field.DefaultValue, "(") &&
|
|
|
|
strings.Contains(field.DefaultValue, ")") || strings.ToLower(field.DefaultValue) == "null" || field.DefaultValue == ""
|
2020-05-30 16:05:27 +03:00
|
|
|
switch reflect.Indirect(fieldValue).Kind() {
|
2020-01-31 07:22:37 +03:00
|
|
|
case reflect.Bool:
|
|
|
|
field.DataType = Bool
|
2020-09-10 06:59:18 +03:00
|
|
|
if field.HasDefaultValue && !skipParseDefaultValue {
|
2020-09-03 13:20:57 +03:00
|
|
|
if field.DefaultValueInterface, err = strconv.ParseBool(field.DefaultValue); err != nil {
|
2021-06-10 05:21:28 +03:00
|
|
|
schema.err = fmt.Errorf("failed to parse %s as default value for bool, got error: %v", field.DefaultValue, err)
|
2020-09-03 13:20:57 +03:00
|
|
|
}
|
2020-02-18 17:56:37 +03:00
|
|
|
}
|
2020-01-31 07:22:37 +03:00
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
field.DataType = Int
|
2020-09-10 06:59:18 +03:00
|
|
|
if field.HasDefaultValue && !skipParseDefaultValue {
|
2020-09-03 13:20:57 +03:00
|
|
|
if field.DefaultValueInterface, err = strconv.ParseInt(field.DefaultValue, 0, 64); err != nil {
|
2021-06-10 05:21:28 +03:00
|
|
|
schema.err = fmt.Errorf("failed to parse %s as default value for int, got error: %v", field.DefaultValue, err)
|
2020-09-03 13:20:57 +03:00
|
|
|
}
|
2020-02-18 17:56:37 +03:00
|
|
|
}
|
2020-01-31 07:22:37 +03:00
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
|
|
field.DataType = Uint
|
2020-09-10 06:59:18 +03:00
|
|
|
if field.HasDefaultValue && !skipParseDefaultValue {
|
2020-09-03 13:20:57 +03:00
|
|
|
if field.DefaultValueInterface, err = strconv.ParseUint(field.DefaultValue, 0, 64); err != nil {
|
2021-06-10 05:21:28 +03:00
|
|
|
schema.err = fmt.Errorf("failed to parse %s as default value for uint, got error: %v", field.DefaultValue, err)
|
2020-09-03 13:20:57 +03:00
|
|
|
}
|
2020-02-18 17:56:37 +03:00
|
|
|
}
|
2020-01-31 07:22:37 +03:00
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
field.DataType = Float
|
2020-09-10 06:59:18 +03:00
|
|
|
if field.HasDefaultValue && !skipParseDefaultValue {
|
2020-09-03 13:20:57 +03:00
|
|
|
if field.DefaultValueInterface, err = strconv.ParseFloat(field.DefaultValue, 64); err != nil {
|
2021-06-10 05:21:28 +03:00
|
|
|
schema.err = fmt.Errorf("failed to parse %s as default value for float, got error: %v", field.DefaultValue, err)
|
2020-09-03 13:20:57 +03:00
|
|
|
}
|
2020-02-18 17:56:37 +03:00
|
|
|
}
|
2020-01-31 07:22:37 +03:00
|
|
|
case reflect.String:
|
|
|
|
field.DataType = String
|
2020-09-10 06:59:18 +03:00
|
|
|
if field.HasDefaultValue && !skipParseDefaultValue {
|
2020-05-30 11:03:27 +03:00
|
|
|
field.DefaultValue = strings.Trim(field.DefaultValue, "'")
|
2021-06-10 05:21:28 +03:00
|
|
|
field.DefaultValue = strings.Trim(field.DefaultValue, `"`)
|
2020-02-18 17:56:37 +03:00
|
|
|
field.DefaultValueInterface = field.DefaultValue
|
|
|
|
}
|
2020-01-31 07:22:37 +03:00
|
|
|
case reflect.Struct:
|
2020-02-01 15:18:25 +03:00
|
|
|
if _, ok := fieldValue.Interface().(*time.Time); ok {
|
2020-01-31 07:22:37 +03:00
|
|
|
field.DataType = Time
|
2020-09-24 15:08:24 +03:00
|
|
|
} else if fieldValue.Type().ConvertibleTo(TimeReflectType) {
|
2020-07-30 12:36:39 +03:00
|
|
|
field.DataType = Time
|
2022-02-16 10:30:43 +03:00
|
|
|
} else if fieldValue.Type().ConvertibleTo(TimePtrReflectType) {
|
2020-02-15 19:37:59 +03:00
|
|
|
field.DataType = Time
|
2020-01-31 07:22:37 +03:00
|
|
|
}
|
2022-03-17 06:34:27 +03:00
|
|
|
if field.HasDefaultValue && !skipParseDefaultValue && field.DataType == Time {
|
2022-03-20 03:55:08 +03:00
|
|
|
if t, err := now.Parse(field.DefaultValue); err == nil {
|
|
|
|
field.DefaultValueInterface = t
|
2022-03-17 06:34:27 +03:00
|
|
|
}
|
|
|
|
}
|
2020-01-31 07:22:37 +03:00
|
|
|
case reflect.Array, reflect.Slice:
|
2022-02-16 10:30:43 +03:00
|
|
|
if reflect.Indirect(fieldValue).Type().Elem() == ByteReflectType && field.DataType == "" {
|
2020-01-31 07:22:37 +03:00
|
|
|
field.DataType = Bytes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-06 05:47:32 +03:00
|
|
|
if dataTyper, ok := fieldValue.Interface().(GormDataTypeInterface); ok {
|
|
|
|
field.DataType = DataType(dataTyper.GormDataType())
|
|
|
|
}
|
|
|
|
|
2020-06-03 03:44:13 +03:00
|
|
|
if v, ok := field.TagSettings["AUTOCREATETIME"]; ok || (field.Name == "CreatedAt" && (field.DataType == Time || field.DataType == Int || field.DataType == Uint)) {
|
2021-09-17 10:29:49 +03:00
|
|
|
if field.DataType == Time {
|
|
|
|
field.AutoCreateTime = UnixTime
|
|
|
|
} else if strings.ToUpper(v) == "NANO" {
|
2020-03-08 08:24:08 +03:00
|
|
|
field.AutoCreateTime = UnixNanosecond
|
2020-07-30 12:39:57 +03:00
|
|
|
} else if strings.ToUpper(v) == "MILLI" {
|
|
|
|
field.AutoCreateTime = UnixMillisecond
|
2020-03-08 08:24:08 +03:00
|
|
|
} else {
|
|
|
|
field.AutoCreateTime = UnixSecond
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 03:44:13 +03:00
|
|
|
if v, ok := field.TagSettings["AUTOUPDATETIME"]; ok || (field.Name == "UpdatedAt" && (field.DataType == Time || field.DataType == Int || field.DataType == Uint)) {
|
2021-09-17 10:29:49 +03:00
|
|
|
if field.DataType == Time {
|
|
|
|
field.AutoUpdateTime = UnixTime
|
|
|
|
} else if strings.ToUpper(v) == "NANO" {
|
2020-03-08 08:24:08 +03:00
|
|
|
field.AutoUpdateTime = UnixNanosecond
|
2020-07-30 12:39:57 +03:00
|
|
|
} else if strings.ToUpper(v) == "MILLI" {
|
|
|
|
field.AutoUpdateTime = UnixMillisecond
|
2020-03-08 08:24:08 +03:00
|
|
|
} else {
|
|
|
|
field.AutoUpdateTime = UnixSecond
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-23 12:48:13 +03:00
|
|
|
if field.GORMDataType == "" {
|
|
|
|
field.GORMDataType = field.DataType
|
|
|
|
}
|
|
|
|
|
2020-06-26 02:26:45 +03:00
|
|
|
if val, ok := field.TagSettings["TYPE"]; ok {
|
|
|
|
switch DataType(strings.ToLower(val)) {
|
|
|
|
case Bool, Int, Uint, Float, String, Time, Bytes:
|
|
|
|
field.DataType = DataType(strings.ToLower(val))
|
|
|
|
default:
|
|
|
|
field.DataType = DataType(val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-31 07:22:37 +03:00
|
|
|
if field.Size == 0 {
|
2020-03-12 03:39:42 +03:00
|
|
|
switch reflect.Indirect(fieldValue).Kind() {
|
2020-01-31 07:22:37 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-05 14:18:22 +03:00
|
|
|
// setup permission
|
2021-02-09 11:03:02 +03:00
|
|
|
if val, ok := field.TagSettings["-"]; ok {
|
|
|
|
val = strings.ToLower(strings.TrimSpace(val))
|
|
|
|
switch val {
|
|
|
|
case "-":
|
|
|
|
field.Creatable = false
|
|
|
|
field.Updatable = false
|
|
|
|
field.Readable = false
|
|
|
|
field.DataType = ""
|
|
|
|
case "all":
|
|
|
|
field.Creatable = false
|
|
|
|
field.Updatable = false
|
|
|
|
field.Readable = false
|
|
|
|
field.DataType = ""
|
|
|
|
field.IgnoreMigration = true
|
|
|
|
case "migration":
|
|
|
|
field.IgnoreMigration = true
|
|
|
|
}
|
2020-06-05 14:18:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := field.TagSettings["->"]; ok {
|
|
|
|
field.Creatable = false
|
|
|
|
field.Updatable = false
|
|
|
|
if strings.ToLower(v) == "false" {
|
|
|
|
field.Readable = false
|
|
|
|
} else {
|
|
|
|
field.Readable = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := field.TagSettings["<-"]; ok {
|
|
|
|
field.Creatable = true
|
|
|
|
field.Updatable = true
|
|
|
|
|
|
|
|
if v != "<-" {
|
|
|
|
if !strings.Contains(v, "create") {
|
|
|
|
field.Creatable = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(v, "update") {
|
|
|
|
field.Updatable = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-16 10:30:43 +03:00
|
|
|
// Normal anonymous field or having `EMBEDDED` tag
|
|
|
|
if _, ok := field.TagSettings["EMBEDDED"]; ok || (field.GORMDataType != Time && field.GORMDataType != Bytes && !isValuer &&
|
|
|
|
fieldStruct.Anonymous && (field.Creatable || field.Updatable || field.Readable)) {
|
fix type alias AutoMigrate bug(Add Test Case) (#4888)
* fix type alias AutoMigrate bug. eg
```go
package main
type IDer interface{ GetID() int64 }
// ID will add some method to implement some interface eg: GetID
type ID int64
func (z ID) GetID() int64 { return int64(z) }
type Test struct {
ID
Code string `gorm:"size:50"`
Name string `gorm:"size:50"`
}
func main() {
db, err := gorm.Open(postgres.New(postgres.Config{
DSN: `dsn`,
PreferSimpleProtocol: false,
}), &gorm.Config{
Logger: logger.Default.LogMode(logger.Info),
SkipDefaultTransaction: true,
})
if err != nil {
log.Fatal(err)
}
if err = db.AutoMigrate(&Test{}); err != nil {
// invalid embedded struct for Test's field ID, should be struct, but got main.ID
log.Fatal(err)
}
}
```
* fix type alias AutoMigrate bug. eg
```go
package main
type IDer interface{ GetID() int64 }
// ID will add some method to implement some interface eg: GetID
type ID int64
func (z ID) GetID() int64 { return int64(z) }
type Test struct {
ID
Code string `gorm:"size:50"`
Name string `gorm:"size:50"`
}
func main() {
db, err := gorm.Open(postgres.New(postgres.Config{
DSN: `dsn`,
PreferSimpleProtocol: false,
}), &gorm.Config{
Logger: logger.Default.LogMode(logger.Info),
SkipDefaultTransaction: true,
})
if err != nil {
log.Fatal(err)
}
if err = db.AutoMigrate(&Test{}); err != nil {
// invalid embedded struct for Test's field ID, should be struct, but got main.ID
log.Fatal(err)
}
}
```
* Add typealis test.
* try to fix golangci-lint
2021-12-10 12:45:36 +03:00
|
|
|
kind := reflect.Indirect(fieldValue).Kind()
|
|
|
|
switch kind {
|
|
|
|
case reflect.Struct:
|
2020-07-29 05:23:14 +03:00
|
|
|
var err error
|
|
|
|
field.Creatable = false
|
|
|
|
field.Updatable = false
|
|
|
|
field.Readable = false
|
2020-08-17 07:02:41 +03:00
|
|
|
|
2020-08-18 06:21:40 +03:00
|
|
|
cacheStore := &sync.Map{}
|
|
|
|
cacheStore.Store(embeddedCacheKey, true)
|
2020-11-27 09:32:20 +03:00
|
|
|
if field.EmbeddedSchema, err = getOrParse(fieldValue.Interface(), cacheStore, embeddedNamer{Table: schema.Table, Namer: schema.namer}); err != nil {
|
2020-07-29 05:23:14 +03:00
|
|
|
schema.err = err
|
2020-02-15 11:04:21 +03:00
|
|
|
}
|
2020-08-18 06:21:40 +03:00
|
|
|
|
2020-07-29 05:23:14 +03:00
|
|
|
for _, ef := range field.EmbeddedSchema.Fields {
|
|
|
|
ef.Schema = schema
|
2020-08-04 07:10:19 +03:00
|
|
|
ef.OwnerSchema = field.EmbeddedSchema
|
2020-07-29 05:23:14 +03:00
|
|
|
ef.BindNames = append([]string{fieldStruct.Name}, ef.BindNames...)
|
|
|
|
// index is negative means is pointer
|
|
|
|
if field.FieldType.Kind() == reflect.Struct {
|
|
|
|
ef.StructField.Index = append([]int{fieldStruct.Index[0]}, ef.StructField.Index...)
|
|
|
|
} else {
|
|
|
|
ef.StructField.Index = append([]int{-fieldStruct.Index[0] - 1}, ef.StructField.Index...)
|
|
|
|
}
|
2020-01-31 07:22:37 +03:00
|
|
|
|
2020-09-04 09:30:53 +03:00
|
|
|
if prefix, ok := field.TagSettings["EMBEDDEDPREFIX"]; ok && ef.DBName != "" {
|
2020-07-29 05:23:14 +03:00
|
|
|
ef.DBName = prefix + ef.DBName
|
|
|
|
}
|
2020-01-31 07:22:37 +03:00
|
|
|
|
2020-09-11 05:49:31 +03:00
|
|
|
if ef.PrimaryKey {
|
|
|
|
if val, ok := ef.TagSettings["PRIMARYKEY"]; ok && utils.CheckTruth(val) {
|
|
|
|
ef.PrimaryKey = true
|
|
|
|
} else if val, ok := ef.TagSettings["PRIMARY_KEY"]; ok && utils.CheckTruth(val) {
|
|
|
|
ef.PrimaryKey = true
|
|
|
|
} else {
|
|
|
|
ef.PrimaryKey = false
|
|
|
|
|
|
|
|
if val, ok := ef.TagSettings["AUTOINCREMENT"]; !ok || !utils.CheckTruth(val) {
|
|
|
|
ef.AutoIncrement = false
|
|
|
|
}
|
2020-08-19 14:02:32 +03:00
|
|
|
|
2020-09-11 05:49:31 +03:00
|
|
|
if ef.DefaultValue == "" {
|
|
|
|
ef.HasDefaultValue = false
|
|
|
|
}
|
2020-08-19 14:02:32 +03:00
|
|
|
}
|
2020-07-29 05:23:14 +03:00
|
|
|
}
|
2020-05-31 09:18:07 +03:00
|
|
|
|
2020-07-29 05:23:14 +03:00
|
|
|
for k, v := range field.TagSettings {
|
|
|
|
ef.TagSettings[k] = v
|
|
|
|
}
|
2020-01-31 07:22:37 +03:00
|
|
|
}
|
2022-01-06 10:02:53 +03:00
|
|
|
case reflect.Invalid, reflect.Uintptr, reflect.Array, reflect.Chan, reflect.Func, reflect.Interface,
|
|
|
|
reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer, reflect.Complex64, reflect.Complex128:
|
2021-06-10 05:21:28 +03:00
|
|
|
schema.err = fmt.Errorf("invalid embedded struct for %s's field %s, should be struct, but got %v", field.Schema.Name, field.Name, field.FieldType)
|
2020-07-29 05:23:14 +03:00
|
|
|
}
|
2020-01-31 07:22:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return field
|
|
|
|
}
|
2020-02-15 11:04:21 +03:00
|
|
|
|
|
|
|
// create valuer, setter when parse struct
|
|
|
|
func (field *Field) setupValuerAndSetter() {
|
2022-02-16 10:30:43 +03:00
|
|
|
// Setup NewValuePool
|
2022-03-18 12:12:17 +03:00
|
|
|
field.setupNewValuePool()
|
2020-02-15 14:45:27 +03:00
|
|
|
|
2022-02-16 10:30:43 +03:00
|
|
|
// ValueOf returns field's value and if it is zero
|
2022-02-27 17:10:17 +03:00
|
|
|
fieldIndex := field.StructField.Index[0]
|
|
|
|
switch {
|
|
|
|
case len(field.StructField.Index) == 1 && fieldIndex > 0:
|
|
|
|
field.ValueOf = func(ctx context.Context, value reflect.Value) (interface{}, bool) {
|
|
|
|
fieldValue := reflect.Indirect(value).Field(fieldIndex)
|
|
|
|
return fieldValue.Interface(), fieldValue.IsZero()
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
field.ValueOf = func(ctx context.Context, v reflect.Value) (interface{}, bool) {
|
|
|
|
v = reflect.Indirect(v)
|
|
|
|
for _, fieldIdx := range field.StructField.Index {
|
|
|
|
if fieldIdx >= 0 {
|
|
|
|
v = v.Field(fieldIdx)
|
2022-02-16 10:30:43 +03:00
|
|
|
} else {
|
2022-02-27 17:10:17 +03:00
|
|
|
v = v.Field(-fieldIdx - 1)
|
|
|
|
|
|
|
|
if !v.IsNil() {
|
|
|
|
v = v.Elem()
|
|
|
|
} else {
|
|
|
|
return nil, true
|
|
|
|
}
|
2020-02-15 11:04:21 +03:00
|
|
|
}
|
|
|
|
}
|
2022-02-16 10:30:43 +03:00
|
|
|
|
2022-02-27 17:10:17 +03:00
|
|
|
fv, zero := v.Interface(), v.IsZero()
|
|
|
|
return fv, zero
|
|
|
|
}
|
2020-02-15 11:04:21 +03:00
|
|
|
}
|
|
|
|
|
2022-02-16 10:30:43 +03:00
|
|
|
if field.Serializer != nil {
|
|
|
|
oldValuerOf := field.ValueOf
|
|
|
|
field.ValueOf = func(ctx context.Context, v reflect.Value) (interface{}, bool) {
|
|
|
|
value, zero := oldValuerOf(ctx, v)
|
|
|
|
if zero {
|
|
|
|
return value, zero
|
|
|
|
}
|
|
|
|
|
|
|
|
s, ok := value.(SerializerValuerInterface)
|
|
|
|
if !ok {
|
|
|
|
s = field.Serializer
|
|
|
|
}
|
|
|
|
|
2022-03-18 12:12:17 +03:00
|
|
|
return &serializer{
|
2022-02-16 10:30:43 +03:00
|
|
|
Field: field,
|
|
|
|
SerializeValuer: s,
|
|
|
|
Destination: v,
|
|
|
|
Context: ctx,
|
|
|
|
fieldValue: value,
|
|
|
|
}, false
|
2020-02-15 11:04:21 +03:00
|
|
|
}
|
2022-02-16 10:30:43 +03:00
|
|
|
}
|
2020-02-15 14:45:27 +03:00
|
|
|
|
2022-02-16 10:30:43 +03:00
|
|
|
// ReflectValueOf returns field's reflect value
|
2022-02-27 17:10:17 +03:00
|
|
|
switch {
|
|
|
|
case len(field.StructField.Index) == 1 && fieldIndex > 0:
|
|
|
|
field.ReflectValueOf = func(ctx context.Context, value reflect.Value) reflect.Value {
|
|
|
|
return reflect.Indirect(value).Field(fieldIndex)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
field.ReflectValueOf = func(ctx context.Context, v reflect.Value) reflect.Value {
|
|
|
|
v = reflect.Indirect(v)
|
|
|
|
for idx, fieldIdx := range field.StructField.Index {
|
|
|
|
if fieldIdx >= 0 {
|
|
|
|
v = v.Field(fieldIdx)
|
|
|
|
} else {
|
|
|
|
v = v.Field(-fieldIdx - 1)
|
2020-02-15 11:04:21 +03:00
|
|
|
|
2022-02-27 17:10:17 +03:00
|
|
|
if v.IsNil() {
|
|
|
|
v.Set(reflect.New(v.Type().Elem()))
|
|
|
|
}
|
2022-02-16 10:30:43 +03:00
|
|
|
|
2022-02-27 17:10:17 +03:00
|
|
|
if idx < len(field.StructField.Index)-1 {
|
|
|
|
v = v.Elem()
|
|
|
|
}
|
2020-02-15 11:04:21 +03:00
|
|
|
}
|
|
|
|
}
|
2022-02-27 17:10:17 +03:00
|
|
|
return v
|
2020-02-15 11:04:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-16 10:30:43 +03:00
|
|
|
fallbackSetter := func(ctx context.Context, value reflect.Value, v interface{}, setter func(context.Context, reflect.Value, interface{}) error) (err error) {
|
2020-05-24 12:24:23 +03:00
|
|
|
if v == nil {
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).Set(reflect.New(field.FieldType).Elem())
|
2020-02-15 19:37:59 +03:00
|
|
|
} else {
|
2020-05-24 12:24:23 +03:00
|
|
|
reflectV := reflect.ValueOf(v)
|
2021-04-13 04:41:30 +03:00
|
|
|
// Optimal value type acquisition for v
|
|
|
|
reflectValType := reflectV.Type()
|
2020-06-03 03:44:13 +03:00
|
|
|
|
2021-04-13 04:41:30 +03:00
|
|
|
if reflectValType.AssignableTo(field.FieldType) {
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).Set(reflectV)
|
2020-06-02 17:13:53 +03:00
|
|
|
return
|
2021-04-13 04:41:30 +03:00
|
|
|
} else if reflectValType.ConvertibleTo(field.FieldType) {
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).Set(reflectV.Convert(field.FieldType))
|
2020-06-02 17:13:53 +03:00
|
|
|
return
|
|
|
|
} else if field.FieldType.Kind() == reflect.Ptr {
|
2022-02-16 10:30:43 +03:00
|
|
|
fieldValue := field.ReflectValueOf(ctx, value)
|
2021-07-14 17:03:17 +03:00
|
|
|
fieldType := field.FieldType.Elem()
|
2020-06-02 17:13:53 +03:00
|
|
|
|
2021-07-14 17:03:17 +03:00
|
|
|
if reflectValType.AssignableTo(fieldType) {
|
2020-06-23 04:09:46 +03:00
|
|
|
if !fieldValue.IsValid() {
|
2021-07-14 17:03:17 +03:00
|
|
|
fieldValue = reflect.New(fieldType)
|
2020-06-23 04:09:46 +03:00
|
|
|
} else if fieldValue.IsNil() {
|
2021-07-14 17:03:17 +03:00
|
|
|
fieldValue.Set(reflect.New(fieldType))
|
2020-06-02 17:13:53 +03:00
|
|
|
}
|
|
|
|
fieldValue.Elem().Set(reflectV)
|
|
|
|
return
|
2021-07-14 17:03:17 +03:00
|
|
|
} else if reflectValType.ConvertibleTo(fieldType) {
|
2020-06-02 17:13:53 +03:00
|
|
|
if fieldValue.IsNil() {
|
2021-07-14 17:03:17 +03:00
|
|
|
fieldValue.Set(reflect.New(fieldType))
|
2020-05-29 18:38:03 +03:00
|
|
|
}
|
2020-06-02 17:13:53 +03:00
|
|
|
|
2021-07-14 17:03:17 +03:00
|
|
|
fieldValue.Elem().Set(reflectV.Convert(fieldType))
|
2020-06-02 17:13:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-20 13:13:29 +03:00
|
|
|
if reflectV.Kind() == reflect.Ptr {
|
2020-06-03 03:44:13 +03:00
|
|
|
if reflectV.IsNil() {
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).Set(reflect.New(field.FieldType).Elem())
|
|
|
|
} else if reflectV.Type().Elem().AssignableTo(field.FieldType) {
|
|
|
|
field.ReflectValueOf(ctx, value).Set(reflectV.Elem())
|
|
|
|
return
|
2020-06-03 03:44:13 +03:00
|
|
|
} else {
|
2022-02-16 10:30:43 +03:00
|
|
|
err = setter(ctx, value, reflectV.Elem().Interface())
|
2020-06-03 03:44:13 +03:00
|
|
|
}
|
2020-08-20 13:13:29 +03:00
|
|
|
} else if valuer, ok := v.(driver.Valuer); ok {
|
|
|
|
if v, err = valuer.Value(); err == nil {
|
2022-02-16 10:30:43 +03:00
|
|
|
err = setter(ctx, value, v)
|
2020-08-20 13:13:29 +03:00
|
|
|
}
|
2022-03-23 12:24:25 +03:00
|
|
|
} else if _, ok := v.(clause.Expr); !ok {
|
|
|
|
return fmt.Errorf("failed to set value %#v to field %s", v, field.Name)
|
2020-05-24 12:24:23 +03:00
|
|
|
}
|
2020-02-15 19:37:59 +03:00
|
|
|
}
|
2020-06-02 17:13:53 +03:00
|
|
|
|
|
|
|
return
|
2020-02-15 19:37:59 +03:00
|
|
|
}
|
|
|
|
|
2020-02-18 17:56:37 +03:00
|
|
|
// Set
|
2020-02-15 11:04:21 +03:00
|
|
|
switch field.FieldType.Kind() {
|
|
|
|
case reflect.Bool:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.Set = func(ctx context.Context, value reflect.Value, v interface{}) error {
|
2020-02-15 11:04:21 +03:00
|
|
|
switch data := v.(type) {
|
2022-02-16 10:30:43 +03:00
|
|
|
case **bool:
|
|
|
|
if data != nil && *data != nil {
|
|
|
|
field.ReflectValueOf(ctx, value).SetBool(**data)
|
2020-07-04 03:21:23 +03:00
|
|
|
}
|
2022-02-16 10:30:43 +03:00
|
|
|
case bool:
|
|
|
|
field.ReflectValueOf(ctx, value).SetBool(data)
|
2020-06-02 17:13:53 +03:00
|
|
|
case int64:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetBool(data > 0)
|
2020-06-02 17:13:53 +03:00
|
|
|
case string:
|
|
|
|
b, _ := strconv.ParseBool(data)
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetBool(b)
|
2020-02-15 11:04:21 +03:00
|
|
|
default:
|
2022-02-16 10:30:43 +03:00
|
|
|
return fallbackSetter(ctx, value, v, field.Set)
|
2020-02-15 11:04:21 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.Set = func(ctx context.Context, value reflect.Value, v interface{}) (err error) {
|
2020-02-15 11:04:21 +03:00
|
|
|
switch data := v.(type) {
|
2022-02-16 10:30:43 +03:00
|
|
|
case **int64:
|
|
|
|
if data != nil && *data != nil {
|
|
|
|
field.ReflectValueOf(ctx, value).SetInt(**data)
|
|
|
|
}
|
2020-02-15 11:04:21 +03:00
|
|
|
case int64:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetInt(data)
|
2020-02-15 11:04:21 +03:00
|
|
|
case int:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetInt(int64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case int8:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetInt(int64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case int16:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetInt(int64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case int32:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetInt(int64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case uint:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetInt(int64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case uint8:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetInt(int64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case uint16:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetInt(int64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case uint32:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetInt(int64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case uint64:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetInt(int64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case float32:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetInt(int64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case float64:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetInt(int64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case []byte:
|
2022-02-16 10:30:43 +03:00
|
|
|
return field.Set(ctx, value, string(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case string:
|
|
|
|
if i, err := strconv.ParseInt(data, 0, 64); err == nil {
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetInt(i)
|
2020-02-15 11:04:21 +03:00
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-18 17:56:37 +03:00
|
|
|
case time.Time:
|
2020-06-05 14:18:22 +03:00
|
|
|
if field.AutoCreateTime == UnixNanosecond || field.AutoUpdateTime == UnixNanosecond {
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetInt(data.UnixNano())
|
2020-07-30 12:39:57 +03:00
|
|
|
} else if field.AutoCreateTime == UnixMillisecond || field.AutoUpdateTime == UnixMillisecond {
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetInt(data.UnixNano() / 1e6)
|
2020-02-18 17:56:37 +03:00
|
|
|
} else {
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetInt(data.Unix())
|
2020-02-18 17:56:37 +03:00
|
|
|
}
|
|
|
|
case *time.Time:
|
|
|
|
if data != nil {
|
2020-06-05 14:18:22 +03:00
|
|
|
if field.AutoCreateTime == UnixNanosecond || field.AutoUpdateTime == UnixNanosecond {
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetInt(data.UnixNano())
|
2020-07-30 12:39:57 +03:00
|
|
|
} else if field.AutoCreateTime == UnixMillisecond || field.AutoUpdateTime == UnixMillisecond {
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetInt(data.UnixNano() / 1e6)
|
2020-02-18 17:56:37 +03:00
|
|
|
} else {
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetInt(data.Unix())
|
2020-02-18 17:56:37 +03:00
|
|
|
}
|
|
|
|
} else {
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetInt(0)
|
2020-02-18 17:56:37 +03:00
|
|
|
}
|
2020-02-15 11:04:21 +03:00
|
|
|
default:
|
2022-02-16 10:30:43 +03:00
|
|
|
return fallbackSetter(ctx, value, v, field.Set)
|
2020-02-15 11:04:21 +03:00
|
|
|
}
|
2020-02-15 19:37:59 +03:00
|
|
|
return err
|
2020-02-15 11:04:21 +03:00
|
|
|
}
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.Set = func(ctx context.Context, value reflect.Value, v interface{}) (err error) {
|
2020-02-15 11:04:21 +03:00
|
|
|
switch data := v.(type) {
|
2022-02-16 10:30:43 +03:00
|
|
|
case **uint64:
|
|
|
|
if data != nil && *data != nil {
|
|
|
|
field.ReflectValueOf(ctx, value).SetUint(**data)
|
|
|
|
}
|
2020-02-15 11:04:21 +03:00
|
|
|
case uint64:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetUint(data)
|
2020-02-15 11:04:21 +03:00
|
|
|
case uint:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetUint(uint64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case uint8:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetUint(uint64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case uint16:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetUint(uint64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case uint32:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetUint(uint64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case int64:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetUint(uint64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case int:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetUint(uint64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case int8:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetUint(uint64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case int16:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetUint(uint64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case int32:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetUint(uint64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case float32:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetUint(uint64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case float64:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetUint(uint64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case []byte:
|
2022-02-16 10:30:43 +03:00
|
|
|
return field.Set(ctx, value, string(data))
|
2020-09-11 12:33:31 +03:00
|
|
|
case time.Time:
|
|
|
|
if field.AutoCreateTime == UnixNanosecond || field.AutoUpdateTime == UnixNanosecond {
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetUint(uint64(data.UnixNano()))
|
2020-09-11 12:33:31 +03:00
|
|
|
} else if field.AutoCreateTime == UnixMillisecond || field.AutoUpdateTime == UnixMillisecond {
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetUint(uint64(data.UnixNano() / 1e6))
|
2020-09-11 12:33:31 +03:00
|
|
|
} else {
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetUint(uint64(data.Unix()))
|
2020-09-11 12:33:31 +03:00
|
|
|
}
|
2020-02-15 11:04:21 +03:00
|
|
|
case string:
|
|
|
|
if i, err := strconv.ParseUint(data, 0, 64); err == nil {
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetUint(i)
|
2020-02-15 11:04:21 +03:00
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
2022-02-16 10:30:43 +03:00
|
|
|
return fallbackSetter(ctx, value, v, field.Set)
|
2020-02-15 11:04:21 +03:00
|
|
|
}
|
2020-02-15 19:37:59 +03:00
|
|
|
return err
|
2020-02-15 11:04:21 +03:00
|
|
|
}
|
|
|
|
case reflect.Float32, reflect.Float64:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.Set = func(ctx context.Context, value reflect.Value, v interface{}) (err error) {
|
2020-02-15 11:04:21 +03:00
|
|
|
switch data := v.(type) {
|
2022-02-16 10:30:43 +03:00
|
|
|
case **float64:
|
|
|
|
if data != nil && *data != nil {
|
|
|
|
field.ReflectValueOf(ctx, value).SetFloat(**data)
|
|
|
|
}
|
2020-02-15 11:04:21 +03:00
|
|
|
case float64:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetFloat(data)
|
2020-02-15 11:04:21 +03:00
|
|
|
case float32:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetFloat(float64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case int64:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetFloat(float64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case int:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetFloat(float64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case int8:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetFloat(float64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case int16:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetFloat(float64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case int32:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetFloat(float64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case uint:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetFloat(float64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case uint8:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetFloat(float64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case uint16:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetFloat(float64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case uint32:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetFloat(float64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case uint64:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetFloat(float64(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case []byte:
|
2022-02-16 10:30:43 +03:00
|
|
|
return field.Set(ctx, value, string(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case string:
|
|
|
|
if i, err := strconv.ParseFloat(data, 64); err == nil {
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetFloat(i)
|
2020-02-15 11:04:21 +03:00
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
2022-02-16 10:30:43 +03:00
|
|
|
return fallbackSetter(ctx, value, v, field.Set)
|
2020-02-15 11:04:21 +03:00
|
|
|
}
|
2020-02-15 19:37:59 +03:00
|
|
|
return err
|
2020-02-15 11:04:21 +03:00
|
|
|
}
|
|
|
|
case reflect.String:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.Set = func(ctx context.Context, value reflect.Value, v interface{}) (err error) {
|
2020-02-15 11:04:21 +03:00
|
|
|
switch data := v.(type) {
|
2022-02-16 10:30:43 +03:00
|
|
|
case **string:
|
|
|
|
if data != nil && *data != nil {
|
|
|
|
field.ReflectValueOf(ctx, value).SetString(**data)
|
|
|
|
}
|
2020-02-15 11:04:21 +03:00
|
|
|
case string:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetString(data)
|
2020-02-15 11:04:21 +03:00
|
|
|
case []byte:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetString(string(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetString(utils.ToString(data))
|
2020-02-15 11:04:21 +03:00
|
|
|
case float64, float32:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).SetString(fmt.Sprintf("%."+strconv.Itoa(field.Precision)+"f", data))
|
2020-02-15 11:04:21 +03:00
|
|
|
default:
|
2022-02-16 10:30:43 +03:00
|
|
|
return fallbackSetter(ctx, value, v, field.Set)
|
2020-02-15 11:04:21 +03:00
|
|
|
}
|
2020-02-15 19:37:59 +03:00
|
|
|
return err
|
2020-02-15 11:04:21 +03:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
fieldValue := reflect.New(field.FieldType)
|
2020-02-15 14:45:27 +03:00
|
|
|
switch fieldValue.Elem().Interface().(type) {
|
2020-02-15 11:04:21 +03:00
|
|
|
case time.Time:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.Set = func(ctx context.Context, value reflect.Value, v interface{}) error {
|
2020-02-15 11:04:21 +03:00
|
|
|
switch data := v.(type) {
|
2022-02-16 10:30:43 +03:00
|
|
|
case **time.Time:
|
|
|
|
if data != nil && *data != nil {
|
|
|
|
field.Set(ctx, value, *data)
|
|
|
|
}
|
2020-02-15 11:04:21 +03:00
|
|
|
case time.Time:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).Set(reflect.ValueOf(v))
|
2020-02-15 11:04:21 +03:00
|
|
|
case *time.Time:
|
2020-07-04 02:45:07 +03:00
|
|
|
if data != nil {
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).Set(reflect.ValueOf(data).Elem())
|
2020-07-04 02:45:07 +03:00
|
|
|
} else {
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).Set(reflect.ValueOf(time.Time{}))
|
2020-07-04 02:45:07 +03:00
|
|
|
}
|
2020-02-15 11:04:21 +03:00
|
|
|
case string:
|
|
|
|
if t, err := now.Parse(data); err == nil {
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).Set(reflect.ValueOf(t))
|
2020-02-15 11:04:21 +03:00
|
|
|
} else {
|
2021-06-10 05:21:28 +03:00
|
|
|
return fmt.Errorf("failed to set string %v to time.Time field %s, failed to parse it as time, got error %v", v, field.Name, err)
|
2020-02-15 11:04:21 +03:00
|
|
|
}
|
|
|
|
default:
|
2022-02-16 10:30:43 +03:00
|
|
|
return fallbackSetter(ctx, value, v, field.Set)
|
2020-02-15 11:04:21 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case *time.Time:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.Set = func(ctx context.Context, value reflect.Value, v interface{}) error {
|
2020-02-15 11:04:21 +03:00
|
|
|
switch data := v.(type) {
|
2022-02-16 10:30:43 +03:00
|
|
|
case **time.Time:
|
|
|
|
if data != nil {
|
|
|
|
field.ReflectValueOf(ctx, value).Set(reflect.ValueOf(*data))
|
|
|
|
}
|
2020-02-15 11:04:21 +03:00
|
|
|
case time.Time:
|
2022-02-16 10:30:43 +03:00
|
|
|
fieldValue := field.ReflectValueOf(ctx, value)
|
2020-05-29 18:38:03 +03:00
|
|
|
if fieldValue.IsNil() {
|
|
|
|
fieldValue.Set(reflect.New(field.FieldType.Elem()))
|
|
|
|
}
|
|
|
|
fieldValue.Elem().Set(reflect.ValueOf(v))
|
2020-02-15 11:04:21 +03:00
|
|
|
case *time.Time:
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).Set(reflect.ValueOf(v))
|
2020-02-15 11:04:21 +03:00
|
|
|
case string:
|
|
|
|
if t, err := now.Parse(data); err == nil {
|
2022-02-16 10:30:43 +03:00
|
|
|
fieldValue := field.ReflectValueOf(ctx, value)
|
2020-05-29 18:38:03 +03:00
|
|
|
if fieldValue.IsNil() {
|
|
|
|
if v == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
fieldValue.Set(reflect.New(field.FieldType.Elem()))
|
|
|
|
}
|
|
|
|
fieldValue.Elem().Set(reflect.ValueOf(t))
|
2020-02-15 11:04:21 +03:00
|
|
|
} else {
|
2021-06-10 05:21:28 +03:00
|
|
|
return fmt.Errorf("failed to set string %v to time.Time field %s, failed to parse it as time, got error %v", v, field.Name, err)
|
2020-02-15 11:04:21 +03:00
|
|
|
}
|
|
|
|
default:
|
2022-02-16 10:30:43 +03:00
|
|
|
return fallbackSetter(ctx, value, v, field.Set)
|
2020-02-15 11:04:21 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
default:
|
2020-08-13 07:05:55 +03:00
|
|
|
if _, ok := fieldValue.Elem().Interface().(sql.Scanner); ok {
|
|
|
|
// pointer scanner
|
2022-02-16 10:30:43 +03:00
|
|
|
field.Set = func(ctx context.Context, value reflect.Value, v interface{}) (err error) {
|
2020-06-02 17:13:53 +03:00
|
|
|
reflectV := reflect.ValueOf(v)
|
2020-10-23 13:40:05 +03:00
|
|
|
if !reflectV.IsValid() {
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).Set(reflect.New(field.FieldType).Elem())
|
2020-10-23 13:40:05 +03:00
|
|
|
} else if reflectV.Type().AssignableTo(field.FieldType) {
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).Set(reflectV)
|
2020-06-03 03:44:13 +03:00
|
|
|
} else if reflectV.Kind() == reflect.Ptr {
|
2020-10-23 13:40:05 +03:00
|
|
|
if reflectV.IsNil() || !reflectV.IsValid() {
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).Set(reflect.New(field.FieldType).Elem())
|
2020-06-03 03:44:13 +03:00
|
|
|
} else {
|
2022-02-16 10:30:43 +03:00
|
|
|
return field.Set(ctx, value, reflectV.Elem().Interface())
|
2020-06-03 03:44:13 +03:00
|
|
|
}
|
2020-05-24 17:52:16 +03:00
|
|
|
} else {
|
2022-02-16 10:30:43 +03:00
|
|
|
fieldValue := field.ReflectValueOf(ctx, value)
|
2020-08-13 07:05:55 +03:00
|
|
|
if fieldValue.IsNil() {
|
|
|
|
fieldValue.Set(reflect.New(field.FieldType.Elem()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if valuer, ok := v.(driver.Valuer); ok {
|
|
|
|
v, _ = valuer.Value()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = fieldValue.Interface().(sql.Scanner).Scan(v)
|
2020-02-15 11:04:21 +03:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2020-08-13 07:05:55 +03:00
|
|
|
} else if _, ok := fieldValue.Interface().(sql.Scanner); ok {
|
|
|
|
// struct scanner
|
2022-02-16 10:30:43 +03:00
|
|
|
field.Set = func(ctx context.Context, value reflect.Value, v interface{}) (err error) {
|
2020-07-30 05:23:35 +03:00
|
|
|
reflectV := reflect.ValueOf(v)
|
2020-08-13 07:05:55 +03:00
|
|
|
if !reflectV.IsValid() {
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).Set(reflect.New(field.FieldType).Elem())
|
2020-08-13 07:05:55 +03:00
|
|
|
} else if reflectV.Type().AssignableTo(field.FieldType) {
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).Set(reflectV)
|
2020-06-03 03:44:13 +03:00
|
|
|
} else if reflectV.Kind() == reflect.Ptr {
|
2020-08-13 07:05:55 +03:00
|
|
|
if reflectV.IsNil() || !reflectV.IsValid() {
|
2022-02-16 10:30:43 +03:00
|
|
|
field.ReflectValueOf(ctx, value).Set(reflect.New(field.FieldType).Elem())
|
2020-06-03 03:44:13 +03:00
|
|
|
} else {
|
2022-02-16 10:30:43 +03:00
|
|
|
return field.Set(ctx, value, reflectV.Elem().Interface())
|
2020-06-03 03:44:13 +03:00
|
|
|
}
|
2020-05-24 17:52:16 +03:00
|
|
|
} else {
|
2020-08-13 07:05:55 +03:00
|
|
|
if valuer, ok := v.(driver.Valuer); ok {
|
|
|
|
v, _ = valuer.Value()
|
2020-02-15 19:37:59 +03:00
|
|
|
}
|
2020-08-13 07:05:55 +03:00
|
|
|
|
2022-02-16 10:30:43 +03:00
|
|
|
err = field.ReflectValueOf(ctx, value).Addr().Interface().(sql.Scanner).Scan(v)
|
2020-02-15 14:45:27 +03:00
|
|
|
}
|
2020-02-15 19:37:59 +03:00
|
|
|
return
|
2020-02-15 14:45:27 +03:00
|
|
|
}
|
|
|
|
} else {
|
2022-02-16 10:30:43 +03:00
|
|
|
field.Set = func(ctx context.Context, value reflect.Value, v interface{}) (err error) {
|
|
|
|
return fallbackSetter(ctx, value, v, field.Set)
|
2020-02-15 11:04:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-16 10:30:43 +03:00
|
|
|
|
|
|
|
if field.Serializer != nil {
|
|
|
|
var (
|
|
|
|
oldFieldSetter = field.Set
|
|
|
|
sameElemType bool
|
|
|
|
sameType = field.FieldType == reflect.ValueOf(field.Serializer).Type()
|
|
|
|
)
|
|
|
|
|
|
|
|
if reflect.ValueOf(field.Serializer).Kind() == reflect.Ptr {
|
|
|
|
sameElemType = field.FieldType == reflect.ValueOf(field.Serializer).Type().Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
field.Set = func(ctx context.Context, value reflect.Value, v interface{}) (err error) {
|
|
|
|
if s, ok := v.(*serializer); ok {
|
2022-03-18 12:12:17 +03:00
|
|
|
if s.fieldValue != nil {
|
|
|
|
err = oldFieldSetter(ctx, value, s.fieldValue)
|
|
|
|
} else if err = s.Serializer.Scan(ctx, field, value, s.value); err == nil {
|
2022-02-16 10:30:43 +03:00
|
|
|
if sameElemType {
|
|
|
|
field.ReflectValueOf(ctx, value).Set(reflect.ValueOf(s.Serializer).Elem())
|
|
|
|
s.Serializer = reflect.New(reflect.Indirect(reflect.ValueOf(field.Serializer)).Type()).Interface().(SerializerInterface)
|
|
|
|
} else if sameType {
|
|
|
|
field.ReflectValueOf(ctx, value).Set(reflect.ValueOf(s.Serializer))
|
|
|
|
s.Serializer = reflect.New(reflect.Indirect(reflect.ValueOf(field.Serializer)).Type()).Interface().(SerializerInterface)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = oldFieldSetter(ctx, value, v)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-02-15 11:04:21 +03:00
|
|
|
}
|
2022-03-18 12:12:17 +03:00
|
|
|
|
|
|
|
func (field *Field) setupNewValuePool() {
|
|
|
|
var fieldValue = reflect.New(field.FieldType).Interface()
|
|
|
|
if field.Serializer != nil {
|
|
|
|
field.NewValuePool = &sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return &serializer{
|
|
|
|
Field: field,
|
|
|
|
Serializer: reflect.New(reflect.Indirect(reflect.ValueOf(field.Serializer)).Type()).Interface().(SerializerInterface),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else if _, ok := fieldValue.(sql.Scanner); !ok {
|
|
|
|
field.setupDefaultNewValuePool()
|
|
|
|
}
|
|
|
|
|
|
|
|
if field.NewValuePool == nil {
|
|
|
|
field.NewValuePool = poolInitializer(reflect.PtrTo(field.IndirectFieldType))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (field *Field) setupDefaultNewValuePool() {
|
|
|
|
// set default NewValuePool
|
|
|
|
switch field.IndirectFieldType.Kind() {
|
|
|
|
case reflect.String:
|
|
|
|
field.NewValuePool = stringPool
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
field.NewValuePool = intPool
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
|
|
field.NewValuePool = uintPool
|
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
field.NewValuePool = floatPool
|
|
|
|
case reflect.Bool:
|
|
|
|
field.NewValuePool = boolPool
|
|
|
|
default:
|
|
|
|
if field.IndirectFieldType == TimeReflectType {
|
|
|
|
field.NewValuePool = timePool
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|