Change HGetAll and HMSet to return/accept map[string]string.

This commit is contained in:
Vladimir Mihailenco 2016-05-02 14:50:59 +03:00
parent 033a4de2fb
commit 9cbb0c42df
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 return cmd
} }
func (c *commandable) HGetAll(key string) *StringSliceCmd { func (c *commandable) HGetAll(key string) *StringStringMapCmd {
cmd := NewStringSliceCmd("HGETALL", key)
c.Process(cmd)
return cmd
}
func (c *commandable) HGetAllMap(key string) *StringStringMapCmd {
cmd := NewStringStringMapCmd("HGETALL", key) cmd := NewStringStringMapCmd("HGETALL", key)
c.Process(cmd) c.Process(cmd)
return cmd return cmd
@ -694,14 +688,15 @@ func (c *commandable) HMGet(key string, fields ...string) *SliceCmd {
return cmd return cmd
} }
func (c *commandable) HMSet(key, field, value string, pairs ...string) *StatusCmd { func (c *commandable) HMSet(key string, fields map[string]string) *StatusCmd {
args := make([]interface{}, 4+len(pairs)) args := make([]interface{}, 2+len(fields)*2)
args[0] = "HMSET" args[0] = "HMSET"
args[1] = key args[1] = key
args[2] = field i := 2
args[3] = value for k, v := range fields {
for i, pair := range pairs { args[i] = k
args[4+i] = pair args[i+1] = v
i += 2
} }
cmd := NewStatusCmd(args...) cmd := NewStatusCmd(args...)
c.Process(cmd) c.Process(cmd)

View File

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