diff --git a/boom/boom.go b/boom/boom.go index 4b08c34..099543e 100644 --- a/boom/boom.go +++ b/boom/boom.go @@ -135,7 +135,9 @@ func (bm *Boom) setStructKey(src interface{}, key datastore.Key) error { } else { vfType := vf.Type() if vfType.ConvertibleTo(typeOfKey) { - vf.Set(reflect.ValueOf(key.ParentKey()).Convert(vfType)) + if key.ParentKey() != nil { + vf.Set(reflect.ValueOf(key.ParentKey()).Convert(vfType)) + } parentSet = true } } @@ -265,7 +267,7 @@ func (bm *Boom) KeyError(src interface{}) (datastore.Key, error) { } } else { vfType := vf.Type() - if vfType.ConvertibleTo(typeOfKey) { + if !vf.IsNil() && vfType.ConvertibleTo(typeOfKey) { if parent != nil { return nil, fmt.Errorf("boom: Only one field may be marked parent") } diff --git a/boom/boom_test.go b/boom/boom_test.go index 01c590d..717ab14 100644 --- a/boom/boom_test.go +++ b/boom/boom_test.go @@ -645,6 +645,34 @@ func TestBoom_TagParent(t *testing.T) { } } +func TestBoom_TagParentWithNilParent(t *testing.T) { + ctx, client, cleanUp := testutils.SetupCloudDatastore(t) + defer cleanUp() + + ctx = context.WithValue(ctx, contextClient{}, client) + + bm := FromClient(ctx, client) + + type Data struct { + ParentKey datastore.Key `datastore:"-" boom:"parent"` + ID int64 `datastore:"-" boom:"id"` + } + + key, err := bm.Put(&Data{ParentKey: nil, ID: 1}) + if err != nil { + t.Fatal(err) + } + + if v := key.ParentKey(); v != nil { + t.Errorf("unexpected: %v", v) + } + + err = bm.Get(&Data{ID: 1}) + if err != nil { + t.Fatal(err) + } +} + func TestBoom_TagKind(t *testing.T) { ctx, client, cleanUp := testutils.SetupCloudDatastore(t) defer cleanUp()