Make sure we pass around the addr of slices.

- Use reflect.Indirect instead of .Elem()
This commit is contained in:
Aaron L 2016-08-29 22:48:14 -07:00
parent f2b8f39d47
commit 32b5952c96
2 changed files with 13 additions and 8 deletions

View file

@ -176,7 +176,7 @@ func loadRelationships(exec Executor, toLoad []string, obj interface{}, singular
if loadedObject.IsNil() { if loadedObject.IsNil() {
return nil return nil
} }
loadedObject = loadedObject.Elem() loadedObject = reflect.Indirect(loadedObject)
// If it's singular we can just immediately call without looping // If it's singular we can just immediately call without looping
if singular { if singular {
@ -205,8 +205,11 @@ func loadRelationshipsRecurse(exec Executor, current string, toLoad []string, si
if !r.IsValid() || r.IsNil() { if !r.IsValid() || r.IsNil() {
return errors.Errorf("could not traverse into loaded %s relationship to load more things", current) return errors.Errorf("could not traverse into loaded %s relationship to load more things", current)
} }
newObj := r.Elem().FieldByName(current) newObj := reflect.Indirect(r).FieldByName(current)
singular = newObj.Elem().Kind() == reflect.Struct singular = reflect.Indirect(newObj).Kind() == reflect.Struct
if !singular {
newObj = newObj.Addr()
}
return loadRelationships(exec, toLoad, newObj.Interface(), singular) return loadRelationships(exec, toLoad, newObj.Interface(), singular)
} }

View file

@ -182,7 +182,7 @@ type testNestedSlice struct {
R *testNestedRSlice R *testNestedRSlice
} }
type testNestedRSlice struct { type testNestedRSlice struct {
ToEagerLoad *[]*testNestedSlice ToEagerLoad []*testNestedSlice
} }
func (r *testRStruct) LoadTestOne(exec Executor, singular bool, obj interface{}) error { 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) { switch x := obj.(type) {
case *testNestedSlice: case *testNestedSlice:
newSlice := []*testNestedSlice{&testNestedSlice{ID: 5}} x.R = &testNestedRSlice{
x.R = &testNestedRSlice{&newSlice} []*testNestedSlice{&testNestedSlice{ID: 5}},
}
case *[]*testNestedSlice: case *[]*testNestedSlice:
newSlice := []*testNestedSlice{&testNestedSlice{ID: 5}}
for _, r := range *x { for _, r := range *x {
r.R = &testNestedRSlice{&newSlice} r.R = &testNestedRSlice{
[]*testNestedSlice{&testNestedSlice{ID: 5}},
}
} }
} }
loadFunctionNestedCalled++ loadFunctionNestedCalled++