forked from mirror/gorm
Add fast path for ValueOf, ReflectValueOf
This commit is contained in:
parent
4d14ac39ff
commit
530b0a12b4
|
@ -465,6 +465,14 @@ func (field *Field) setupValuerAndSetter() {
|
|||
}
|
||||
|
||||
// ValueOf returns field's value and if it is zero
|
||||
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 {
|
||||
|
@ -484,6 +492,7 @@ func (field *Field) setupValuerAndSetter() {
|
|||
fv, zero := v.Interface(), v.IsZero()
|
||||
return fv, zero
|
||||
}
|
||||
}
|
||||
|
||||
if field.Serializer != nil {
|
||||
oldValuerOf := field.ValueOf
|
||||
|
@ -509,6 +518,12 @@ func (field *Field) setupValuerAndSetter() {
|
|||
}
|
||||
|
||||
// ReflectValueOf returns field's reflect value
|
||||
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 {
|
||||
|
@ -528,6 +543,7 @@ func (field *Field) setupValuerAndSetter() {
|
|||
}
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
fallbackSetter := func(ctx context.Context, value reflect.Value, v interface{}, setter func(context.Context, reflect.Value, interface{}) error) (err error) {
|
||||
if v == nil {
|
||||
|
|
|
@ -3,6 +3,7 @@ module gorm.io/gorm/tests
|
|||
go 1.14
|
||||
|
||||
require (
|
||||
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
|
||||
github.com/google/uuid v1.3.0
|
||||
github.com/jackc/pgx/v4 v4.15.0 // indirect
|
||||
github.com/jinzhu/now v1.1.4
|
||||
|
|
Loading…
Reference in New Issue