Skip to content

Commit

Permalink
Merge pull request redis#667 from laouji/666-add-smembers-map-cmd
Browse files Browse the repository at this point in the history
Add SMembersMap command
  • Loading branch information
vmihailenco authored Nov 20, 2017
2 parents d0f8697 + b2e8f58 commit e5e0212
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
38 changes: 38 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,44 @@ func (cmd *StringIntMapCmd) readReply(cn *pool.Conn) error {

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

type StringStructMapCmd struct {
baseCmd

val map[string]struct{}
}

var _ Cmder = (*StringStructMapCmd)(nil)

func NewStringStructMapCmd(args ...interface{}) *StringStructMapCmd {
return &StringStructMapCmd{
baseCmd: baseCmd{_args: args},
}
}

func (cmd *StringStructMapCmd) Val() map[string]struct{} {
return cmd.val
}

func (cmd *StringStructMapCmd) Result() (map[string]struct{}, error) {
return cmd.val, cmd.err
}

func (cmd *StringStructMapCmd) String() string {
return cmdString(cmd, cmd.val)
}

func (cmd *StringStructMapCmd) readReply(cn *pool.Conn) error {
var v interface{}
v, cmd.err = cn.Rd.ReadArrayReply(stringStructMapParser)
if cmd.err != nil {
return cmd.err
}
cmd.val = v.(map[string]struct{})
return nil
}

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

type ZSliceCmd struct {
baseCmd

Expand Down
9 changes: 9 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ type Cmdable interface {
SInterStore(destination string, keys ...string) *IntCmd
SIsMember(key string, member interface{}) *BoolCmd
SMembers(key string) *StringSliceCmd
SMembersMap(key string) *StringStructMapCmd
SMove(source, destination string, member interface{}) *BoolCmd
SPop(key string) *StringCmd
SPopN(key string, count int64) *StringSliceCmd
Expand Down Expand Up @@ -1163,12 +1164,20 @@ func (c *cmdable) SIsMember(key string, member interface{}) *BoolCmd {
return cmd
}

// Redis `SMEMBERS key` command output as a slice
func (c *cmdable) SMembers(key string) *StringSliceCmd {
cmd := NewStringSliceCmd("smembers", key)
c.process(cmd)
return cmd
}

// Redis `SMEMBERS key` command output as a map
func (c *cmdable) SMembersMap(key string) *StringStructMapCmd {
cmd := NewStringStructMapCmd("smembers", key)
c.process(cmd)
return cmd
}

func (c *cmdable) SMove(source, destination string, member interface{}) *BoolCmd {
cmd := NewBoolCmd("smove", source, destination, member)
c.process(cmd)
Expand Down
11 changes: 11 additions & 0 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1848,6 +1848,17 @@ var _ = Describe("Commands", func() {
Expect(sMembers.Val()).To(ConsistOf([]string{"Hello", "World"}))
})

It("should SMembersMap", func() {
sAdd := client.SAdd("set", "Hello")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd("set", "World")
Expect(sAdd.Err()).NotTo(HaveOccurred())

sMembersMap := client.SMembersMap("set")
Expect(sMembersMap.Err()).NotTo(HaveOccurred())
Expect(sMembersMap.Val()).To(Equal(map[string]struct{}{"Hello": struct{}{}, "World": struct{}{}}))
})

It("should SMove", func() {
sAdd := client.SAdd("set1", "one")
Expect(sAdd.Err()).NotTo(HaveOccurred())
Expand Down
14 changes: 14 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@ func stringIntMapParser(rd *proto.Reader, n int64) (interface{}, error) {
return m, nil
}

// Implements proto.MultiBulkParse
func stringStructMapParser(rd *proto.Reader, n int64) (interface{}, error) {
m := make(map[string]struct{}, n)
for i := int64(0); i < n; i++ {
key, err := rd.ReadStringReply()
if err != nil {
return nil, err
}

m[key] = struct{}{}
}
return m, nil
}

// Implements proto.MultiBulkParse
func zSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
zz := make([]Z, n/2)
Expand Down

0 comments on commit e5e0212

Please sign in to comment.