Skip to content

Commit 8de72d6

Browse files
committed
Extract keys from string commands
1 parent c4cce23 commit 8de72d6

File tree

3 files changed

+63
-8
lines changed

3 files changed

+63
-8
lines changed

command.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,22 @@ func (cmd *baseCmd) readRawReply(rd *proto.Reader) (err error) {
234234
return err
235235
}
236236

237+
// getInterleavedArguments returns arguments at even indices starting from index 0.
238+
func (cmd *baseCmd) getInterleavedArguments() []string {
239+
return cmd.getInterleavedArgumentsWithOffset(0)
240+
}
241+
242+
// getInterleavedArgumentsWithOffset returns arguments at even indices starting from the specified offset.
243+
func (cmd *baseCmd) getInterleavedArgumentsWithOffset(offset int) []string {
244+
var matchingArguments []string
245+
for i := offset; i < len(cmd.args); i += 2 {
246+
if arg, ok := cmd.args[i].(string); ok {
247+
matchingArguments = append(matchingArguments, arg)
248+
}
249+
}
250+
return matchingArguments
251+
}
252+
237253
//------------------------------------------------------------------------------
238254

239255
type Cmd struct {

commands_test.go

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7279,7 +7279,7 @@ var _ = Describe("Commands", func() {
72797279
})
72807280
})
72817281

7282-
var _ = Describe("Get Keys from Commands", func() {
7282+
var _ = Describe("Keys Extraction Tests", func() {
72837283
var client *redis.Client
72847284
var ctx = context.TODO()
72857285

@@ -7292,12 +7292,51 @@ var _ = Describe("Get Keys from Commands", func() {
72927292
Expect(client.Close()).NotTo(HaveOccurred())
72937293
})
72947294

7295-
It("should test string commands", func() {
7296-
setCmd := client.Set(ctx, "key1", "val1", 0)
7297-
Expect(setCmd.Keys()).To(Equal([]string{"key1"}))
7295+
// STRING COMMANDS
72987296

7299-
getCmd := client.MGet(ctx, "key1", "key2", "key3")
7300-
Expect(getCmd.Keys()).To(Equal([]string{"key1", "key2", "key3"}))
7297+
It("should test Append command", func() {
7298+
cmd := client.Append(ctx, "key1", "value")
7299+
Expect(cmd.Keys()).To(Equal([]string{"key1"}))
7300+
})
7301+
7302+
It("should test Decr command", func() {
7303+
cmd := client.Decr(ctx, "key1")
7304+
Expect(cmd.Keys()).To(Equal([]string{"key1"}))
7305+
})
7306+
7307+
It("should test Get command", func() {
7308+
cmd := client.Get(ctx, "key1")
7309+
Expect(cmd.Keys()).To(Equal([]string{"key1"}))
7310+
})
7311+
7312+
It("should test MGet command", func() {
7313+
cmd := client.MGet(ctx, "key1", "key2", "key3")
7314+
Expect(cmd.Keys()).To(Equal([]string{"key1", "key2", "key3"}))
7315+
})
7316+
7317+
It("should test Set command", func() {
7318+
cmd := client.Set(ctx, "key1", "value", time.Second)
7319+
Expect(cmd.Keys()).To(Equal([]string{"key1"}))
7320+
})
7321+
7322+
It("should test MSet command", func() {
7323+
cmd := client.MSet(ctx, "key1", "value1", "key2", "value2")
7324+
Expect(cmd.Keys()).To(Equal([]string{"key1", "key2"}))
7325+
})
7326+
7327+
It("should test IncrBy command", func() {
7328+
cmd := client.IncrBy(ctx, "key1", 10)
7329+
Expect(cmd.Keys()).To(Equal([]string{"key1"}))
7330+
})
7331+
7332+
It("should test SetNX command", func() {
7333+
cmd := client.SetNX(ctx, "key1", "value", time.Second)
7334+
Expect(cmd.Keys()).To(Equal([]string{"key1"}))
7335+
})
7336+
7337+
It("should test GetDel command", func() {
7338+
cmd := client.GetDel(ctx, "key1")
7339+
Expect(cmd.Keys()).To(Equal([]string{"key1"}))
73017340
})
73027341
})
73037342

string_commands.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (c cmdable) MSet(ctx context.Context, values ...interface{}) *StatusCmd {
151151
args[0] = "mset"
152152
args = appendArgs(args, values)
153153
cmd := NewStatusCmd(ctx, args...)
154-
// cmd.keys = append(cmd.keys, keys...)
154+
cmd.keys = append(cmd.keys, cmd.getInterleavedArgumentsWithOffset(1)...)
155155
_ = c(ctx, cmd)
156156
return cmd
157157
}
@@ -166,7 +166,7 @@ func (c cmdable) MSetNX(ctx context.Context, values ...interface{}) *BoolCmd {
166166
args[0] = "msetnx"
167167
args = appendArgs(args, values)
168168
cmd := NewBoolCmd(ctx, args...)
169-
// cmd.keys = append(cmd.keys, keys...)
169+
cmd.keys = append(cmd.keys, cmd.getInterleavedArgumentsWithOffset(1)...)
170170
_ = c(ctx, cmd)
171171
return cmd
172172
}

0 commit comments

Comments
 (0)