Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return keys from commands #3130

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,35 @@ func cmdString(cmd Cmder, val interface{}) string {
return util.BytesToString(b)
}

// A wrapper around extract keys, just to make it easier to test.
func GetKeys(cmd Cmder) []string {
return extractKeys(cmd)
}

func extractKeys(cmd Cmder) []string {
firstKeyPos := cmdFirstKeyPos(cmd)
if firstKeyPos == -1 {
return nil
}

args := cmd.Args()
keys := []string{}

switch cmd.Name() {
case "mget":
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach isn't scalable, each time we need a new type of extracting we have to add a command here. Cmder is an interface that should be abstract as possible and should not contain any implementation specific (mget is an implementation specific in this case).

Cmder should return a keys, but shouldn't be aware about the keys position of specific command (this is implementation details), instead the specific command that implements this interface should define keys position for itself.

for i := int(firstKeyPos); i < len(args); i++ {
keys = append(keys, cmd.stringArg(i))
}

default:
if int(firstKeyPos) < len(args) {
keys = append(keys, cmd.stringArg(firstKeyPos))
}
}

return keys
}

//------------------------------------------------------------------------------

type baseCmd struct {
Expand Down
23 changes: 23 additions & 0 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7279,6 +7279,29 @@ var _ = Describe("Commands", func() {
})
})

var _ = Describe("Get Keys from Command", func() {
var client *redis.Client

BeforeEach(func() {
client = redis.NewClient(redisOptions())
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
})

AfterEach(func() {
Expect(client.Close()).NotTo(HaveOccurred())
})

It("returns the keys extracted from the command", func() {
cmd := redis.NewCmd(ctx, "SET", "key1", "value1")
keys := redis.GetKeys(cmd)
Expect(keys).To(Equal([]string{"key1"}))

cmd = redis.NewCmd(ctx, "MGET", "key1", "key2", "key3")
keys = redis.GetKeys(cmd)
Expect(keys).To(Equal([]string{"key1", "key2", "key3"}))
})
})

type numberStruct struct {
Number int
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ require (
)

retract (
v9.5.3 // This version was accidentally released. Please use version 9.6.0 instead.
v9.5.4 // This version was accidentally released. Please use version 9.6.0 instead.
v9.5.3 // This version was accidentally released. Please use version 9.6.0 instead.
)
Loading