Skip to content

Commit 52b4744

Browse files
authored
optimize: performance optimization (#7526)
1 parent 9af6d51 commit 52b4744

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

schema/field.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,21 +448,30 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
448448
}
449449

450450
// create valuer, setter when parse struct
451-
func (field *Field) setupValuerAndSetter() {
451+
func (field *Field) setupValuerAndSetter(modelType reflect.Type) {
452452
// Setup NewValuePool
453453
field.setupNewValuePool()
454454

455455
// ValueOf returns field's value and if it is zero
456456
fieldIndex := field.StructField.Index[0]
457457
switch {
458458
case len(field.StructField.Index) == 1 && fieldIndex >= 0:
459-
field.ValueOf = func(ctx context.Context, value reflect.Value) (interface{}, bool) {
460-
fieldValue := reflect.Indirect(value).FieldByName(field.Name)
459+
field.ValueOf = func(ctx context.Context, v reflect.Value) (interface{}, bool) {
460+
v = reflect.Indirect(v)
461+
if v.Type() != modelType {
462+
fieldValue := v.FieldByName(field.Name)
463+
return fieldValue.Interface(), fieldValue.IsZero()
464+
}
465+
fieldValue := v.Field(fieldIndex)
461466
return fieldValue.Interface(), fieldValue.IsZero()
462467
}
463468
default:
464469
field.ValueOf = func(ctx context.Context, v reflect.Value) (interface{}, bool) {
465470
v = reflect.Indirect(v)
471+
if v.Type() != modelType {
472+
fieldValue := v.FieldByName(field.Name)
473+
return fieldValue.Interface(), fieldValue.IsZero()
474+
}
466475
for _, fieldIdx := range field.StructField.Index {
467476
if fieldIdx >= 0 {
468477
v = v.Field(fieldIdx)

schema/schema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam
248248
schema.FieldsByBindName[bindName] = field
249249
}
250250

251-
field.setupValuerAndSetter()
251+
field.setupValuerAndSetter(modelType)
252252
}
253253

254254
prioritizedPrimaryField := schema.LookUpField("id")

0 commit comments

Comments
 (0)