Merge pull request #315 from go-redis/feature/hgetall-hmset

Change HGetAll and HMSet to return/accept map[string]string.
This commit is contained in:
Vladimir Mihailenco 2016-05-03 10:14:06 +03:00
commit 3972f28066
2 changed files with 34 additions and 47 deletions

View File

@ -646,13 +646,7 @@ func (c *commandable) HGet(key, field string) *StringCmd {
return cmd
}
func (c *commandable) HGetAll(key string) *StringSliceCmd {
cmd := NewStringSliceCmd("HGETALL", key)
c.Process(cmd)
return cmd
}
func (c *commandable) HGetAllMap(key string) *StringStringMapCmd {
func (c *commandable) HGetAll(key string) *StringStringMapCmd {
cmd := NewStringStringMapCmd("HGETALL", key)
c.Process(cmd)
return cmd
@ -694,14 +688,15 @@ func (c *commandable) HMGet(key string, fields ...string) *SliceCmd {
return cmd
}
func (c *commandable) HMSet(key, field, value string, pairs ...string) *StatusCmd {
args := make([]interface{}, 4+len(pairs))
func (c *commandable) HMSet(key string, fields map[string]string) *StatusCmd {
args := make([]interface{}, 2+len(fields)*2)
args[0] = "HMSET"
args[1] = key
args[2] = field
args[3] = value
for i, pair := range pairs {
args[4+i] = pair
i := 2
for k, v := range fields {
args[i] = k
args[i+1] = v
i += 2
}
cmd := NewStatusCmd(args...)
c.Process(cmd)

View File

@ -1085,25 +1085,14 @@ var _ = Describe("Commands", func() {
})
It("should HGetAll", func() {
hSet := client.HSet("hash", "key1", "hello1")
Expect(hSet.Err()).NotTo(HaveOccurred())
hSet = client.HSet("hash", "key2", "hello2")
Expect(hSet.Err()).NotTo(HaveOccurred())
err := client.HSet("hash", "key1", "hello1").Err()
Expect(err).NotTo(HaveOccurred())
err = client.HSet("hash", "key2", "hello2").Err()
Expect(err).NotTo(HaveOccurred())
hGetAll := client.HGetAll("hash")
Expect(hGetAll.Err()).NotTo(HaveOccurred())
Expect(hGetAll.Val()).To(Equal([]string{"key1", "hello1", "key2", "hello2"}))
})
It("should HGetAllMap", func() {
hSet := client.HSet("hash", "key1", "hello1")
Expect(hSet.Err()).NotTo(HaveOccurred())
hSet = client.HSet("hash", "key2", "hello2")
Expect(hSet.Err()).NotTo(HaveOccurred())
hGetAll := client.HGetAllMap("hash")
Expect(hGetAll.Err()).NotTo(HaveOccurred())
Expect(hGetAll.Val()).To(Equal(map[string]string{"key1": "hello1", "key2": "hello2"}))
m, err := client.HGetAll("hash").Result()
Expect(err).NotTo(HaveOccurred())
Expect(m).To(Equal(map[string]string{"key1": "hello1", "key2": "hello2"}))
})
It("should HIncrBy", func() {
@ -1168,28 +1157,31 @@ var _ = Describe("Commands", func() {
})
It("should HMGet", func() {
hSet := client.HSet("hash", "key1", "hello1")
Expect(hSet.Err()).NotTo(HaveOccurred())
hSet = client.HSet("hash", "key2", "hello2")
Expect(hSet.Err()).NotTo(HaveOccurred())
err := client.HSet("hash", "key1", "hello1").Err()
Expect(err).NotTo(HaveOccurred())
err = client.HSet("hash", "key2", "hello2").Err()
Expect(err).NotTo(HaveOccurred())
hMGet := client.HMGet("hash", "key1", "key2", "_")
Expect(hMGet.Err()).NotTo(HaveOccurred())
Expect(hMGet.Val()).To(Equal([]interface{}{"hello1", "hello2", nil}))
vals, err := client.HMGet("hash", "key1", "key2", "_").Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]interface{}{"hello1", "hello2", nil}))
})
It("should HMSet", func() {
hMSet := client.HMSet("hash", "key1", "hello1", "key2", "hello2")
Expect(hMSet.Err()).NotTo(HaveOccurred())
Expect(hMSet.Val()).To(Equal("OK"))
ok, err := client.HMSet("hash", map[string]string{
"key1": "hello1",
"key2": "hello2",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(ok).To(Equal("OK"))
hGet := client.HGet("hash", "key1")
Expect(hGet.Err()).NotTo(HaveOccurred())
Expect(hGet.Val()).To(Equal("hello1"))
v, err := client.HGet("hash", "key1").Result()
Expect(err).NotTo(HaveOccurred())
Expect(v).To(Equal("hello1"))
hGet = client.HGet("hash", "key2")
Expect(hGet.Err()).NotTo(HaveOccurred())
Expect(hGet.Val()).To(Equal("hello2"))
v, err = client.HGet("hash", "key2").Result()
Expect(err).NotTo(HaveOccurred())
Expect(v).To(Equal("hello2"))
})
It("should HSet", func() {