Skip to content

Commit

Permalink
Fix bug where helper types are not properly initialized by using Set(…
Browse files Browse the repository at this point in the history
…). The fix requires that Key() is used before Set(). Will properly throw a warning if Exec() on helper types does not have a key set
  • Loading branch information
zegl committed Nov 23, 2016
1 parent 152c6ac commit 159bd92
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions auto_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (c Command) Set(val interface{}) Command {
riakContext, op, err := encodeInterface(val, requestData{
bucket: c.bucket,
bucketType: c.bucketType,
key: c.key,
})

if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func (c *Counter) Exec(client *Session) error {
return errors.New("Unknown path to counter. Retrieve counter with GetMap before updating the counter")
}

// Validate c.key
if c.key.bucket == "" || c.key.bucketType == "" || c.key.key == "" {
return errors.New("Invalid key in Counter Exec()")
}

op := &riak.MapOperation{}
outerOp := op

Expand Down
5 changes: 5 additions & 0 deletions set.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ func (s *Set) Exec(client *Session) error {
return errors.New("Unknown path to Set. Retrieve Set with GetMap before updating the Set")
}

// Validate s.key
if s.key.bucket == "" || s.key.bucketType == "" || s.key.key == "" {
return errors.New("Invalid key in Set Exec()")
}

op := &riak.MapOperation{}
outerOp := op

Expand Down
72 changes: 72 additions & 0 deletions set_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package goriak

import (
"math/rand"
"reflect"
"sort"
"testing"
Expand Down Expand Up @@ -346,3 +347,74 @@ func TestSetInitializeSet(t *testing.T) {
t.Error("testVal2: Foos is not initialized")
}
}

var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

func randomKey() string {
n := 10
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}

func TestSetUnitialized(t *testing.T) {
type testType struct {
Foos *Set
}

val := testType{}
key := randomKey()

_, err := bucket().Key(key).Set(&val).Run(con())

if err != nil {
t.Error(err)
}

err = val.Foos.AddString("wohoohooo").Exec(con())

if err != nil {
t.Error(err)
}

// Fetch
var val2 testType
_, err = bucket().Get(key, &val2).Run(con())

if err != nil {
t.Error(err)
}

if !val2.Foos.HasString("wohoohooo") {
t.Error("Not in set after save")
}
}

func TestSetUnitializedOtherOrder(t *testing.T) {
type testType struct {
Foos *Set
}

val := testType{}
key := randomKey()

// The difference is here
_, err := bucket().Set(&val).Key(key).Run(con())

if err != nil {
t.Error(err)
}

err = val.Foos.AddString("wohoohooo").Exec(con())

if err == nil {
t.Error("Got no error after AddString()")
return
}

if err.Error() != "Invalid key in Set Exec()" {
t.Error("Unexpected error:", err.Error())
}
}

0 comments on commit 159bd92

Please sign in to comment.