Skip to content

Commit 984bc28

Browse files
authored
Add support for COMMAND GETKEYS & COMMAND GETKEYSANDFLAGS (#2500)
* feat: Adding support for CommandsGetKeysAndFlags & CommandGetKeys Co-authored-by: Anuragkillswitch <[email protected]>
1 parent 6790337 commit 984bc28

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

command.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4168,3 +4168,81 @@ func (cmd *LCSCmd) readPosition(rd *proto.Reader) (pos LCSPosition, err error) {
41684168

41694169
return pos, nil
41704170
}
4171+
4172+
// ------------------------------------------------------------------------
4173+
4174+
type KeyFlags struct {
4175+
Key string
4176+
Flags []string
4177+
}
4178+
4179+
type KeyFlagsCmd struct {
4180+
baseCmd
4181+
4182+
val []KeyFlags
4183+
}
4184+
4185+
var _ Cmder = (*KeyFlagsCmd)(nil)
4186+
4187+
func NewKeyFlagsCmd(ctx context.Context, args ...interface{}) *KeyFlagsCmd {
4188+
return &KeyFlagsCmd{
4189+
baseCmd: baseCmd{
4190+
ctx: ctx,
4191+
args: args,
4192+
},
4193+
}
4194+
}
4195+
4196+
func (cmd *KeyFlagsCmd) SetVal(val []KeyFlags) {
4197+
cmd.val = val
4198+
}
4199+
4200+
func (cmd *KeyFlagsCmd) Val() []KeyFlags {
4201+
return cmd.val
4202+
}
4203+
4204+
func (cmd *KeyFlagsCmd) Result() ([]KeyFlags, error) {
4205+
return cmd.val, cmd.err
4206+
}
4207+
4208+
func (cmd *KeyFlagsCmd) String() string {
4209+
return cmdString(cmd, cmd.val)
4210+
}
4211+
4212+
func (cmd *KeyFlagsCmd) readReply(rd *proto.Reader) error {
4213+
n, err := rd.ReadArrayLen()
4214+
if err != nil {
4215+
return err
4216+
}
4217+
4218+
if n == 0 {
4219+
cmd.val = make([]KeyFlags, 0)
4220+
return nil
4221+
}
4222+
4223+
cmd.val = make([]KeyFlags, n)
4224+
4225+
for i := 0; i < len(cmd.val); i++ {
4226+
4227+
if err = rd.ReadFixedArrayLen(2); err != nil {
4228+
return err
4229+
}
4230+
4231+
if cmd.val[i].Key, err = rd.ReadString(); err != nil {
4232+
return err
4233+
}
4234+
flagsLen, err := rd.ReadArrayLen()
4235+
if err != nil {
4236+
return err
4237+
}
4238+
cmd.val[i].Flags = make([]string, flagsLen)
4239+
4240+
for j := 0; j < flagsLen; j++ {
4241+
if cmd.val[i].Flags[j], err = rd.ReadString(); err != nil {
4242+
return err
4243+
}
4244+
}
4245+
}
4246+
4247+
return nil
4248+
}

commands.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ type Cmdable interface {
129129

130130
Command(ctx context.Context) *CommandsInfoCmd
131131
CommandList(ctx context.Context, filter *FilterBy) *StringSliceCmd
132+
CommandGetKeys(ctx context.Context, commands ...interface{}) *StringSliceCmd
133+
CommandGetKeysAndFlags(ctx context.Context, commands ...interface{}) *KeyFlagsCmd
132134
ClientGetName(ctx context.Context) *StringCmd
133135
Echo(ctx context.Context, message interface{}) *StringCmd
134136
Ping(ctx context.Context) *StatusCmd
@@ -568,6 +570,26 @@ func (c cmdable) CommandList(ctx context.Context, filter *FilterBy) *StringSlice
568570
return cmd
569571
}
570572

573+
func (c cmdable) CommandGetKeys(ctx context.Context, commands ...interface{}) *StringSliceCmd {
574+
args := make([]interface{}, 2+len(commands))
575+
args[0] = "command"
576+
args[1] = "getkeys"
577+
copy(args[2:], commands)
578+
cmd := NewStringSliceCmd(ctx, args...)
579+
_ = c(ctx, cmd)
580+
return cmd
581+
}
582+
583+
func (c cmdable) CommandGetKeysAndFlags(ctx context.Context, commands ...interface{}) *KeyFlagsCmd {
584+
args := make([]interface{}, 2+len(commands))
585+
args[0] = "command"
586+
args[1] = "getkeysandflags"
587+
copy(args[2:], commands)
588+
cmd := NewKeyFlagsCmd(ctx, args...)
589+
_ = c(ctx, cmd)
590+
return cmd
591+
}
592+
571593
// ClientGetName returns the name of the connection.
572594
func (c cmdable) ClientGetName(ctx context.Context) *StringCmd {
573595
cmd := NewStringCmd(ctx, "client", "getname")

commands_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,43 @@ var _ = Describe("Commands", func() {
132132
}, "30s").Should(Equal("Background saving started"))
133133
})
134134

135+
It("Should CommandGetKeys", func() {
136+
keys, err := client.CommandGetKeys(ctx, "MSET", "a", "b", "c", "d", "e", "f").Result()
137+
Expect(err).NotTo(HaveOccurred())
138+
Expect(keys).To(Equal([]string{"a", "c", "e"}))
139+
140+
keys, err = client.CommandGetKeys(ctx, "EVAL", "not consulted", "3", "key1", "key2", "key3", "arg1", "arg2", "arg3", "argN").Result()
141+
Expect(err).NotTo(HaveOccurred())
142+
Expect(keys).To(Equal([]string{"key1", "key2", "key3"}))
143+
144+
keys, err = client.CommandGetKeys(ctx, "SORT", "mylist", "ALPHA", "STORE", "outlist").Result()
145+
Expect(err).NotTo(HaveOccurred())
146+
Expect(keys).To(Equal([]string{"mylist", "outlist"}))
147+
148+
_, err = client.CommandGetKeys(ctx, "FAKECOMMAND", "arg1", "arg2").Result()
149+
Expect(err).To(HaveOccurred())
150+
Expect(err).To(MatchError("ERR Invalid command specified"))
151+
})
152+
153+
It("should CommandGetKeysAndFlags", func() {
154+
keysAndFlags, err := client.CommandGetKeysAndFlags(ctx, "LMOVE", "mylist1", "mylist2", "left", "left").Result()
155+
Expect(err).NotTo(HaveOccurred())
156+
Expect(keysAndFlags).To(Equal([]redis.KeyFlags{
157+
{
158+
Key: "mylist1",
159+
Flags: []string{"RW", "access", "delete"},
160+
},
161+
{
162+
Key: "mylist2",
163+
Flags: []string{"RW", "insert"},
164+
},
165+
}))
166+
167+
_, err = client.CommandGetKeysAndFlags(ctx, "FAKECOMMAND", "arg1", "arg2").Result()
168+
Expect(err).To(HaveOccurred())
169+
Expect(err).To(MatchError("ERR Invalid command specified"))
170+
})
171+
135172
It("should ClientKill", func() {
136173
r := client.ClientKill(ctx, "1.1.1.1:1111")
137174
Expect(r.Err()).To(MatchError("ERR No such client"))

0 commit comments

Comments
 (0)