Fix eager loading, off-by-one pointer

This commit is contained in:
Aaron L 2016-08-23 22:07:51 -07:00
parent 63fae21c51
commit d130354890
6 changed files with 10 additions and 8 deletions

View file

@ -112,7 +112,7 @@ func (q *Query) Bind(obj interface{}) error {
func (q *Query) loadRelationships(obj interface{}, singular bool) error {
typ := reflect.TypeOf(obj).Elem()
if !singular {
typ = typ.Elem()
typ = typ.Elem().Elem()
}
rel, found := typ.FieldByName("Loaded")

View file

@ -122,7 +122,7 @@ func TestLoadRelationshipsSlice(t *testing.T) {
}{}
q := Query{load: []string{"TestOne"}, executor: nil}
if err := q.loadRelationships(testSlice, false); err != nil {
if err := q.loadRelationships(&testSlice, false); err != nil {
t.Error(err)
}
@ -135,13 +135,13 @@ func TestLoadRelationshipsSingular(t *testing.T) {
// t.Parallel() Function uses globals
loadFunctionCalled = false
testSingular := &struct {
testSingular := struct {
ID int
Loaded *testLoadedStruct
}{}
q := Query{load: []string{"TestOne"}, executor: nil}
if err := q.loadRelationships(testSingular, true); err != nil {
if err := q.loadRelationships(&testSingular, true); err != nil {
t.Error(err)
}

View file

@ -11,7 +11,7 @@ func (r *{{.LocalTable.NameGo}}Loaded) Load{{.Function.Name}}(e boil.Executor, s
if singular {
object = {{$arg}}.(*{{.LocalTable.NameGo}})
} else {
slice = {{$arg}}.({{$slice}})
slice = *{{$arg}}.(*{{$slice}})
count = len(slice)
}

View file

@ -18,7 +18,7 @@ func (r *{{$rel.LocalTable.NameGo}}Loaded) Load{{$rel.Function.Name}}(e boil.Exe
if singular {
object = {{$arg}}.(*{{$rel.LocalTable.NameGo}})
} else {
slice = {{$arg}}.({{$slice}})
slice = *{{$arg}}.(*{{$slice}})
count = len(slice)
}

View file

@ -74,7 +74,8 @@ func test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) {
t.Error("expected to find c")
}
if err = a.Loaded.Load{{$rel.Function.Name}}(tx, false, {{$rel.LocalTable.NameGo}}Slice{&a}); err != nil {
slice := {{$rel.LocalTable.NameGo}}Slice{&a}
if err = a.Loaded.Load{{$rel.Function.Name}}(tx, false, &slice); err != nil {
t.Fatal(err)
}
if got := len(a.Loaded.{{$rel.Function.Name}}); got != 2 {

View file

@ -41,7 +41,8 @@ func test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}(
t.Errorf("want: %v, got %v", foreign.{{.Function.ForeignAssignment}}, check.{{.Function.ForeignAssignment}})
}
if err = local.Loaded.Load{{.Function.Name}}(tx, false, {{.LocalTable.NameGo}}Slice{&local}); err != nil {
slice := {{.LocalTable.NameGo}}Slice{&local}
if err = local.Loaded.Load{{.Function.Name}}(tx, false, &slice); err != nil {
t.Fatal(err)
}
if local.Loaded.{{.Function.Name}} == nil {