Accept interface{} values in list and set commands.

This commit is contained in:
Vladimir Mihailenco 2015-12-10 09:52:42 +02:00
parent fb4fc5e880
commit 401979b597
2 changed files with 47 additions and 26 deletions

View File

@ -767,19 +767,17 @@ func (c *commandable) LPop(key string) *StringCmd {
return cmd return cmd
} }
func (c *commandable) LPush(key string, values ...string) *IntCmd { func (c *commandable) LPush(key string, values ...interface{}) *IntCmd {
args := make([]interface{}, 2+len(values)) args := make([]interface{}, 2, 2+len(values))
args[0] = "LPUSH" args[0] = "LPUSH"
args[1] = key args[1] = key
for i, value := range values { args = append(args, values...)
args[2+i] = value
}
cmd := NewIntCmd(args...) cmd := NewIntCmd(args...)
c.Process(cmd) c.Process(cmd)
return cmd return cmd
} }
func (c *commandable) LPushX(key, value string) *IntCmd { func (c *commandable) LPushX(key, value interface{}) *IntCmd {
cmd := NewIntCmd("LPUSHX", key, value) cmd := NewIntCmd("LPUSHX", key, value)
c.Process(cmd) c.Process(cmd)
return cmd return cmd
@ -796,13 +794,13 @@ func (c *commandable) LRange(key string, start, stop int64) *StringSliceCmd {
return cmd return cmd
} }
func (c *commandable) LRem(key string, count int64, value string) *IntCmd { func (c *commandable) LRem(key string, count int64, value interface{}) *IntCmd {
cmd := NewIntCmd("LREM", key, count, value) cmd := NewIntCmd("LREM", key, count, value)
c.Process(cmd) c.Process(cmd)
return cmd return cmd
} }
func (c *commandable) LSet(key string, index int64, value string) *StatusCmd { func (c *commandable) LSet(key string, index int64, value interface{}) *StatusCmd {
cmd := NewStatusCmd("LSET", key, index, value) cmd := NewStatusCmd("LSET", key, index, value)
c.Process(cmd) c.Process(cmd)
return cmd return cmd
@ -831,19 +829,17 @@ func (c *commandable) RPopLPush(source, destination string) *StringCmd {
return cmd return cmd
} }
func (c *commandable) RPush(key string, values ...string) *IntCmd { func (c *commandable) RPush(key string, values ...interface{}) *IntCmd {
args := make([]interface{}, 2+len(values)) args := make([]interface{}, 2, 2+len(values))
args[0] = "RPUSH" args[0] = "RPUSH"
args[1] = key args[1] = key
for i, value := range values { args = append(args, values...)
args[2+i] = value
}
cmd := NewIntCmd(args...) cmd := NewIntCmd(args...)
c.Process(cmd) c.Process(cmd)
return cmd return cmd
} }
func (c *commandable) RPushX(key string, value string) *IntCmd { func (c *commandable) RPushX(key string, value interface{}) *IntCmd {
cmd := NewIntCmd("RPUSHX", key, value) cmd := NewIntCmd("RPUSHX", key, value)
c.Process(cmd) c.Process(cmd)
return cmd return cmd
@ -851,13 +847,11 @@ func (c *commandable) RPushX(key string, value string) *IntCmd {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
func (c *commandable) SAdd(key string, members ...string) *IntCmd { func (c *commandable) SAdd(key string, members ...interface{}) *IntCmd {
args := make([]interface{}, 2+len(members)) args := make([]interface{}, 2, 2+len(members))
args[0] = "SADD" args[0] = "SADD"
args[1] = key args[1] = key
for i, member := range members { args = append(args, members...)
args[2+i] = member
}
cmd := NewIntCmd(args...) cmd := NewIntCmd(args...)
c.Process(cmd) c.Process(cmd)
return cmd return cmd
@ -915,7 +909,7 @@ func (c *commandable) SInterStore(destination string, keys ...string) *IntCmd {
return cmd return cmd
} }
func (c *commandable) SIsMember(key, member string) *BoolCmd { func (c *commandable) SIsMember(key string, member interface{}) *BoolCmd {
cmd := NewBoolCmd("SISMEMBER", key, member) cmd := NewBoolCmd("SISMEMBER", key, member)
c.Process(cmd) c.Process(cmd)
return cmd return cmd
@ -927,7 +921,7 @@ func (c *commandable) SMembers(key string) *StringSliceCmd {
return cmd return cmd
} }
func (c *commandable) SMove(source, destination, member string) *BoolCmd { func (c *commandable) SMove(source, destination string, member interface{}) *BoolCmd {
cmd := NewBoolCmd("SMOVE", source, destination, member) cmd := NewBoolCmd("SMOVE", source, destination, member)
c.Process(cmd) c.Process(cmd)
return cmd return cmd
@ -953,13 +947,11 @@ func (c *commandable) SRandMemberN(key string, count int64) *StringSliceCmd {
return cmd return cmd
} }
func (c *commandable) SRem(key string, members ...string) *IntCmd { func (c *commandable) SRem(key string, members ...interface{}) *IntCmd {
args := make([]interface{}, 2+len(members)) args := make([]interface{}, 2, 2+len(members))
args[0] = "SREM" args[0] = "SREM"
args[1] = key args[1] = key
for i, member := range members { args = append(args, members...)
args[2+i] = member
}
cmd := NewIntCmd(args...) cmd := NewIntCmd(args...)
c.Process(cmd) c.Process(cmd)
return cmd return cmd

View File

@ -1379,6 +1379,17 @@ var _ = Describe("Commands", func() {
Expect(lRange.Val()).To(Equal([]string{"Hello", "World"})) Expect(lRange.Val()).To(Equal([]string{"Hello", "World"}))
}) })
It("should LPush bytes", func() {
lPush := client.LPush("list", []byte("World"))
Expect(lPush.Err()).NotTo(HaveOccurred())
lPush = client.LPush("list", []byte("Hello"))
Expect(lPush.Err()).NotTo(HaveOccurred())
lRange := client.LRange("list", 0, -1)
Expect(lRange.Err()).NotTo(HaveOccurred())
Expect(lRange.Val()).To(Equal([]string{"Hello", "World"}))
})
It("should LPushX", func() { It("should LPushX", func() {
lPush := client.LPush("list", "World") lPush := client.LPush("list", "World")
Expect(lPush.Err()).NotTo(HaveOccurred()) Expect(lPush.Err()).NotTo(HaveOccurred())
@ -1578,6 +1589,24 @@ var _ = Describe("Commands", func() {
Expect(sMembers.Val()).To(ConsistOf([]string{"Hello", "World"})) Expect(sMembers.Val()).To(ConsistOf([]string{"Hello", "World"}))
}) })
It("should SAdd bytes", func() {
sAdd := client.SAdd("set", []byte("Hello"))
Expect(sAdd.Err()).NotTo(HaveOccurred())
Expect(sAdd.Val()).To(Equal(int64(1)))
sAdd = client.SAdd("set", []byte("World"))
Expect(sAdd.Err()).NotTo(HaveOccurred())
Expect(sAdd.Val()).To(Equal(int64(1)))
sAdd = client.SAdd("set", []byte("World"))
Expect(sAdd.Err()).NotTo(HaveOccurred())
Expect(sAdd.Val()).To(Equal(int64(0)))
sMembers := client.SMembers("set")
Expect(sMembers.Err()).NotTo(HaveOccurred())
Expect(sMembers.Val()).To(ConsistOf([]string{"Hello", "World"}))
})
It("should SCard", func() { It("should SCard", func() {
sAdd := client.SAdd("set", "Hello") sAdd := client.SAdd("set", "Hello")
Expect(sAdd.Err()).NotTo(HaveOccurred()) Expect(sAdd.Err()).NotTo(HaveOccurred())