mirror of
https://github.com/redis/go-redis.git
synced 2025-04-17 20:17:02 +03:00
Extract keys from string commands
This commit is contained in:
parent
c4cce2333a
commit
8de72d6d3c
16
command.go
16
command.go
@ -234,6 +234,22 @@ func (cmd *baseCmd) readRawReply(rd *proto.Reader) (err error) {
|
|||||||
return err
|
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 {
|
type Cmd struct {
|
||||||
|
@ -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 client *redis.Client
|
||||||
var ctx = context.TODO()
|
var ctx = context.TODO()
|
||||||
|
|
||||||
@ -7292,12 +7292,51 @@ var _ = Describe("Get Keys from Commands", func() {
|
|||||||
Expect(client.Close()).NotTo(HaveOccurred())
|
Expect(client.Close()).NotTo(HaveOccurred())
|
||||||
})
|
})
|
||||||
|
|
||||||
It("should test string commands", func() {
|
// STRING COMMANDS
|
||||||
setCmd := client.Set(ctx, "key1", "val1", 0)
|
|
||||||
Expect(setCmd.Keys()).To(Equal([]string{"key1"}))
|
|
||||||
|
|
||||||
getCmd := client.MGet(ctx, "key1", "key2", "key3")
|
It("should test Append command", func() {
|
||||||
Expect(getCmd.Keys()).To(Equal([]string{"key1", "key2", "key3"}))
|
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"}))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -151,7 +151,7 @@ func (c cmdable) MSet(ctx context.Context, values ...interface{}) *StatusCmd {
|
|||||||
args[0] = "mset"
|
args[0] = "mset"
|
||||||
args = appendArgs(args, values)
|
args = appendArgs(args, values)
|
||||||
cmd := NewStatusCmd(ctx, args...)
|
cmd := NewStatusCmd(ctx, args...)
|
||||||
// cmd.keys = append(cmd.keys, keys...)
|
cmd.keys = append(cmd.keys, cmd.getInterleavedArgumentsWithOffset(1)...)
|
||||||
_ = c(ctx, cmd)
|
_ = c(ctx, cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
@ -166,7 +166,7 @@ func (c cmdable) MSetNX(ctx context.Context, values ...interface{}) *BoolCmd {
|
|||||||
args[0] = "msetnx"
|
args[0] = "msetnx"
|
||||||
args = appendArgs(args, values)
|
args = appendArgs(args, values)
|
||||||
cmd := NewBoolCmd(ctx, args...)
|
cmd := NewBoolCmd(ctx, args...)
|
||||||
// cmd.keys = append(cmd.keys, keys...)
|
cmd.keys = append(cmd.keys, cmd.getInterleavedArgumentsWithOffset(1)...)
|
||||||
_ = c(ctx, cmd)
|
_ = c(ctx, cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user