From 9cbb0c42df59407f84e40494f26cc22596e3ca3d Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Mon, 2 May 2016 14:50:59 +0300 Subject: [PATCH] Change HGetAll and HMSet to return/accept map[string]string. --- commands.go | 21 +++++++---------- commands_test.go | 60 +++++++++++++++++++++--------------------------- 2 files changed, 34 insertions(+), 47 deletions(-) diff --git a/commands.go b/commands.go index 48a59f6..e6c7e8a 100644 --- a/commands.go +++ b/commands.go @@ -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) diff --git a/commands_test.go b/commands_test.go index 8f27974..d4f25ff 100644 --- a/commands_test.go +++ b/commands_test.go @@ -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() {