Skip to content

Commit 9cbb0c4

Browse files
committed
Change HGetAll and HMSet to return/accept map[string]string.
1 parent 033a4de commit 9cbb0c4

File tree

2 files changed

+34
-47
lines changed

2 files changed

+34
-47
lines changed

commands.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -646,13 +646,7 @@ func (c *commandable) HGet(key, field string) *StringCmd {
646646
return cmd
647647
}
648648

649-
func (c *commandable) HGetAll(key string) *StringSliceCmd {
650-
cmd := NewStringSliceCmd("HGETALL", key)
651-
c.Process(cmd)
652-
return cmd
653-
}
654-
655-
func (c *commandable) HGetAllMap(key string) *StringStringMapCmd {
649+
func (c *commandable) HGetAll(key string) *StringStringMapCmd {
656650
cmd := NewStringStringMapCmd("HGETALL", key)
657651
c.Process(cmd)
658652
return cmd
@@ -694,14 +688,15 @@ func (c *commandable) HMGet(key string, fields ...string) *SliceCmd {
694688
return cmd
695689
}
696690

697-
func (c *commandable) HMSet(key, field, value string, pairs ...string) *StatusCmd {
698-
args := make([]interface{}, 4+len(pairs))
691+
func (c *commandable) HMSet(key string, fields map[string]string) *StatusCmd {
692+
args := make([]interface{}, 2+len(fields)*2)
699693
args[0] = "HMSET"
700694
args[1] = key
701-
args[2] = field
702-
args[3] = value
703-
for i, pair := range pairs {
704-
args[4+i] = pair
695+
i := 2
696+
for k, v := range fields {
697+
args[i] = k
698+
args[i+1] = v
699+
i += 2
705700
}
706701
cmd := NewStatusCmd(args...)
707702
c.Process(cmd)

commands_test.go

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,25 +1085,14 @@ var _ = Describe("Commands", func() {
10851085
})
10861086

10871087
It("should HGetAll", func() {
1088-
hSet := client.HSet("hash", "key1", "hello1")
1089-
Expect(hSet.Err()).NotTo(HaveOccurred())
1090-
hSet = client.HSet("hash", "key2", "hello2")
1091-
Expect(hSet.Err()).NotTo(HaveOccurred())
1092-
1093-
hGetAll := client.HGetAll("hash")
1094-
Expect(hGetAll.Err()).NotTo(HaveOccurred())
1095-
Expect(hGetAll.Val()).To(Equal([]string{"key1", "hello1", "key2", "hello2"}))
1096-
})
1097-
1098-
It("should HGetAllMap", func() {
1099-
hSet := client.HSet("hash", "key1", "hello1")
1100-
Expect(hSet.Err()).NotTo(HaveOccurred())
1101-
hSet = client.HSet("hash", "key2", "hello2")
1102-
Expect(hSet.Err()).NotTo(HaveOccurred())
1088+
err := client.HSet("hash", "key1", "hello1").Err()
1089+
Expect(err).NotTo(HaveOccurred())
1090+
err = client.HSet("hash", "key2", "hello2").Err()
1091+
Expect(err).NotTo(HaveOccurred())
11031092

1104-
hGetAll := client.HGetAllMap("hash")
1105-
Expect(hGetAll.Err()).NotTo(HaveOccurred())
1106-
Expect(hGetAll.Val()).To(Equal(map[string]string{"key1": "hello1", "key2": "hello2"}))
1093+
m, err := client.HGetAll("hash").Result()
1094+
Expect(err).NotTo(HaveOccurred())
1095+
Expect(m).To(Equal(map[string]string{"key1": "hello1", "key2": "hello2"}))
11071096
})
11081097

11091098
It("should HIncrBy", func() {
@@ -1168,28 +1157,31 @@ var _ = Describe("Commands", func() {
11681157
})
11691158

11701159
It("should HMGet", func() {
1171-
hSet := client.HSet("hash", "key1", "hello1")
1172-
Expect(hSet.Err()).NotTo(HaveOccurred())
1173-
hSet = client.HSet("hash", "key2", "hello2")
1174-
Expect(hSet.Err()).NotTo(HaveOccurred())
1160+
err := client.HSet("hash", "key1", "hello1").Err()
1161+
Expect(err).NotTo(HaveOccurred())
1162+
err = client.HSet("hash", "key2", "hello2").Err()
1163+
Expect(err).NotTo(HaveOccurred())
11751164

1176-
hMGet := client.HMGet("hash", "key1", "key2", "_")
1177-
Expect(hMGet.Err()).NotTo(HaveOccurred())
1178-
Expect(hMGet.Val()).To(Equal([]interface{}{"hello1", "hello2", nil}))
1165+
vals, err := client.HMGet("hash", "key1", "key2", "_").Result()
1166+
Expect(err).NotTo(HaveOccurred())
1167+
Expect(vals).To(Equal([]interface{}{"hello1", "hello2", nil}))
11791168
})
11801169

11811170
It("should HMSet", func() {
1182-
hMSet := client.HMSet("hash", "key1", "hello1", "key2", "hello2")
1183-
Expect(hMSet.Err()).NotTo(HaveOccurred())
1184-
Expect(hMSet.Val()).To(Equal("OK"))
1171+
ok, err := client.HMSet("hash", map[string]string{
1172+
"key1": "hello1",
1173+
"key2": "hello2",
1174+
}).Result()
1175+
Expect(err).NotTo(HaveOccurred())
1176+
Expect(ok).To(Equal("OK"))
11851177

1186-
hGet := client.HGet("hash", "key1")
1187-
Expect(hGet.Err()).NotTo(HaveOccurred())
1188-
Expect(hGet.Val()).To(Equal("hello1"))
1178+
v, err := client.HGet("hash", "key1").Result()
1179+
Expect(err).NotTo(HaveOccurred())
1180+
Expect(v).To(Equal("hello1"))
11891181

1190-
hGet = client.HGet("hash", "key2")
1191-
Expect(hGet.Err()).NotTo(HaveOccurred())
1192-
Expect(hGet.Val()).To(Equal("hello2"))
1182+
v, err = client.HGet("hash", "key2").Result()
1183+
Expect(err).NotTo(HaveOccurred())
1184+
Expect(v).To(Equal("hello2"))
11931185
})
11941186

11951187
It("should HSet", func() {

0 commit comments

Comments
 (0)