Skip to content

Commit

Permalink
Merge pull request redis#65 from go-redis/fix/pubsubnumsub
Browse files Browse the repository at this point in the history
Better fix for pubsub numsub
  • Loading branch information
vmihailenco committed Jan 30, 2015
2 parents 2a81661 + 3485517 commit b408dc2
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 6 deletions.
37 changes: 37 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var (
_ Cmder = (*StringSliceCmd)(nil)
_ Cmder = (*BoolSliceCmd)(nil)
_ Cmder = (*StringStringMapCmd)(nil)
_ Cmder = (*StringIntMapCmd)(nil)
_ Cmder = (*ZSliceCmd)(nil)
_ Cmder = (*ScanCmd)(nil)
)
Expand Down Expand Up @@ -514,6 +515,42 @@ func (cmd *StringStringMapCmd) parseReply(rd *bufio.Reader) error {

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

type StringIntMapCmd struct {
*baseCmd

val map[string]int64
}

func NewStringIntMapCmd(args ...string) *StringIntMapCmd {
return &StringIntMapCmd{
baseCmd: newBaseCmd(args...),
}
}

func (cmd *StringIntMapCmd) Val() map[string]int64 {
return cmd.val
}

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

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

func (cmd *StringIntMapCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, parseStringIntMap)
if err != nil {
cmd.err = err
return err
}
cmd.val = v.(map[string]int64)
return nil
}

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

type ZSliceCmd struct {
*baseCmd

Expand Down
4 changes: 2 additions & 2 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -1231,10 +1231,10 @@ func (c *Client) PubSubChannels(pattern string) *StringSliceCmd {
return cmd
}

func (c *Client) PubSubNumSub(channels ...string) *SliceCmd {
func (c *Client) PubSubNumSub(channels ...string) *StringIntMapCmd {
args := []string{"PUBSUB", "NUMSUB"}
args = append(args, channels...)
cmd := NewSliceCmd(args...)
cmd := NewStringIntMapCmd(args...)
c.Process(cmd)
return cmd
}
Expand Down
32 changes: 32 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,38 @@ func parseStringStringMap(rd *bufio.Reader, n int64) (interface{}, error) {
return m, nil
}

func parseStringIntMap(rd *bufio.Reader, n int64) (interface{}, error) {
m := make(map[string]int64, n/2)
for i := int64(0); i < n; i += 2 {
keyiface, err := parseReply(rd, nil)
if err != nil {
return nil, err
}
key, ok := keyiface.(string)
if !ok {
return nil, fmt.Errorf("got %T, expected string", keyiface)
}

valueiface, err := parseReply(rd, nil)
if err != nil {
return nil, err
}
switch value := valueiface.(type) {
case int64:
m[key] = value
case string:
m[key], err = strconv.ParseInt(value, 10, 64)
if err != nil {
return nil, fmt.Errorf("got %v, expected number", value)
}
default:
return nil, fmt.Errorf("got %T, expected number or string", valueiface)

}
}
return m, nil
}

func parseZSlice(rd *bufio.Reader, n int64) (interface{}, error) {
zz := make([]Z, n/2)
for i := int64(0); i < n; i += 2 {
Expand Down
8 changes: 4 additions & 4 deletions pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ var _ = Describe("PubSub", func() {

channels, err := client.PubSubNumSub("mychannel", "mychannel2", "mychannel3").Result()
Expect(err).NotTo(HaveOccurred())
Expect(channels).To(Equal([]interface{}{
"mychannel", int64(1),
"mychannel2", int64(1),
"mychannel3", int64(0),
Expect(channels).To(Equal(map[string]int64{
"mychannel": 1,
"mychannel2": 1,
"mychannel3": 0,
}))
})

Expand Down

0 comments on commit b408dc2

Please sign in to comment.