mirror of https://github.com/go-redis/redis.git
Merge pull request #667 from laouji/666-add-smembers-map-cmd
Add SMembersMap command
This commit is contained in:
commit
e5e021257b
38
command.go
38
command.go
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
@ -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)
|
||||
|
|
|
@ -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())
|
||||
|
|
14
parser.go
14
parser.go
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue