Make sure we pass around the addr of slices.
- Use reflect.Indirect instead of .Elem()
This commit is contained in:
parent
f2b8f39d47
commit
32b5952c96
2 changed files with 13 additions and 8 deletions
|
@ -176,7 +176,7 @@ func loadRelationships(exec Executor, toLoad []string, obj interface{}, singular
|
|||
if loadedObject.IsNil() {
|
||||
return nil
|
||||
}
|
||||
loadedObject = loadedObject.Elem()
|
||||
loadedObject = reflect.Indirect(loadedObject)
|
||||
|
||||
// If it's singular we can just immediately call without looping
|
||||
if singular {
|
||||
|
@ -205,8 +205,11 @@ func loadRelationshipsRecurse(exec Executor, current string, toLoad []string, si
|
|||
if !r.IsValid() || r.IsNil() {
|
||||
return errors.Errorf("could not traverse into loaded %s relationship to load more things", current)
|
||||
}
|
||||
newObj := r.Elem().FieldByName(current)
|
||||
singular = newObj.Elem().Kind() == reflect.Struct
|
||||
newObj := reflect.Indirect(r).FieldByName(current)
|
||||
singular = reflect.Indirect(newObj).Kind() == reflect.Struct
|
||||
if !singular {
|
||||
newObj = newObj.Addr()
|
||||
}
|
||||
return loadRelationships(exec, toLoad, newObj.Interface(), singular)
|
||||
}
|
||||
|
||||
|
|
|
@ -182,7 +182,7 @@ type testNestedSlice struct {
|
|||
R *testNestedRSlice
|
||||
}
|
||||
type testNestedRSlice struct {
|
||||
ToEagerLoad *[]*testNestedSlice
|
||||
ToEagerLoad []*testNestedSlice
|
||||
}
|
||||
|
||||
func (r *testRStruct) LoadTestOne(exec Executor, singular bool, obj interface{}) error {
|
||||
|
@ -211,12 +211,14 @@ func (r *testNestedRSlice) LoadToEagerLoad(exec Executor, singular bool, obj int
|
|||
|
||||
switch x := obj.(type) {
|
||||
case *testNestedSlice:
|
||||
newSlice := []*testNestedSlice{&testNestedSlice{ID: 5}}
|
||||
x.R = &testNestedRSlice{&newSlice}
|
||||
x.R = &testNestedRSlice{
|
||||
[]*testNestedSlice{&testNestedSlice{ID: 5}},
|
||||
}
|
||||
case *[]*testNestedSlice:
|
||||
newSlice := []*testNestedSlice{&testNestedSlice{ID: 5}}
|
||||
for _, r := range *x {
|
||||
r.R = &testNestedRSlice{&newSlice}
|
||||
r.R = &testNestedRSlice{
|
||||
[]*testNestedSlice{&testNestedSlice{ID: 5}},
|
||||
}
|
||||
}
|
||||
}
|
||||
loadFunctionNestedCalled++
|
||||
|
|
Loading…
Reference in a new issue