Skip to content

Commit

Permalink
Change SetJSON to correctly handle pointer args (#63)
Browse files Browse the repository at this point in the history
This fixes an issue with not grabbed `goriakindex` tags when we pass the pointer to SetJSON method.

Before this commit the behavior was:
- call SetJSON with the pointer to a struct which has `goriakindex` tag.
- under the hood, the method determinates kind of passed value as `ptr` and doesn't try to find the field with tag.
- data are silently written into a bucket, without a secondary key.

In this commit added an extra check on pointer and data passed by pointer handled in the same way as data passed by value.
Test case included into the commit.
  • Loading branch information
Shkurpylo authored and zegl committed Dec 18, 2018
1 parent ccbaaca commit cb710d4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 27 deletions.
11 changes: 10 additions & 1 deletion json.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,17 @@ func (c *Command) SetJSON(value interface{}) *SetRawCommand {
Value: by,
}

refType := reflect.TypeOf(value)
refValue := reflect.ValueOf(value)
// Handling case with the pointer as an argument
if refValue.Kind() == reflect.Ptr {
refValue = refValue.Elem()
}

refType := reflect.TypeOf(value)
// Avoiding panic when zero value passed
if refValue.IsValid() {
refType = refValue.Type()
}

// Indexes from struct value
if refType.Kind() == reflect.Struct {
Expand Down
76 changes: 50 additions & 26 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,64 @@ func TestSetJSONWithIndexes(t *testing.T) {
Name string
}

val := testType{
Username: "zegl",
Name: "Gustav",
}

setresult, err := Bucket("json", "default").SetJSON(val).Run(con())
tt := []struct {
name string
indexKey string
getItem func() interface{}
}{
{
name: "SetJSON by value case",
indexKey: "zegl",
getItem: func() interface{} {
return testType{
Username: "zegl",
Name: "Gustav",
}
},
},
{
name: "SetJSON by pointer case",
indexKey: "shkurpylo",
getItem: func() interface{} {
return &testType{
Username: "shkurpylo",
Name: "Anatolii",
}
},
},
}

for _, tc := range tt {

setresult, err := Bucket("json", "default").SetJSON(tc.getItem()).Run(con())

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

foundCount := 0
foundCorrent := false
foundCount := 0
foundCorrent := false

cb := func(key SecondaryIndexQueryResult) {
if !key.IsComplete {
foundCount++
}
cb := func(key SecondaryIndexQueryResult) {
if !key.IsComplete {
foundCount++
}

if key.Key == setresult.Key {
foundCorrent = true
if key.Key == setresult.Key {
foundCorrent = true
}
}

}

Bucket("json", "default").KeysInIndex("username_bin", "zegl", cb).Run(con())
Bucket("json", "default").KeysInIndex("username_bin", tc.indexKey, cb).Run(con())

if foundCount != 1 {
t.Error("Expected to find 1 item, found ", foundCount)
}
if foundCount != 1 {
t.Errorf("%s: Expected to find 1 item, found %d\n", tc.name, foundCount)
}

if !foundCorrent {
t.Error("Did not find the correct item")
if !foundCorrent {
t.Errorf("%s: Did not find the correct item\n", tc.name)
}
}
}

Expand Down

0 comments on commit cb710d4

Please sign in to comment.