Skip to content

Commit

Permalink
Hashes: support reading and writing interface{} values
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Hoisie committed Jun 3, 2010
1 parent cae5ef7 commit 3bb600e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,10 @@ func valueToString(v reflect.Value) (string, os.Error) {
}

switch v := v.(type) {
case *reflect.PtrValue:
return valueToString(reflect.Indirect(v))
case *reflect.InterfaceValue:
return valueToString(v.Elem())
case *reflect.BoolValue:
x := v.Get()
if x {
Expand Down Expand Up @@ -1073,6 +1077,10 @@ func (client *Client) Hvals(key string) ([][]byte, os.Error) {
func writeTo(data []byte, val reflect.Value) os.Error {
s := string(data)
switch v := val.(type) {
// if we're writing to an interace value, just set the byte data
// TODO: should we support writing to a pointer?
case *reflect.InterfaceValue:
v.Set(reflect.NewValue(data))
case *reflect.BoolValue:
b, err := strconv.Atob(s)
if err != nil {
Expand Down
16 changes: 16 additions & 0 deletions redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,28 @@ func TestHash(t *testing.T) {
err = client.Hgetall("hdne", &test5)
if err == nil {
t.Fatal("should be an error")
}

test6 := make(map[string]interface{})
for _, v := range keys {
test6[v] = []byte(strings.Repeat(v, 5))
}
client.Hmset("h4", test6)

//test Hgetall
test7 := make(map[string]interface{})
err = client.Hgetall("h4", &test7)
if err != nil {
t.Fatal("verifyHash Hgetall failed", err.String())
}
if !reflect.DeepEqual(test6, test7) {
t.Fatal("verifyHash Hgetall failed")
}

client.Del("h")
client.Del("h2")
client.Del("h3")
client.Del("h4")
}

func BenchmarkMultipleGet(b *testing.B) {
Expand Down

0 comments on commit 3bb600e

Please sign in to comment.