Fix a len(0) check that wasn't occurring

This commit is contained in:
Aaron L 2016-11-15 20:34:36 -08:00
parent ac48562dc2
commit a8330b18ad
2 changed files with 78 additions and 0 deletions

View file

@ -164,6 +164,9 @@ func (l loadRelationshipState) callLoadFunction(depth int, loadingFrom reflect.V
// Get a loader instance from anything we have, *struct, or *[]*struct
val := reflect.Indirect(loadingFrom)
if bkind == kindPtrSliceStruct {
if val.Len() == 0 {
return nil
}
val = reflect.Indirect(val.Index(0))
}

View file

@ -23,6 +23,8 @@ type testEager struct {
type testEagerR struct {
ChildOne *testEagerChild
ChildMany []*testEagerChild
ZeroOne *testEagerZero
ZeroMany []*testEagerZero
}
type testEagerL struct {
}
@ -49,6 +51,18 @@ type testEagerNestedR struct {
type testEagerNestedL struct {
}
type testEagerZero struct {
ID int
R *testEagerZeroR
L testEagerZeroL
}
type testEagerZeroR struct {
NestedOne *testEagerNested
NestedMany []*testEagerNested
}
type testEagerZeroL struct {
}
func (testEagerL) LoadChildOne(_ boil.Executor, singular bool, obj interface{}) error {
var toSetOn []*testEager
if singular {
@ -135,6 +149,48 @@ func (testEagerChildL) LoadNestedMany(_ boil.Executor, singular bool, obj interf
return nil
}
func (testEagerL) LoadZeroOne(_ boil.Executor, singular bool, obj interface{}) error {
var toSetOn []*testEager
if singular {
toSetOn = []*testEager{obj.(*testEager)}
} else {
toSetOn = *obj.(*[]*testEager)
}
for _, o := range toSetOn {
if o.R == nil {
o.R = &testEagerR{}
}
}
return nil
}
func (testEagerL) LoadZeroMany(_ boil.Executor, singular bool, obj interface{}) error {
var toSetOn []*testEager
if singular {
toSetOn = []*testEager{obj.(*testEager)}
} else {
toSetOn = *obj.(*[]*testEager)
}
for _, o := range toSetOn {
if o.R == nil {
o.R = &testEagerR{}
}
o.R.ZeroMany = []*testEagerZero{}
}
return nil
}
func (testEagerZeroL) LoadNestedOne(_ boil.Executor, singular bool, obj interface{}) error {
return nil
}
func (testEagerZeroL) LoadNestedMany(_ boil.Executor, singular bool, obj interface{}) error {
return nil
}
func TestEagerLoadFromOne(t *testing.T) {
testEagerCounters.ChildOne = 0
testEagerCounters.ChildMany = 0
@ -224,6 +280,25 @@ func TestEagerLoadFromMany(t *testing.T) {
checkNestedMany(slice[1].R.ChildMany[1].R.NestedMany)
}
func TestEagerLoadZeroParents(t *testing.T) {
t.Parallel()
obj := &testEager{}
toLoad := []string{"ZeroMany.NestedMany", "ZeroOne.NestedOne", "ZeroMany.NestedMany", "ZeroOne.NestedOne"}
err := eagerLoad(nil, toLoad, obj, kindStruct)
if err != nil {
t.Fatal(err)
}
if len(obj.R.ZeroMany) != 0 {
t.Error("should have loaded nothing")
}
if obj.R.ZeroOne != nil {
t.Error("should have loaded nothing")
}
}
func checkChildOne(c *testEagerChild) {
if c == nil {
panic("c was nil")