Extract keys from string commands

This commit is contained in:
ofekshenawa 2024-09-23 01:32:35 +03:00
parent c4cce2333a
commit 8de72d6d3c
3 changed files with 63 additions and 8 deletions

View File

@ -234,6 +234,22 @@ func (cmd *baseCmd) readRawReply(rd *proto.Reader) (err error) {
return err
}
// getInterleavedArguments returns arguments at even indices starting from index 0.
func (cmd *baseCmd) getInterleavedArguments() []string {
return cmd.getInterleavedArgumentsWithOffset(0)
}
// getInterleavedArgumentsWithOffset returns arguments at even indices starting from the specified offset.
func (cmd *baseCmd) getInterleavedArgumentsWithOffset(offset int) []string {
var matchingArguments []string
for i := offset; i < len(cmd.args); i += 2 {
if arg, ok := cmd.args[i].(string); ok {
matchingArguments = append(matchingArguments, arg)
}
}
return matchingArguments
}
//------------------------------------------------------------------------------
type Cmd struct {

View File

@ -7279,7 +7279,7 @@ var _ = Describe("Commands", func() {
})
})
var _ = Describe("Get Keys from Commands", func() {
var _ = Describe("Keys Extraction Tests", func() {
var client *redis.Client
var ctx = context.TODO()
@ -7292,12 +7292,51 @@ var _ = Describe("Get Keys from Commands", func() {
Expect(client.Close()).NotTo(HaveOccurred())
})
It("should test string commands", func() {
setCmd := client.Set(ctx, "key1", "val1", 0)
Expect(setCmd.Keys()).To(Equal([]string{"key1"}))
// STRING COMMANDS
getCmd := client.MGet(ctx, "key1", "key2", "key3")
Expect(getCmd.Keys()).To(Equal([]string{"key1", "key2", "key3"}))
It("should test Append command", func() {
cmd := client.Append(ctx, "key1", "value")
Expect(cmd.Keys()).To(Equal([]string{"key1"}))
})
It("should test Decr command", func() {
cmd := client.Decr(ctx, "key1")
Expect(cmd.Keys()).To(Equal([]string{"key1"}))
})
It("should test Get command", func() {
cmd := client.Get(ctx, "key1")
Expect(cmd.Keys()).To(Equal([]string{"key1"}))
})
It("should test MGet command", func() {
cmd := client.MGet(ctx, "key1", "key2", "key3")
Expect(cmd.Keys()).To(Equal([]string{"key1", "key2", "key3"}))
})
It("should test Set command", func() {
cmd := client.Set(ctx, "key1", "value", time.Second)
Expect(cmd.Keys()).To(Equal([]string{"key1"}))
})
It("should test MSet command", func() {
cmd := client.MSet(ctx, "key1", "value1", "key2", "value2")
Expect(cmd.Keys()).To(Equal([]string{"key1", "key2"}))
})
It("should test IncrBy command", func() {
cmd := client.IncrBy(ctx, "key1", 10)
Expect(cmd.Keys()).To(Equal([]string{"key1"}))
})
It("should test SetNX command", func() {
cmd := client.SetNX(ctx, "key1", "value", time.Second)
Expect(cmd.Keys()).To(Equal([]string{"key1"}))
})
It("should test GetDel command", func() {
cmd := client.GetDel(ctx, "key1")
Expect(cmd.Keys()).To(Equal([]string{"key1"}))
})
})

View File

@ -151,7 +151,7 @@ func (c cmdable) MSet(ctx context.Context, values ...interface{}) *StatusCmd {
args[0] = "mset"
args = appendArgs(args, values)
cmd := NewStatusCmd(ctx, args...)
// cmd.keys = append(cmd.keys, keys...)
cmd.keys = append(cmd.keys, cmd.getInterleavedArgumentsWithOffset(1)...)
_ = c(ctx, cmd)
return cmd
}
@ -166,7 +166,7 @@ func (c cmdable) MSetNX(ctx context.Context, values ...interface{}) *BoolCmd {
args[0] = "msetnx"
args = appendArgs(args, values)
cmd := NewBoolCmd(ctx, args...)
// cmd.keys = append(cmd.keys, keys...)
cmd.keys = append(cmd.keys, cmd.getInterleavedArgumentsWithOffset(1)...)
_ = c(ctx, cmd)
return cmd
}