| 1 |
package schema |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"database/sql" |
| 6 |
"database/sql/driver" |
| 7 |
"fmt" |
| 8 |
"reflect" |
| 9 |
"strconv" |
| 10 |
"strings" |
| 11 |
"sync" |
| 12 |
"time" |
| 13 |
|
| 14 |
"github.com/jinzhu/now" |
| 15 |
"gorm.io/gorm/clause" |
| 16 |
"gorm.io/gorm/utils" |
| 17 |
) |
| 18 |
|
| 19 |
// special types' reflect type |
| 20 |
var ( |
| 21 |
TimeReflectType = reflect.TypeOf(time.Time{}) |
| 22 |
TimePtrReflectType = reflect.TypeOf(&time.Time{}) |
| 23 |
ByteReflectType = reflect.TypeOf(uint8(0)) |
| 24 |
) |
| 25 |
|
| 26 |
type ( |
| 27 |
// DataType GORM data type |
| 28 |
DataType string |
| 29 |
// TimeType GORM time type |
| 30 |
TimeType int64 |
| 31 |
) |
| 32 |
|
| 33 |
// GORM time types |
| 34 |
const ( |
| 35 |
UnixTime TimeType = 1 |
| 36 |
UnixSecond TimeType = 2 |
| 37 |
UnixMillisecond TimeType = 3 |
| 38 |
UnixNanosecond TimeType = 4 |
| 39 |
) |
| 40 |
|
| 41 |
// GORM fields types |
| 42 |
const ( |
| 43 |
Bool DataType = "bool" |
| 44 |
Int DataType = "int" |
| 45 |
Uint DataType = "uint" |
| 46 |
Float DataType = "float" |
| 47 |
String DataType = "string" |
| 48 |
Time DataType = "time" |
| 49 |
Bytes DataType = "bytes" |
| 50 |
) |
| 51 |
|
| 52 |
const DefaultAutoIncrementIncrement int64 = 1 |
| 53 |
|
| 54 |
// Field is the representation of model schema's field |
| 55 |
type Field struct { |
| 56 |
Name string |
| 57 |
DBName string |
| 58 |
BindNames []string |
| 59 |
EmbeddedBindNames []string |
| 60 |
DataType DataType |
| 61 |
GORMDataType DataType |
| 62 |
PrimaryKey bool |
| 63 |
AutoIncrement bool |
| 64 |
AutoIncrementIncrement int64 |
| 65 |
Creatable bool |
| 66 |
Updatable bool |
| 67 |
Readable bool |
| 68 |
AutoCreateTime TimeType |
| 69 |
AutoUpdateTime TimeType |
| 70 |
HasDefaultValue bool |
| 71 |
DefaultValue string |
| 72 |
DefaultValueInterface interface{} |
| 73 |
NotNull bool |
| 74 |
Unique bool |
| 75 |
Comment string |
| 76 |
Size int |
| 77 |
Precision int |
| 78 |
Scale int |
| 79 |
IgnoreMigration bool |
| 80 |
FieldType reflect.Type |
| 81 |
IndirectFieldType reflect.Type |
| 82 |
StructField reflect.StructField |
| 83 |
Tag reflect.StructTag |
| 84 |
TagSettings map[string]string |
| 85 |
Schema *Schema |
| 86 |
EmbeddedSchema *Schema |
| 87 |
OwnerSchema *Schema |
| 88 |
ReflectValueOf func(context.Context, reflect.Value) reflect.Value |
| 89 |
ValueOf func(context.Context, reflect.Value) (value interface{}, zero bool) |
| 90 |
Set func(context.Context, reflect.Value, interface{}) error |
| 91 |
Serializer SerializerInterface |
| 92 |
NewValuePool FieldNewValuePool |
| 93 |
|
| 94 |
// In some db (e.g. MySQL), Unique and UniqueIndex are indistinguishable. |
| 95 |
// When a column has a (not Mul) UniqueIndex, Migrator always reports its gorm.ColumnType is Unique. |
| 96 |
// It causes field unnecessarily migration. |
| 97 |
// Therefore, we need to record the UniqueIndex on this column (exclude Mul UniqueIndex) for MigrateColumnUnique. |
| 98 |
UniqueIndex string |
| 99 |
} |
| 100 |
|
| 101 |
func (field *Field) BindName() string { |
| 102 |
return strings.Join(field.BindNames, ".") |
| 103 |
} |
| 104 |
|
| 105 |
// ParseField parses reflect.StructField to Field |
| 106 |
func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field { |
| 107 |
var ( |
| 108 |
err error |
| 109 |
tagSetting = ParseTagSetting(fieldStruct.Tag.Get("gorm"), ";") |
| 110 |
) |
| 111 |
|
| 112 |
field := &Field{ |
| 113 |
Name: fieldStruct.Name, |
| 114 |
DBName: tagSetting["COLUMN"], |
| 115 |
BindNames: []string{fieldStruct.Name}, |
| 116 |
EmbeddedBindNames: []string{fieldStruct.Name}, |
| 117 |
FieldType: fieldStruct.Type, |
| 118 |
IndirectFieldType: fieldStruct.Type, |
| 119 |
StructField: fieldStruct, |
| 120 |
Tag: fieldStruct.Tag, |
| 121 |
TagSettings: tagSetting, |
| 122 |
Schema: schema, |
| 123 |
Creatable: true, |
| 124 |
Updatable: true, |
| 125 |
Readable: true, |
| 126 |
PrimaryKey: utils.CheckTruth(tagSetting["PRIMARYKEY"], tagSetting["PRIMARY_KEY"]), |
| 127 |
AutoIncrement: utils.CheckTruth(tagSetting["AUTOINCREMENT"]), |
| 128 |
HasDefaultValue: utils.CheckTruth(tagSetting["AUTOINCREMENT"]), |
| 129 |
NotNull: utils.CheckTruth(tagSetting["NOT NULL"], tagSetting["NOTNULL"]), |
| 130 |
Unique: utils.CheckTruth(tagSetting["UNIQUE"]), |
| 131 |
Comment: tagSetting["COMMENT"], |
| 132 |
AutoIncrementIncrement: DefaultAutoIncrementIncrement, |
| 133 |
} |
| 134 |
|
| 135 |
for field.IndirectFieldType.Kind() == reflect.Ptr { |
| 136 |
field.IndirectFieldType = field.IndirectFieldType.Elem() |
| 137 |
} |
| 138 |
|
| 139 |
fieldValue := reflect.New(field.IndirectFieldType) |
| 140 |
// if field is valuer, used its value or first field as data type |
| 141 |
valuer, isValuer := fieldValue.Interface().(driver.Valuer) |
| 142 |
if isValuer { |
| 143 |
if _, ok := fieldValue.Interface().(GormDataTypeInterface); !ok { |
| 144 |
if v, err := valuer.Value(); reflect.ValueOf(v).IsValid() && err == nil { |
| 145 |
fieldValue = reflect.ValueOf(v) |
| 146 |
} |
| 147 |
|
| 148 |
// Use the field struct's first field type as data type, e.g: use `string` for sql.NullString |
| 149 |
var getRealFieldValue func(reflect.Value) |
| 150 |
getRealFieldValue = func(v reflect.Value) { |
| 151 |
var ( |
| 152 |
rv = reflect.Indirect(v) |
| 153 |
rvType = rv.Type() |
| 154 |
) |
| 155 |
|
| 156 |
if rv.Kind() == reflect.Struct && !rvType.ConvertibleTo(TimeReflectType) { |
| 157 |
for i := 0; i < rvType.NumField(); i++ { |
| 158 |
for key, value := range ParseTagSetting(rvType.Field(i).Tag.Get("gorm"), ";") { |
| 159 |
if _, ok := field.TagSettings[key]; !ok { |
| 160 |
field.TagSettings[key] = value |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
for i := 0; i < rvType.NumField(); i++ { |
| 166 |
newFieldType := rvType.Field(i).Type |
| 167 |
for newFieldType.Kind() == reflect.Ptr { |
| 168 |
newFieldType = newFieldType.Elem() |
| 169 |
} |
| 170 |
|
| 171 |
fieldValue = reflect.New(newFieldType) |
| 172 |
if rvType != reflect.Indirect(fieldValue).Type() { |
| 173 |
getRealFieldValue(fieldValue) |
| 174 |
} |
| 175 |
|
| 176 |
if fieldValue.IsValid() { |
| 177 |
return |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
getRealFieldValue(fieldValue) |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
if v, isSerializer := fieldValue.Interface().(SerializerInterface); isSerializer { |
| 188 |
field.DataType = String |
| 189 |
field.Serializer = v |
| 190 |
} else { |
| 191 |
serializerName := field.TagSettings["JSON"] |
| 192 |
if serializerName == "" { |
| 193 |
serializerName = field.TagSettings["SERIALIZER"] |
| 194 |
} |
| 195 |
if serializerName != "" { |
| 196 |
if serializer, ok := GetSerializer(serializerName); ok { |
| 197 |
// Set default data type to string for serializer |
| 198 |
field.DataType = String |
| 199 |
field.Serializer = serializer |
| 200 |
} else { |
| 201 |
schema.err = fmt.Errorf("invalid serializer type %v", serializerName) |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
if num, ok := field.TagSettings["AUTOINCREMENTINCREMENT"]; ok { |
| 207 |
field.AutoIncrementIncrement, _ = strconv.ParseInt(num, 10, 64) |
| 208 |
} |
| 209 |
|
| 210 |
if v, ok := field.TagSettings["DEFAULT"]; ok { |
| 211 |
field.HasDefaultValue = true |
| 212 |
field.DefaultValue = v |
| 213 |
} |
| 214 |
|
| 215 |
if num, ok := field.TagSettings["SIZE"]; ok { |
| 216 |
if field.Size, err = strconv.Atoi(num); err != nil { |
| 217 |
field.Size = -1 |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
if p, ok := field.TagSettings["PRECISION"]; ok { |
| 222 |
field.Precision, _ = strconv.Atoi(p) |
| 223 |
} |
| 224 |
|
| 225 |
if s, ok := field.TagSettings["SCALE"]; ok { |
| 226 |
field.Scale, _ = strconv.Atoi(s) |
| 227 |
} |
| 228 |
|
| 229 |
// default value is function or null or blank (primary keys) |
| 230 |
field.DefaultValue = strings.TrimSpace(field.DefaultValue) |
| 231 |
skipParseDefaultValue := strings.Contains(field.DefaultValue, "(") && |
| 232 |
strings.Contains(field.DefaultValue, ")") || strings.ToLower(field.DefaultValue) == "null" || field.DefaultValue == "" |
| 233 |
switch reflect.Indirect(fieldValue).Kind() { |
| 234 |
case reflect.Bool: |
| 235 |
field.DataType = Bool |
| 236 |
if field.HasDefaultValue && !skipParseDefaultValue { |
| 237 |
if field.DefaultValueInterface, err = strconv.ParseBool(field.DefaultValue); err != nil { |
| 238 |
schema.err = fmt.Errorf("failed to parse %s as default value for bool, got error: %v", field.DefaultValue, err) |
| 239 |
} |
| 240 |
} |
| 241 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 242 |
field.DataType = Int |
| 243 |
if field.HasDefaultValue && !skipParseDefaultValue { |
| 244 |
if field.DefaultValueInterface, err = strconv.ParseInt(field.DefaultValue, 0, 64); err != nil { |
| 245 |
schema.err = fmt.Errorf("failed to parse %s as default value for int, got error: %v", field.DefaultValue, err) |
| 246 |
} |
| 247 |
} |
| 248 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
| 249 |
field.DataType = Uint |
| 250 |
if field.HasDefaultValue && !skipParseDefaultValue { |
| 251 |
if field.DefaultValueInterface, err = strconv.ParseUint(field.DefaultValue, 0, 64); err != nil { |
| 252 |
schema.err = fmt.Errorf("failed to parse %s as default value for uint, got error: %v", field.DefaultValue, err) |
| 253 |
} |
| 254 |
} |
| 255 |
case reflect.Float32, reflect.Float64: |
| 256 |
field.DataType = Float |
| 257 |
if field.HasDefaultValue && !skipParseDefaultValue { |
| 258 |
if field.DefaultValueInterface, err = strconv.ParseFloat(field.DefaultValue, 64); err != nil { |
| 259 |
schema.err = fmt.Errorf("failed to parse %s as default value for float, got error: %v", field.DefaultValue, err) |
| 260 |
} |
| 261 |
} |
| 262 |
case reflect.String: |
| 263 |
field.DataType = String |
| 264 |
if field.HasDefaultValue && !skipParseDefaultValue { |
| 265 |
field.DefaultValue = strings.Trim(field.DefaultValue, "'") |
| 266 |
field.DefaultValue = strings.Trim(field.DefaultValue, `"`) |
| 267 |
field.DefaultValueInterface = field.DefaultValue |
| 268 |
} |
| 269 |
case reflect.Struct: |
| 270 |
if _, ok := fieldValue.Interface().(*time.Time); ok { |
| 271 |
field.DataType = Time |
| 272 |
} else if fieldValue.Type().ConvertibleTo(TimeReflectType) { |
| 273 |
field.DataType = Time |
| 274 |
} else if fieldValue.Type().ConvertibleTo(TimePtrReflectType) { |
| 275 |
field.DataType = Time |
| 276 |
} |
| 277 |
if field.HasDefaultValue && !skipParseDefaultValue && field.DataType == Time { |
| 278 |
if t, err := now.Parse(field.DefaultValue); err == nil { |
| 279 |
field.DefaultValueInterface = t |
| 280 |
} |
| 281 |
} |
| 282 |
case reflect.Array, reflect.Slice: |
| 283 |
if reflect.Indirect(fieldValue).Type().Elem() == ByteReflectType && field.DataType == "" { |
| 284 |
field.DataType = Bytes |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
if dataTyper, ok := fieldValue.Interface().(GormDataTypeInterface); ok { |
| 289 |
field.DataType = DataType(dataTyper.GormDataType()) |
| 290 |
} |
| 291 |
|
| 292 |
if v, ok := field.TagSettings["AUTOCREATETIME"]; (ok && utils.CheckTruth(v)) || (!ok && field.Name == "CreatedAt" && (field.DataType == Time || field.DataType == Int || field.DataType == Uint)) { |
| 293 |
if field.DataType == Time { |
| 294 |
field.AutoCreateTime = UnixTime |
| 295 |
} else if strings.ToUpper(v) == "NANO" { |
| 296 |
field.AutoCreateTime = UnixNanosecond |
| 297 |
} else if strings.ToUpper(v) == "MILLI" { |
| 298 |
field.AutoCreateTime = UnixMillisecond |
| 299 |
} else { |
| 300 |
field.AutoCreateTime = UnixSecond |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
if v, ok := field.TagSettings["AUTOUPDATETIME"]; (ok && utils.CheckTruth(v)) || (!ok && field.Name == "UpdatedAt" && (field.DataType == Time || field.DataType == Int || field.DataType == Uint)) { |
| 305 |
if field.DataType == Time { |
| 306 |
field.AutoUpdateTime = UnixTime |
| 307 |
} else if strings.ToUpper(v) == "NANO" { |
| 308 |
field.AutoUpdateTime = UnixNanosecond |
| 309 |
} else if strings.ToUpper(v) == "MILLI" { |
| 310 |
field.AutoUpdateTime = UnixMillisecond |
| 311 |
} else { |
| 312 |
field.AutoUpdateTime = UnixSecond |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
if field.GORMDataType == "" { |
| 317 |
field.GORMDataType = field.DataType |
| 318 |
} |
| 319 |
|
| 320 |
if val, ok := field.TagSettings["TYPE"]; ok { |
| 321 |
lowerVal := DataType(strings.ToLower(val)) |
| 322 |
switch lowerVal { |
| 323 |
case Bool, Int, Uint, Float, String, Time, Bytes: |
| 324 |
field.DataType = lowerVal |
| 325 |
default: |
| 326 |
field.DataType = DataType(val) |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
if field.Size == 0 { |
| 331 |
switch reflect.Indirect(fieldValue).Kind() { |
| 332 |
case reflect.Int, reflect.Int64, reflect.Uint, reflect.Uint64, reflect.Float64: |
| 333 |
field.Size = 64 |
| 334 |
case reflect.Int8, reflect.Uint8: |
| 335 |
field.Size = 8 |
| 336 |
case reflect.Int16, reflect.Uint16: |
| 337 |
field.Size = 16 |
| 338 |
case reflect.Int32, reflect.Uint32, reflect.Float32: |
| 339 |
field.Size = 32 |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
// setup permission |
| 344 |
if val, ok := field.TagSettings["-"]; ok { |
| 345 |
val = strings.ToLower(strings.TrimSpace(val)) |
| 346 |
switch val { |
| 347 |
case "-": |
| 348 |
field.Creatable = false |
| 349 |
field.Updatable = false |
| 350 |
field.Readable = false |
| 351 |
field.DataType = "" |
| 352 |
case "all": |
| 353 |
field.Creatable = false |
| 354 |
field.Updatable = false |
| 355 |
field.Readable = false |
| 356 |
field.DataType = "" |
| 357 |
field.IgnoreMigration = true |
| 358 |
case "migration": |
| 359 |
field.IgnoreMigration = true |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
if v, ok := field.TagSettings["->"]; ok { |
| 364 |
field.Creatable = false |
| 365 |
field.Updatable = false |
| 366 |
if strings.ToLower(v) == "false" { |
| 367 |
field.Readable = false |
| 368 |
} else { |
| 369 |
field.Readable = true |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
if v, ok := field.TagSettings["<-"]; ok { |
| 374 |
field.Creatable = true |
| 375 |
field.Updatable = true |
| 376 |
|
| 377 |
if v != "<-" { |
| 378 |
if !strings.Contains(v, "create") { |
| 379 |
field.Creatable = false |
| 380 |
} |
| 381 |
|
| 382 |
if !strings.Contains(v, "update") { |
| 383 |
field.Updatable = false |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
// Normal anonymous field or having `EMBEDDED` tag |
| 389 |
if _, ok := field.TagSettings["EMBEDDED"]; ok || (field.GORMDataType != Time && field.GORMDataType != Bytes && !isValuer && |
| 390 |
fieldStruct.Anonymous && (field.Creatable || field.Updatable || field.Readable)) { |
| 391 |
kind := reflect.Indirect(fieldValue).Kind() |
| 392 |
switch kind { |
| 393 |
case reflect.Struct: |
| 394 |
var err error |
| 395 |
field.Creatable = false |
| 396 |
field.Updatable = false |
| 397 |
field.Readable = false |
| 398 |
|
| 399 |
cacheStore := &sync.Map{} |
| 400 |
cacheStore.Store(embeddedCacheKey, true) |
| 401 |
if field.EmbeddedSchema, err = getOrParse(fieldValue.Interface(), cacheStore, embeddedNamer{Table: schema.Table, Namer: schema.namer}); err != nil { |
| 402 |
schema.err = err |
| 403 |
} |
| 404 |
|
| 405 |
for _, ef := range field.EmbeddedSchema.Fields { |
| 406 |
ef.Schema = schema |
| 407 |
ef.OwnerSchema = field.EmbeddedSchema |
| 408 |
ef.BindNames = append([]string{fieldStruct.Name}, ef.BindNames...) |
| 409 |
if _, ok := field.TagSettings["EMBEDDED"]; ok || !fieldStruct.Anonymous { |
| 410 |
ef.EmbeddedBindNames = append([]string{fieldStruct.Name}, ef.EmbeddedBindNames...) |
| 411 |
} |
| 412 |
// index is negative means is pointer |
| 413 |
if field.FieldType.Kind() == reflect.Struct { |
| 414 |
ef.StructField.Index = append([]int{fieldStruct.Index[0]}, ef.StructField.Index...) |
| 415 |
} else { |
| 416 |
ef.StructField.Index = append([]int{-fieldStruct.Index[0] - 1}, ef.StructField.Index...) |
| 417 |
} |
| 418 |
|
| 419 |
if prefix, ok := field.TagSettings["EMBEDDEDPREFIX"]; ok && ef.DBName != "" { |
| 420 |
ef.DBName = prefix + ef.DBName |
| 421 |
} |
| 422 |
|
| 423 |
if ef.PrimaryKey { |
| 424 |
if !utils.CheckTruth(ef.TagSettings["PRIMARYKEY"], ef.TagSettings["PRIMARY_KEY"]) { |
| 425 |
ef.PrimaryKey = false |
| 426 |
|
| 427 |
if val, ok := ef.TagSettings["AUTOINCREMENT"]; !ok || !utils.CheckTruth(val) { |
| 428 |
ef.AutoIncrement = false |
| 429 |
} |
| 430 |
|
| 431 |
if !ef.AutoIncrement && ef.DefaultValue == "" { |
| 432 |
ef.HasDefaultValue = false |
| 433 |
} |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
for k, v := range field.TagSettings { |
| 438 |
ef.TagSettings[k] = v |
| 439 |
} |
| 440 |
} |
| 441 |
case reflect.Invalid, reflect.Uintptr, reflect.Array, reflect.Chan, reflect.Func, reflect.Interface, |
| 442 |
reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer, reflect.Complex64, reflect.Complex128: |
| 443 |
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) |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
return field |
| 448 |
} |
| 449 |
|
| 450 |
// create valuer, setter when parse struct |
| 451 |
func (field *Field) setupValuerAndSetter(modelType reflect.Type) { |
| 452 |
// Setup NewValuePool |
| 453 |
field.setupNewValuePool() |
| 454 |
|
| 455 |
// ValueOf returns field's value and if it is zero |
| 456 |
fieldIndex := field.StructField.Index[0] |
| 457 |
switch { |
| 458 |
case len(field.StructField.Index) == 1 && fieldIndex >= 0: |
| 459 |
field.ValueOf = func(ctx context.Context, v reflect.Value) (interface{}, bool) { |
| 460 |
v = reflect.Indirect(v) |
| 461 |
fieldValue := v.Field(fieldIndex) |
| 462 |
return fieldValue.Interface(), fieldValue.IsZero() |
| 463 |
} |
| 464 |
default: |
| 465 |
field.ValueOf = func(ctx context.Context, v reflect.Value) (interface{}, bool) { |
| 466 |
v = reflect.Indirect(v) |
| 467 |
for _, fieldIdx := range field.StructField.Index { |
| 468 |
if fieldIdx >= 0 { |
| 469 |
v = v.Field(fieldIdx) |
| 470 |
} else { |
| 471 |
v = v.Field(-fieldIdx - 1) |
| 472 |
|
| 473 |
if !v.IsNil() { |
| 474 |
v = v.Elem() |
| 475 |
} else { |
| 476 |
return nil, true |
| 477 |
} |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
fv, zero := v.Interface(), v.IsZero() |
| 482 |
return fv, zero |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
if field.Serializer != nil { |
| 487 |
oldValuerOf := field.ValueOf |
| 488 |
field.ValueOf = func(ctx context.Context, v reflect.Value) (interface{}, bool) { |
| 489 |
value, zero := oldValuerOf(ctx, v) |
| 490 |
|
| 491 |
s, ok := value.(SerializerValuerInterface) |
| 492 |
if !ok { |
| 493 |
s = field.Serializer |
| 494 |
} |
| 495 |
|
| 496 |
return &serializer{ |
| 497 |
Field: field, |
| 498 |
SerializeValuer: s, |
| 499 |
Destination: v, |
| 500 |
Context: ctx, |
| 501 |
fieldValue: value, |
| 502 |
}, zero |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
// ReflectValueOf returns field's reflect value |
| 507 |
switch { |
| 508 |
case len(field.StructField.Index) == 1 && fieldIndex >= 0: |
| 509 |
field.ReflectValueOf = func(ctx context.Context, v reflect.Value) reflect.Value { |
| 510 |
v = reflect.Indirect(v) |
| 511 |
return v.Field(fieldIndex) |
| 512 |
} |
| 513 |
default: |
| 514 |
field.ReflectValueOf = func(ctx context.Context, v reflect.Value) reflect.Value { |
| 515 |
v = reflect.Indirect(v) |
| 516 |
for idx, fieldIdx := range field.StructField.Index { |
| 517 |
if fieldIdx >= 0 { |
| 518 |
v = v.Field(fieldIdx) |
| 519 |
} else { |
| 520 |
v = v.Field(-fieldIdx - 1) |
| 521 |
|
| 522 |
if v.IsNil() { |
| 523 |
v.Set(reflect.New(v.Type().Elem())) |
| 524 |
} |
| 525 |
|
| 526 |
if idx < len(field.StructField.Index)-1 { |
| 527 |
v = v.Elem() |
| 528 |
} |
| 529 |
} |
| 530 |
} |
| 531 |
return v |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
fallbackSetter := func(ctx context.Context, value reflect.Value, v interface{}, setter func(context.Context, reflect.Value, interface{}) error) (err error) { |
| 536 |
if v == nil { |
| 537 |
field.ReflectValueOf(ctx, value).Set(reflect.New(field.FieldType).Elem()) |
| 538 |
} else { |
| 539 |
reflectV := reflect.ValueOf(v) |
| 540 |
// Optimal value type acquisition for v |
| 541 |
reflectValType := reflectV.Type() |
| 542 |
|
| 543 |
if reflectValType.AssignableTo(field.FieldType) { |
| 544 |
if reflectV.Kind() == reflect.Ptr && reflectV.Elem().Kind() == reflect.Ptr { |
| 545 |
reflectV = reflect.Indirect(reflectV) |
| 546 |
} |
| 547 |
field.ReflectValueOf(ctx, value).Set(reflectV) |
| 548 |
return |
| 549 |
} else if reflectValType.ConvertibleTo(field.FieldType) { |
| 550 |
field.ReflectValueOf(ctx, value).Set(reflectV.Convert(field.FieldType)) |
| 551 |
return |
| 552 |
} else if field.FieldType.Kind() == reflect.Ptr { |
| 553 |
fieldValue := field.ReflectValueOf(ctx, value) |
| 554 |
fieldType := field.FieldType.Elem() |
| 555 |
|
| 556 |
if reflectValType.AssignableTo(fieldType) { |
| 557 |
if !fieldValue.IsValid() { |
| 558 |
fieldValue = reflect.New(fieldType) |
| 559 |
} else if fieldValue.IsNil() { |
| 560 |
fieldValue.Set(reflect.New(fieldType)) |
| 561 |
} |
| 562 |
fieldValue.Elem().Set(reflectV) |
| 563 |
return |
| 564 |
} else if reflectValType.ConvertibleTo(fieldType) { |
| 565 |
if fieldValue.IsNil() { |
| 566 |
fieldValue.Set(reflect.New(fieldType)) |
| 567 |
} |
| 568 |
|
| 569 |
fieldValue.Elem().Set(reflectV.Convert(fieldType)) |
| 570 |
return |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
if reflectV.Kind() == reflect.Ptr { |
| 575 |
if reflectV.IsNil() { |
| 576 |
field.ReflectValueOf(ctx, value).Set(reflect.New(field.FieldType).Elem()) |
| 577 |
} else if reflectV.Type().Elem().AssignableTo(field.FieldType) { |
| 578 |
field.ReflectValueOf(ctx, value).Set(reflectV.Elem()) |
| 579 |
return |
| 580 |
} else { |
| 581 |
err = setter(ctx, value, reflectV.Elem().Interface()) |
| 582 |
} |
| 583 |
} else if valuer, ok := v.(driver.Valuer); ok { |
| 584 |
if v, err = valuer.Value(); err == nil { |
| 585 |
err = setter(ctx, value, v) |
| 586 |
} |
| 587 |
} else if _, ok := v.(clause.Expr); !ok { |
| 588 |
return fmt.Errorf("failed to set value %#v to field %s", v, field.Name) |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
return |
| 593 |
} |
| 594 |
|
| 595 |
// Set |
| 596 |
switch field.FieldType.Kind() { |
| 597 |
case reflect.Bool: |
| 598 |
field.Set = func(ctx context.Context, value reflect.Value, v interface{}) error { |
| 599 |
switch data := v.(type) { |
| 600 |
case **bool: |
| 601 |
if data != nil && *data != nil { |
| 602 |
field.ReflectValueOf(ctx, value).SetBool(**data) |
| 603 |
} |
| 604 |
case bool: |
| 605 |
field.ReflectValueOf(ctx, value).SetBool(data) |
| 606 |
case int64: |
| 607 |
field.ReflectValueOf(ctx, value).SetBool(data > 0) |
| 608 |
case string: |
| 609 |
b, _ := strconv.ParseBool(data) |
| 610 |
field.ReflectValueOf(ctx, value).SetBool(b) |
| 611 |
default: |
| 612 |
return fallbackSetter(ctx, value, v, field.Set) |
| 613 |
} |
| 614 |
return nil |
| 615 |
} |
| 616 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 617 |
field.Set = func(ctx context.Context, value reflect.Value, v interface{}) (err error) { |
| 618 |
switch data := v.(type) { |
| 619 |
case **int64: |
| 620 |
if data != nil && *data != nil { |
| 621 |
field.ReflectValueOf(ctx, value).SetInt(**data) |
| 622 |
} |
| 623 |
case **int: |
| 624 |
if data != nil && *data != nil { |
| 625 |
field.ReflectValueOf(ctx, value).SetInt(int64(**data)) |
| 626 |
} |
| 627 |
case **int8: |
| 628 |
if data != nil && *data != nil { |
| 629 |
field.ReflectValueOf(ctx, value).SetInt(int64(**data)) |
| 630 |
} |
| 631 |
case **int16: |
| 632 |
if data != nil && *data != nil { |
| 633 |
field.ReflectValueOf(ctx, value).SetInt(int64(**data)) |
| 634 |
} |
| 635 |
case **int32: |
| 636 |
if data != nil && *data != nil { |
| 637 |
field.ReflectValueOf(ctx, value).SetInt(int64(**data)) |
| 638 |
} |
| 639 |
case int64: |
| 640 |
field.ReflectValueOf(ctx, value).SetInt(data) |
| 641 |
case int: |
| 642 |
field.ReflectValueOf(ctx, value).SetInt(int64(data)) |
| 643 |
case int8: |
| 644 |
field.ReflectValueOf(ctx, value).SetInt(int64(data)) |
| 645 |
case int16: |
| 646 |
field.ReflectValueOf(ctx, value).SetInt(int64(data)) |
| 647 |
case int32: |
| 648 |
field.ReflectValueOf(ctx, value).SetInt(int64(data)) |
| 649 |
case uint: |
| 650 |
field.ReflectValueOf(ctx, value).SetInt(int64(data)) |
| 651 |
case uint8: |
| 652 |
field.ReflectValueOf(ctx, value).SetInt(int64(data)) |
| 653 |
case uint16: |
| 654 |
field.ReflectValueOf(ctx, value).SetInt(int64(data)) |
| 655 |
case uint32: |
| 656 |
field.ReflectValueOf(ctx, value).SetInt(int64(data)) |
| 657 |
case uint64: |
| 658 |
field.ReflectValueOf(ctx, value).SetInt(int64(data)) |
| 659 |
case float32: |
| 660 |
field.ReflectValueOf(ctx, value).SetInt(int64(data)) |
| 661 |
case float64: |
| 662 |
field.ReflectValueOf(ctx, value).SetInt(int64(data)) |
| 663 |
case []byte: |
| 664 |
return field.Set(ctx, value, string(data)) |
| 665 |
case string: |
| 666 |
if i, err := strconv.ParseInt(data, 0, 64); err == nil { |
| 667 |
field.ReflectValueOf(ctx, value).SetInt(i) |
| 668 |
} else { |
| 669 |
return err |
| 670 |
} |
| 671 |
case time.Time: |
| 672 |
if field.AutoCreateTime == UnixNanosecond || field.AutoUpdateTime == UnixNanosecond { |
| 673 |
field.ReflectValueOf(ctx, value).SetInt(data.UnixNano()) |
| 674 |
} else if field.AutoCreateTime == UnixMillisecond || field.AutoUpdateTime == UnixMillisecond { |
| 675 |
field.ReflectValueOf(ctx, value).SetInt(data.UnixMilli()) |
| 676 |
} else { |
| 677 |
field.ReflectValueOf(ctx, value).SetInt(data.Unix()) |
| 678 |
} |
| 679 |
case *time.Time: |
| 680 |
if data != nil { |
| 681 |
if field.AutoCreateTime == UnixNanosecond || field.AutoUpdateTime == UnixNanosecond { |
| 682 |
field.ReflectValueOf(ctx, value).SetInt(data.UnixNano()) |
| 683 |
} else if field.AutoCreateTime == UnixMillisecond || field.AutoUpdateTime == UnixMillisecond { |
| 684 |
field.ReflectValueOf(ctx, value).SetInt(data.UnixMilli()) |
| 685 |
} else { |
| 686 |
field.ReflectValueOf(ctx, value).SetInt(data.Unix()) |
| 687 |
} |
| 688 |
} else { |
| 689 |
field.ReflectValueOf(ctx, value).SetInt(0) |
| 690 |
} |
| 691 |
default: |
| 692 |
return fallbackSetter(ctx, value, v, field.Set) |
| 693 |
} |
| 694 |
return err |
| 695 |
} |
| 696 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
| 697 |
field.Set = func(ctx context.Context, value reflect.Value, v interface{}) (err error) { |
| 698 |
switch data := v.(type) { |
| 699 |
case **uint64: |
| 700 |
if data != nil && *data != nil { |
| 701 |
field.ReflectValueOf(ctx, value).SetUint(**data) |
| 702 |
} |
| 703 |
case **uint: |
| 704 |
if data != nil && *data != nil { |
| 705 |
field.ReflectValueOf(ctx, value).SetUint(uint64(**data)) |
| 706 |
} |
| 707 |
case **uint8: |
| 708 |
if data != nil && *data != nil { |
| 709 |
field.ReflectValueOf(ctx, value).SetUint(uint64(**data)) |
| 710 |
} |
| 711 |
case **uint16: |
| 712 |
if data != nil && *data != nil { |
| 713 |
field.ReflectValueOf(ctx, value).SetUint(uint64(**data)) |
| 714 |
} |
| 715 |
case **uint32: |
| 716 |
if data != nil && *data != nil { |
| 717 |
field.ReflectValueOf(ctx, value).SetUint(uint64(**data)) |
| 718 |
} |
| 719 |
case uint64: |
| 720 |
field.ReflectValueOf(ctx, value).SetUint(data) |
| 721 |
case uint: |
| 722 |
field.ReflectValueOf(ctx, value).SetUint(uint64(data)) |
| 723 |
case uint8: |
| 724 |
field.ReflectValueOf(ctx, value).SetUint(uint64(data)) |
| 725 |
case uint16: |
| 726 |
field.ReflectValueOf(ctx, value).SetUint(uint64(data)) |
| 727 |
case uint32: |
| 728 |
field.ReflectValueOf(ctx, value).SetUint(uint64(data)) |
| 729 |
case int64: |
| 730 |
field.ReflectValueOf(ctx, value).SetUint(uint64(data)) |
| 731 |
case int: |
| 732 |
field.ReflectValueOf(ctx, value).SetUint(uint64(data)) |
| 733 |
case int8: |
| 734 |
field.ReflectValueOf(ctx, value).SetUint(uint64(data)) |
| 735 |
case int16: |
| 736 |
field.ReflectValueOf(ctx, value).SetUint(uint64(data)) |
| 737 |
case int32: |
| 738 |
field.ReflectValueOf(ctx, value).SetUint(uint64(data)) |
| 739 |
case float32: |
| 740 |
field.ReflectValueOf(ctx, value).SetUint(uint64(data)) |
| 741 |
case float64: |
| 742 |
field.ReflectValueOf(ctx, value).SetUint(uint64(data)) |
| 743 |
case []byte: |
| 744 |
return field.Set(ctx, value, string(data)) |
| 745 |
case time.Time: |
| 746 |
if field.AutoCreateTime == UnixNanosecond || field.AutoUpdateTime == UnixNanosecond { |
| 747 |
field.ReflectValueOf(ctx, value).SetUint(uint64(data.UnixNano())) |
| 748 |
} else if field.AutoCreateTime == UnixMillisecond || field.AutoUpdateTime == UnixMillisecond { |
| 749 |
field.ReflectValueOf(ctx, value).SetUint(uint64(data.UnixMilli())) |
| 750 |
} else { |
| 751 |
field.ReflectValueOf(ctx, value).SetUint(uint64(data.Unix())) |
| 752 |
} |
| 753 |
case string: |
| 754 |
if i, err := strconv.ParseUint(data, 0, 64); err == nil { |
| 755 |
field.ReflectValueOf(ctx, value).SetUint(i) |
| 756 |
} else { |
| 757 |
return err |
| 758 |
} |
| 759 |
default: |
| 760 |
return fallbackSetter(ctx, value, v, field.Set) |
| 761 |
} |
| 762 |
return err |
| 763 |
} |
| 764 |
case reflect.Float32, reflect.Float64: |
| 765 |
field.Set = func(ctx context.Context, value reflect.Value, v interface{}) (err error) { |
| 766 |
switch data := v.(type) { |
| 767 |
case **float64: |
| 768 |
if data != nil && *data != nil { |
| 769 |
field.ReflectValueOf(ctx, value).SetFloat(**data) |
| 770 |
} |
| 771 |
case **float32: |
| 772 |
if data != nil && *data != nil { |
| 773 |
field.ReflectValueOf(ctx, value).SetFloat(float64(**data)) |
| 774 |
} |
| 775 |
case float64: |
| 776 |
field.ReflectValueOf(ctx, value).SetFloat(data) |
| 777 |
case float32: |
| 778 |
field.ReflectValueOf(ctx, value).SetFloat(float64(data)) |
| 779 |
case int64: |
| 780 |
field.ReflectValueOf(ctx, value).SetFloat(float64(data)) |
| 781 |
case int: |
| 782 |
field.ReflectValueOf(ctx, value).SetFloat(float64(data)) |
| 783 |
case int8: |
| 784 |
field.ReflectValueOf(ctx, value).SetFloat(float64(data)) |
| 785 |
case int16: |
| 786 |
field.ReflectValueOf(ctx, value).SetFloat(float64(data)) |
| 787 |
case int32: |
| 788 |
field.ReflectValueOf(ctx, value).SetFloat(float64(data)) |
| 789 |
case uint: |
| 790 |
field.ReflectValueOf(ctx, value).SetFloat(float64(data)) |
| 791 |
case uint8: |
| 792 |
field.ReflectValueOf(ctx, value).SetFloat(float64(data)) |
| 793 |
case uint16: |
| 794 |
field.ReflectValueOf(ctx, value).SetFloat(float64(data)) |
| 795 |
case uint32: |
| 796 |
field.ReflectValueOf(ctx, value).SetFloat(float64(data)) |
| 797 |
case uint64: |
| 798 |
field.ReflectValueOf(ctx, value).SetFloat(float64(data)) |
| 799 |
case []byte: |
| 800 |
return field.Set(ctx, value, string(data)) |
| 801 |
case string: |
| 802 |
if i, err := strconv.ParseFloat(data, 64); err == nil { |
| 803 |
field.ReflectValueOf(ctx, value).SetFloat(i) |
| 804 |
} else { |
| 805 |
return err |
| 806 |
} |
| 807 |
default: |
| 808 |
return fallbackSetter(ctx, value, v, field.Set) |
| 809 |
} |
| 810 |
return err |
| 811 |
} |
| 812 |
case reflect.String: |
| 813 |
field.Set = func(ctx context.Context, value reflect.Value, v interface{}) (err error) { |
| 814 |
switch data := v.(type) { |
| 815 |
case **string: |
| 816 |
if data != nil && *data != nil { |
| 817 |
field.ReflectValueOf(ctx, value).SetString(**data) |
| 818 |
} |
| 819 |
case string: |
| 820 |
field.ReflectValueOf(ctx, value).SetString(data) |
| 821 |
case []byte: |
| 822 |
field.ReflectValueOf(ctx, value).SetString(string(data)) |
| 823 |
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: |
| 824 |
field.ReflectValueOf(ctx, value).SetString(utils.ToString(data)) |
| 825 |
case float64, float32: |
| 826 |
field.ReflectValueOf(ctx, value).SetString(fmt.Sprintf("%."+strconv.Itoa(field.Precision)+"f", data)) |
| 827 |
default: |
| 828 |
return fallbackSetter(ctx, value, v, field.Set) |
| 829 |
} |
| 830 |
return err |
| 831 |
} |
| 832 |
default: |
| 833 |
fieldValue := reflect.New(field.FieldType) |
| 834 |
switch fieldValue.Elem().Interface().(type) { |
| 835 |
case time.Time: |
| 836 |
field.Set = func(ctx context.Context, value reflect.Value, v interface{}) error { |
| 837 |
switch data := v.(type) { |
| 838 |
case **time.Time: |
| 839 |
if data != nil && *data != nil { |
| 840 |
field.Set(ctx, value, *data) |
| 841 |
} |
| 842 |
case time.Time: |
| 843 |
field.ReflectValueOf(ctx, value).Set(reflect.ValueOf(v)) |
| 844 |
case *time.Time: |
| 845 |
if data != nil { |
| 846 |
field.ReflectValueOf(ctx, value).Set(reflect.ValueOf(data).Elem()) |
| 847 |
} else { |
| 848 |
field.ReflectValueOf(ctx, value).Set(reflect.ValueOf(time.Time{})) |
| 849 |
} |
| 850 |
case string: |
| 851 |
if t, err := now.Parse(data); err == nil { |
| 852 |
field.ReflectValueOf(ctx, value).Set(reflect.ValueOf(t)) |
| 853 |
} else { |
| 854 |
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) |
| 855 |
} |
| 856 |
default: |
| 857 |
return fallbackSetter(ctx, value, v, field.Set) |
| 858 |
} |
| 859 |
return nil |
| 860 |
} |
| 861 |
case *time.Time: |
| 862 |
field.Set = func(ctx context.Context, value reflect.Value, v interface{}) error { |
| 863 |
switch data := v.(type) { |
| 864 |
case **time.Time: |
| 865 |
if data != nil && *data != nil { |
| 866 |
field.ReflectValueOf(ctx, value).Set(reflect.ValueOf(*data)) |
| 867 |
} |
| 868 |
case time.Time: |
| 869 |
fieldValue := field.ReflectValueOf(ctx, value) |
| 870 |
if fieldValue.IsNil() { |
| 871 |
fieldValue.Set(reflect.New(field.FieldType.Elem())) |
| 872 |
} |
| 873 |
fieldValue.Elem().Set(reflect.ValueOf(v)) |
| 874 |
case *time.Time: |
| 875 |
field.ReflectValueOf(ctx, value).Set(reflect.ValueOf(v)) |
| 876 |
case string: |
| 877 |
if t, err := now.Parse(data); err == nil { |
| 878 |
fieldValue := field.ReflectValueOf(ctx, value) |
| 879 |
if fieldValue.IsNil() { |
| 880 |
if v == "" { |
| 881 |
return nil |
| 882 |
} |
| 883 |
fieldValue.Set(reflect.New(field.FieldType.Elem())) |
| 884 |
} |
| 885 |
fieldValue.Elem().Set(reflect.ValueOf(t)) |
| 886 |
} else { |
| 887 |
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) |
| 888 |
} |
| 889 |
default: |
| 890 |
return fallbackSetter(ctx, value, v, field.Set) |
| 891 |
} |
| 892 |
return nil |
| 893 |
} |
| 894 |
default: |
| 895 |
if _, ok := fieldValue.Elem().Interface().(sql.Scanner); ok { |
| 896 |
// pointer scanner |
| 897 |
field.Set = func(ctx context.Context, value reflect.Value, v interface{}) (err error) { |
| 898 |
reflectV := reflect.ValueOf(v) |
| 899 |
if !reflectV.IsValid() { |
| 900 |
field.ReflectValueOf(ctx, value).Set(reflect.New(field.FieldType).Elem()) |
| 901 |
} else if reflectV.Kind() == reflect.Ptr && reflectV.IsNil() { |
| 902 |
return |
| 903 |
} else if reflectV.Type().AssignableTo(field.FieldType) { |
| 904 |
field.ReflectValueOf(ctx, value).Set(reflectV) |
| 905 |
} else if reflectV.Kind() == reflect.Ptr { |
| 906 |
return field.Set(ctx, value, reflectV.Elem().Interface()) |
| 907 |
} else { |
| 908 |
fieldValue := field.ReflectValueOf(ctx, value) |
| 909 |
if fieldValue.IsNil() { |
| 910 |
fieldValue.Set(reflect.New(field.FieldType.Elem())) |
| 911 |
} |
| 912 |
|
| 913 |
if valuer, ok := v.(driver.Valuer); ok { |
| 914 |
v, _ = valuer.Value() |
| 915 |
} |
| 916 |
|
| 917 |
err = fieldValue.Interface().(sql.Scanner).Scan(v) |
| 918 |
} |
| 919 |
return |
| 920 |
} |
| 921 |
} else if _, ok := fieldValue.Interface().(sql.Scanner); ok { |
| 922 |
// struct scanner |
| 923 |
field.Set = func(ctx context.Context, value reflect.Value, v interface{}) (err error) { |
| 924 |
reflectV := reflect.ValueOf(v) |
| 925 |
if !reflectV.IsValid() { |
| 926 |
field.ReflectValueOf(ctx, value).Set(reflect.New(field.FieldType).Elem()) |
| 927 |
} else if reflectV.Kind() == reflect.Ptr && reflectV.IsNil() { |
| 928 |
return |
| 929 |
} else if reflectV.Type().AssignableTo(field.FieldType) { |
| 930 |
field.ReflectValueOf(ctx, value).Set(reflectV) |
| 931 |
} else if reflectV.Kind() == reflect.Ptr { |
| 932 |
return field.Set(ctx, value, reflectV.Elem().Interface()) |
| 933 |
} else { |
| 934 |
if valuer, ok := v.(driver.Valuer); ok { |
| 935 |
v, _ = valuer.Value() |
| 936 |
} |
| 937 |
|
| 938 |
err = field.ReflectValueOf(ctx, value).Addr().Interface().(sql.Scanner).Scan(v) |
| 939 |
} |
| 940 |
return |
| 941 |
} |
| 942 |
} else { |
| 943 |
field.Set = func(ctx context.Context, value reflect.Value, v interface{}) (err error) { |
| 944 |
return fallbackSetter(ctx, value, v, field.Set) |
| 945 |
} |
| 946 |
} |
| 947 |
} |
| 948 |
} |
| 949 |
|
| 950 |
if field.Serializer != nil { |
| 951 |
var ( |
| 952 |
oldFieldSetter = field.Set |
| 953 |
sameElemType bool |
| 954 |
sameType = field.FieldType == reflect.ValueOf(field.Serializer).Type() |
| 955 |
) |
| 956 |
|
| 957 |
if reflect.ValueOf(field.Serializer).Kind() == reflect.Ptr { |
| 958 |
sameElemType = field.FieldType == reflect.ValueOf(field.Serializer).Type().Elem() |
| 959 |
} |
| 960 |
|
| 961 |
serializerValue := reflect.Indirect(reflect.ValueOf(field.Serializer)) |
| 962 |
serializerType := serializerValue.Type() |
| 963 |
field.Set = func(ctx context.Context, value reflect.Value, v interface{}) (err error) { |
| 964 |
if s, ok := v.(*serializer); ok { |
| 965 |
if s.fieldValue != nil { |
| 966 |
err = oldFieldSetter(ctx, value, s.fieldValue) |
| 967 |
} else if err = s.Serializer.Scan(ctx, field, value, s.value); err == nil { |
| 968 |
if sameElemType { |
| 969 |
field.ReflectValueOf(ctx, value).Set(reflect.ValueOf(s.Serializer).Elem()) |
| 970 |
} else if sameType { |
| 971 |
field.ReflectValueOf(ctx, value).Set(reflect.ValueOf(s.Serializer)) |
| 972 |
} |
| 973 |
si := reflect.New(serializerType) |
| 974 |
si.Elem().Set(serializerValue) |
| 975 |
s.Serializer = si.Interface().(SerializerInterface) |
| 976 |
} |
| 977 |
} else { |
| 978 |
err = oldFieldSetter(ctx, value, v) |
| 979 |
} |
| 980 |
return |
| 981 |
} |
| 982 |
} |
| 983 |
} |
| 984 |
|
| 985 |
func (field *Field) setupNewValuePool() { |
| 986 |
if field.Serializer != nil { |
| 987 |
serializerValue := reflect.Indirect(reflect.ValueOf(field.Serializer)) |
| 988 |
serializerType := serializerValue.Type() |
| 989 |
field.NewValuePool = &sync.Pool{ |
| 990 |
New: func() interface{} { |
| 991 |
si := reflect.New(serializerType) |
| 992 |
si.Elem().Set(serializerValue) |
| 993 |
return &serializer{ |
| 994 |
Field: field, |
| 995 |
Serializer: si.Interface().(SerializerInterface), |
| 996 |
} |
| 997 |
}, |
| 998 |
} |
| 999 |
} |
| 1000 |
|
| 1001 |
if field.NewValuePool == nil { |
| 1002 |
field.NewValuePool = poolInitializer(reflect.PointerTo(field.IndirectFieldType)) |
| 1003 |
} |
| 1004 |
} |