Skip to content

Commit

Permalink
fix(boom): fix nil parent key handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vvakame committed Nov 22, 2017
1 parent ec6791b commit 7dc317b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
6 changes: 4 additions & 2 deletions boom/boom.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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")
}
Expand Down
28 changes: 28 additions & 0 deletions boom/boom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 7dc317b

Please sign in to comment.