Skip to content

Commit d5b2ecc

Browse files
saitofunvmihailenco
authored andcommitted
add xinfo groups command (#1166)
Add xinfo groups command
1 parent 4afa84c commit d5b2ecc

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

command.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,96 @@ func (cmd *XPendingExtCmd) readReply(rd *proto.Reader) error {
12891289

12901290
//------------------------------------------------------------------------------
12911291

1292+
type XInfoGroupsCmd struct {
1293+
baseCmd
1294+
val []XGroup
1295+
}
1296+
1297+
type XGroup struct {
1298+
Name string
1299+
Consumers int64
1300+
Pending int64
1301+
LastDeliveredID string
1302+
}
1303+
1304+
var _ Cmder = (*XInfoGroupsCmd)(nil)
1305+
1306+
func NewXInfoGroupsCmd(stream string) *XInfoGroupsCmd {
1307+
return &XInfoGroupsCmd{
1308+
baseCmd: baseCmd{args: []interface{}{"xinfo", "groups", stream}},
1309+
}
1310+
}
1311+
1312+
func (cmd *XInfoGroupsCmd) Val() []XGroup {
1313+
return cmd.val
1314+
}
1315+
1316+
func (cmd *XInfoGroupsCmd) Result() ([]XGroup, error) {
1317+
return cmd.val, cmd.err
1318+
}
1319+
1320+
func (cmd *XInfoGroupsCmd) String() string {
1321+
return cmdString(cmd, cmd.val)
1322+
}
1323+
1324+
func (cmd *XInfoGroupsCmd) readReply(rd *proto.Reader) error {
1325+
_, cmd.err = rd.ReadArrayReply(
1326+
func(rd *proto.Reader, n int64) (interface{}, error) {
1327+
for i := int64(0); i < n; i++ {
1328+
v, err := rd.ReadReply(xGroupInfoParser)
1329+
if err != nil {
1330+
return nil, err
1331+
}
1332+
cmd.val = append(cmd.val, v.(XGroup))
1333+
}
1334+
return nil, nil
1335+
})
1336+
return nil
1337+
}
1338+
1339+
func xGroupInfoParser(rd *proto.Reader, n int64) (interface{}, error) {
1340+
if n != 8 {
1341+
return nil, fmt.Errorf("redis: got %d elements in XINFO GROUPS reply,"+
1342+
"wanted 8", n)
1343+
}
1344+
var (
1345+
err error
1346+
grp XGroup
1347+
key string
1348+
val string
1349+
)
1350+
1351+
for i := 0; i < 4; i++ {
1352+
key, err = rd.ReadString()
1353+
if err != nil {
1354+
return nil, err
1355+
}
1356+
val, err = rd.ReadString()
1357+
if err != nil {
1358+
return nil, err
1359+
}
1360+
switch key {
1361+
case "name":
1362+
grp.Name = val
1363+
case "consumers":
1364+
grp.Consumers, err = strconv.ParseInt(val, 0, 64)
1365+
case "pending":
1366+
grp.Pending, err = strconv.ParseInt(val, 0, 64)
1367+
case "last-delivered-id":
1368+
grp.LastDeliveredID = val
1369+
default:
1370+
return nil, fmt.Errorf("redis: unexpected content %s "+
1371+
"in XINFO GROUPS reply", key)
1372+
}
1373+
if err != nil {
1374+
return nil, err
1375+
}
1376+
}
1377+
return grp, err
1378+
}
1379+
1380+
//------------------------------------------------------------------------------
1381+
12921382
type ZSliceCmd struct {
12931383
baseCmd
12941384

commands.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ type Cmdable interface {
186186
XClaimJustID(a *XClaimArgs) *StringSliceCmd
187187
XTrim(key string, maxLen int64) *IntCmd
188188
XTrimApprox(key string, maxLen int64) *IntCmd
189+
XInfoGroups(key string) *XInfoGroupsCmd
189190
BZPopMax(timeout time.Duration, keys ...string) *ZWithKeyCmd
190191
BZPopMin(timeout time.Duration, keys ...string) *ZWithKeyCmd
191192
ZAdd(key string, members ...*Z) *IntCmd
@@ -1571,6 +1572,12 @@ func (c cmdable) XTrimApprox(key string, maxLen int64) *IntCmd {
15711572
return cmd
15721573
}
15731574

1575+
func (c cmdable) XInfoGroups(key string) *XInfoGroupsCmd {
1576+
cmd := NewXInfoGroupsCmd(key)
1577+
_ = c(cmd)
1578+
return cmd
1579+
}
1580+
15741581
//------------------------------------------------------------------------------
15751582

15761583
// Z represents sorted set member.

0 commit comments

Comments
 (0)