Skip to content

Commit

Permalink
Merge pull request redis#212 from go-redis/fix/accept-interface-list-set
Browse files Browse the repository at this point in the history
Accept interface{} values in list and set commands.
  • Loading branch information
vmihailenco committed Dec 10, 2015
2 parents fb4fc5e + 401979b commit 781f7f8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
44 changes: 18 additions & 26 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -767,19 +767,17 @@ func (c *commandable) LPop(key string) *StringCmd {
return cmd
}

func (c *commandable) LPush(key string, values ...string) *IntCmd {
args := make([]interface{}, 2+len(values))
func (c *commandable) LPush(key string, values ...interface{}) *IntCmd {
args := make([]interface{}, 2, 2+len(values))
args[0] = "LPUSH"
args[1] = key
for i, value := range values {
args[2+i] = value
}
args = append(args, values...)
cmd := NewIntCmd(args...)
c.Process(cmd)
return cmd
}

func (c *commandable) LPushX(key, value string) *IntCmd {
func (c *commandable) LPushX(key, value interface{}) *IntCmd {
cmd := NewIntCmd("LPUSHX", key, value)
c.Process(cmd)
return cmd
Expand All @@ -796,13 +794,13 @@ func (c *commandable) LRange(key string, start, stop int64) *StringSliceCmd {
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)
c.Process(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)
c.Process(cmd)
return cmd
Expand Down Expand Up @@ -831,33 +829,29 @@ func (c *commandable) RPopLPush(source, destination string) *StringCmd {
return cmd
}

func (c *commandable) RPush(key string, values ...string) *IntCmd {
args := make([]interface{}, 2+len(values))
func (c *commandable) RPush(key string, values ...interface{}) *IntCmd {
args := make([]interface{}, 2, 2+len(values))
args[0] = "RPUSH"
args[1] = key
for i, value := range values {
args[2+i] = value
}
args = append(args, values...)
cmd := NewIntCmd(args...)
c.Process(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)
c.Process(cmd)
return cmd
}

//------------------------------------------------------------------------------

func (c *commandable) SAdd(key string, members ...string) *IntCmd {
args := make([]interface{}, 2+len(members))
func (c *commandable) SAdd(key string, members ...interface{}) *IntCmd {
args := make([]interface{}, 2, 2+len(members))
args[0] = "SADD"
args[1] = key
for i, member := range members {
args[2+i] = member
}
args = append(args, members...)
cmd := NewIntCmd(args...)
c.Process(cmd)
return cmd
Expand Down Expand Up @@ -915,7 +909,7 @@ func (c *commandable) SInterStore(destination string, keys ...string) *IntCmd {
return cmd
}

func (c *commandable) SIsMember(key, member string) *BoolCmd {
func (c *commandable) SIsMember(key string, member interface{}) *BoolCmd {
cmd := NewBoolCmd("SISMEMBER", key, member)
c.Process(cmd)
return cmd
Expand All @@ -927,7 +921,7 @@ func (c *commandable) SMembers(key string) *StringSliceCmd {
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)
c.Process(cmd)
return cmd
Expand All @@ -953,13 +947,11 @@ func (c *commandable) SRandMemberN(key string, count int64) *StringSliceCmd {
return cmd
}

func (c *commandable) SRem(key string, members ...string) *IntCmd {
args := make([]interface{}, 2+len(members))
func (c *commandable) SRem(key string, members ...interface{}) *IntCmd {
args := make([]interface{}, 2, 2+len(members))
args[0] = "SREM"
args[1] = key
for i, member := range members {
args[2+i] = member
}
args = append(args, members...)
cmd := NewIntCmd(args...)
c.Process(cmd)
return cmd
Expand Down
29 changes: 29 additions & 0 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1379,6 +1379,17 @@ var _ = Describe("Commands", func() {
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() {
lPush := client.LPush("list", "World")
Expect(lPush.Err()).NotTo(HaveOccurred())
Expand Down Expand Up @@ -1578,6 +1589,24 @@ var _ = Describe("Commands", func() {
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() {
sAdd := client.SAdd("set", "Hello")
Expect(sAdd.Err()).NotTo(HaveOccurred())
Expand Down

0 comments on commit 781f7f8

Please sign in to comment.