Skip to content

Commit

Permalink
fix: alloc when mounting (#891)
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco authored Sep 10, 2023
1 parent 806e632 commit f2256f1
Showing 4 changed files with 29 additions and 27 deletions.
24 changes: 24 additions & 0 deletions internal/util.go
Original file line number Diff line number Diff line change
@@ -55,3 +55,27 @@ func Unwrap(err error) error {
}
return u.Unwrap()
}

func FieldByIndexAlloc(v reflect.Value, index []int) reflect.Value {
if len(index) == 1 {
return v.Field(index[0])
}

for i, idx := range index {
if i > 0 {
v = indirectNil(v)
}
v = v.Field(idx)
}
return v
}

func indirectNil(v reflect.Value) reflect.Value {
if v.Kind() == reflect.Ptr {
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
v = v.Elem()
}
return v
}
3 changes: 2 additions & 1 deletion model_table_struct.go
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import (
"strings"
"time"

"github.com/uptrace/bun/internal"
"github.com/uptrace/bun/schema"
)

@@ -234,7 +235,7 @@ func (m *structTableModel) parentIndex() []int {
}

func (m *structTableModel) mount(host reflect.Value) {
m.strct = host.FieldByIndex(m.rel.Field.Index)
m.strct = internal.FieldByIndexAlloc(host, m.rel.Field.Index)
m.structInited = false
}

5 changes: 3 additions & 2 deletions schema/field.go
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import (
"reflect"

"github.com/uptrace/bun/dialect"
"github.com/uptrace/bun/internal"
"github.com/uptrace/bun/internal/tagparser"
)

@@ -50,7 +51,7 @@ func (f *Field) Clone() *Field {
}

func (f *Field) Value(strct reflect.Value) reflect.Value {
return fieldByIndexAlloc(strct, f.Index)
return internal.FieldByIndexAlloc(strct, f.Index)
}

func (f *Field) HasNilValue(v reflect.Value) bool {
@@ -117,7 +118,7 @@ func (f *Field) ScanValue(strct reflect.Value, src interface{}) error {
return nil
}

fv := fieldByIndexAlloc(strct, f.Index)
fv := internal.FieldByIndexAlloc(strct, f.Index)
return f.ScanWithCheck(fv, src)
}

24 changes: 0 additions & 24 deletions schema/reflect.go
Original file line number Diff line number Diff line change
@@ -46,27 +46,3 @@ func fieldByIndex(v reflect.Value, index []int) (_ reflect.Value, ok bool) {
}
return v, true
}

func fieldByIndexAlloc(v reflect.Value, index []int) reflect.Value {
if len(index) == 1 {
return v.Field(index[0])
}

for i, idx := range index {
if i > 0 {
v = indirectNil(v)
}
v = v.Field(idx)
}
return v
}

func indirectNil(v reflect.Value) reflect.Value {
if v.Kind() == reflect.Ptr {
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
v = v.Elem()
}
return v
}

0 comments on commit f2256f1

Please sign in to comment.