From 1e5a1ab0d67a2652d25820f5f8d51c53e2b891a8 Mon Sep 17 00:00:00 2001 From: Anuragkillswitch <70265851+Anuragkillswitch@users.noreply.github.com> Date: Fri, 24 Mar 2023 14:47:25 +0530 Subject: [PATCH 01/23] feat: Adding support for CommandsGetKeysAndFlags & CommandGetKeys --- command.go | 89 ++++++++++++++++++++++++++++++++++++++++++++++++ commands.go | 28 +++++++++++++++ commands_test.go | 22 ++++++++++++ 3 files changed, 139 insertions(+) diff --git a/command.go b/command.go index e05f0ed37..5f388fc0f 100644 --- a/command.go +++ b/command.go @@ -4168,3 +4168,92 @@ func (cmd *LCSCmd) readPosition(rd *proto.Reader) (pos LCSPosition, err error) { return pos, nil } + +// ------------------------------------------------------------------------ + +type KeyFlags struct { + Key string + Flags []string +} + +type KeyFlagsCmd struct { + baseCmd + + val []KeyFlags +} + +var _ Cmder = (*KeyFlagsCmd)(nil) + +func NewKeyFlagsCmd(ctx context.Context, args ...interface{}) *KeyFlagsCmd { + return &KeyFlagsCmd{ + baseCmd: baseCmd{ + ctx: ctx, + args: args, + }, + } +} + +func (cmd *KeyFlagsCmd) SetVal(val []KeyFlags) { + cmd.val = val +} + +func (cmd *KeyFlagsCmd) Val() []KeyFlags { + return cmd.val +} + +func (cmd *KeyFlagsCmd) Result() ([]KeyFlags, error) { + return cmd.val, cmd.err +} + +func (cmd *KeyFlagsCmd) String() string { + return cmdString(cmd, cmd.val) +} + +func (cmd *KeyFlagsCmd) readReply(rd *proto.Reader) error { + n, err := rd.ReadArrayLen() + if err != nil { + return err + } + + // If the n is 0, can't continue reading. + if n == 0 { + cmd.val = make([]KeyFlags, 0) + return nil + } + + typ, err := rd.PeekReplyType() + if err != nil { + return err + } + array := typ == proto.RespArray + + if array { + cmd.val = make([]KeyFlags, n) + } else { + cmd.val = make([]KeyFlags, n/2) + } + + for i := 0; i < len(cmd.val); i++ { + if array { + if err = rd.ReadFixedArrayLen(2); err != nil { + return err + } + } + + if cmd.val[i].Key, err = rd.ReadString(); err != nil { + return err + } + + flagLen, err := rd.ReadArrayLen() + if err != nil { + return err + } + for j := 0; j < flagLen; j++ { + if cmd.val[i].Flags[j], err = rd.ReadString(); err != nil { + return err + } + } + } + + return nil +} diff --git a/commands.go b/commands.go index 27f11fdb6..b5ba38c64 100644 --- a/commands.go +++ b/commands.go @@ -129,6 +129,8 @@ type Cmdable interface { Command(ctx context.Context) *CommandsInfoCmd CommandList(ctx context.Context, filter *FilterBy) *StringSliceCmd + CommandGetKeys(ctx context.Context, command string, args ...string) *StringSliceCmd + CommandGetKeysAndFlags(ctx context.Context, command string, args ...string) *KeyFlagsCmd ClientGetName(ctx context.Context) *StringCmd Echo(ctx context.Context, message interface{}) *StringCmd Ping(ctx context.Context) *StatusCmd @@ -568,6 +570,32 @@ func (c cmdable) CommandList(ctx context.Context, filter *FilterBy) *StringSlice return cmd } +func (c cmdable) CommandGetKeys(ctx context.Context, command string, cmdArgs ...string) *StringSliceCmd { + args := make([]interface{}, 3+len(cmdArgs)) + args[0] = "command" + args[1] = "getkeys" + args[2] = command + for i, cmdArg := range cmdArgs { + args[3+i] = cmdArg + } + cmd := NewStringSliceCmd(ctx, args...) + _ = c(ctx, cmd) + return cmd +} + +func (c cmdable) CommandGetKeysAndFlags(ctx context.Context, command string, cmdArgs ...string) *KeyFlagsCmd { + args := make([]interface{}, 3+len(cmdArgs)) + args[0] = "command" + args[1] = "getkeysandflags" + args[2] = command + for i, cmdArg := range cmdArgs { + args[3+i] = cmdArg + } + cmd := NewKeyFlagsCmd(ctx, args...) + _ = c(ctx, cmd) + return cmd +} + // ClientGetName returns the name of the connection. func (c cmdable) ClientGetName(ctx context.Context) *StringCmd { cmd := NewStringCmd(ctx, "client", "getname") diff --git a/commands_test.go b/commands_test.go index 05509f552..961995bd8 100644 --- a/commands_test.go +++ b/commands_test.go @@ -132,6 +132,28 @@ var _ = Describe("Commands", func() { }, "30s").Should(Equal("Background saving started")) }) + It("Should CommandGetKeys", func() { + cmd := client.CommandGetKeys(ctx, "MSET", "a", "b", "c", "d", "e", "f") + keys, err := cmd.Result() + Expect(err).NotTo(HaveOccurred()) + Expect(keys).To(Equal([]string{"a", "c", "e"})) + + cmd = client.CommandGetKeys(ctx, "EVAL", "not consulted", "3", "key1", "key2", "key3", "arg1", "arg2", "arg3", "argN") + keys, err = cmd.Result() + Expect(err).NotTo(HaveOccurred()) + Expect(keys).To(Equal([]string{"key1", "key2", "key3"})) + + cmd = client.CommandGetKeys(ctx, "SORT", "mylist", "ALPHA", "STORE", "outlist") + keys, err = cmd.Result() + Expect(err).NotTo(HaveOccurred()) + Expect(keys).To(Equal([]string{"mylist", "outlist"})) + }) + + + It("should CommandGetKeysAndFlags", func() { + // TO-DO + }) + It("should ClientKill", func() { r := client.ClientKill(ctx, "1.1.1.1:1111") Expect(r.Err()).To(MatchError("ERR No such client")) From e1fa31e4faf0c42c6925d98aceadd83e63553f48 Mon Sep 17 00:00:00 2001 From: Anuragkillswitch <70265851+Anuragkillswitch@users.noreply.github.com> Date: Fri, 24 Mar 2023 15:13:09 +0530 Subject: [PATCH 02/23] feat: adding test for CommandGetKeysAndFlags --- commands_test.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/commands_test.go b/commands_test.go index 961995bd8..26ddb7081 100644 --- a/commands_test.go +++ b/commands_test.go @@ -133,25 +133,33 @@ var _ = Describe("Commands", func() { }) It("Should CommandGetKeys", func() { - cmd := client.CommandGetKeys(ctx, "MSET", "a", "b", "c", "d", "e", "f") - keys, err := cmd.Result() + keys, err := client.CommandGetKeys(ctx, "MSET", "a", "b", "c", "d", "e", "f").Result() Expect(err).NotTo(HaveOccurred()) Expect(keys).To(Equal([]string{"a", "c", "e"})) - cmd = client.CommandGetKeys(ctx, "EVAL", "not consulted", "3", "key1", "key2", "key3", "arg1", "arg2", "arg3", "argN") - keys, err = cmd.Result() + keys, err = client.CommandGetKeys(ctx, "EVAL", "not consulted", "3", "key1", "key2", "key3", "arg1", "arg2", "arg3", "argN").Result() Expect(err).NotTo(HaveOccurred()) Expect(keys).To(Equal([]string{"key1", "key2", "key3"})) - cmd = client.CommandGetKeys(ctx, "SORT", "mylist", "ALPHA", "STORE", "outlist") - keys, err = cmd.Result() + keys, err = client.CommandGetKeys(ctx, "SORT", "mylist", "ALPHA", "STORE", "outlist").Result() Expect(err).NotTo(HaveOccurred()) Expect(keys).To(Equal([]string{"mylist", "outlist"})) }) It("should CommandGetKeysAndFlags", func() { - // TO-DO + keysAndFlags, err := client.CommandGetKeysAndFlags(ctx, "LMOVE", "mylist1", "mylist2", "left", "left").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(keysAndFlags).To(Equal([]redis.KeyFlags{ + { + Key: "mylist1", + Flags: []string{"RW", "access", "delete"}, + }, + { + Key: "mylist2", + Flags: []string{"RW", "insert"}, + }, + })) }) It("should ClientKill", func() { From 54ab53cb925e6a577fcb4a49189382d8b63f337b Mon Sep 17 00:00:00 2001 From: Anuragkillswitch <70265851+Anuragkillswitch@users.noreply.github.com> Date: Fri, 24 Mar 2023 15:25:57 +0530 Subject: [PATCH 03/23] fix: read reply for keyflags --- command.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/command.go b/command.go index 5f388fc0f..6a276e0c8 100644 --- a/command.go +++ b/command.go @@ -4244,15 +4244,25 @@ func (cmd *KeyFlagsCmd) readReply(rd *proto.Reader) error { return err } - flagLen, err := rd.ReadArrayLen() + flagsLen, err := rd.ReadArrayLen() + if err != nil { return err } - for j := 0; j < flagLen; j++ { - if cmd.val[i].Flags[j], err = rd.ReadString(); err != nil { + + cmd.val[i].Flags = make([]string, flagsLen) + + for j := 0; j < len(cmd.val[i].Flags); i++ { + switch s, err := rd.ReadString(); { + case err == Nil: + cmd.val[i].Flags[j] = "" + case err != nil: return err + default: + cmd.val[i].Flags[j] = s } } + } return nil From dcd088cd3410b47affaa10ebee37242aee7fbd0b Mon Sep 17 00:00:00 2001 From: Anuragkillswitch <70265851+Anuragkillswitch@users.noreply.github.com> Date: Fri, 24 Mar 2023 15:32:49 +0530 Subject: [PATCH 04/23] fix: test check --- commands_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/commands_test.go b/commands_test.go index 26ddb7081..0605a9c84 100644 --- a/commands_test.go +++ b/commands_test.go @@ -153,11 +153,11 @@ var _ = Describe("Commands", func() { Expect(keysAndFlags).To(Equal([]redis.KeyFlags{ { Key: "mylist1", - Flags: []string{"RW", "access", "delete"}, + Flags: []string{"RW", "", ""}, }, { - Key: "mylist2", - Flags: []string{"RW", "insert"}, + Key: "", + Flags: nil, }, })) }) From 7b0a13d1b059a5dedc9bd2afa01caecebc3d5206 Mon Sep 17 00:00:00 2001 From: Anuragkillswitch <70265851+Anuragkillswitch@users.noreply.github.com> Date: Fri, 24 Mar 2023 15:34:16 +0530 Subject: [PATCH 05/23] fix: fixing the test case --- command.go | 2 -- commands_test.go | 6 +++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/command.go b/command.go index 6a276e0c8..a54a50477 100644 --- a/command.go +++ b/command.go @@ -4254,8 +4254,6 @@ func (cmd *KeyFlagsCmd) readReply(rd *proto.Reader) error { for j := 0; j < len(cmd.val[i].Flags); i++ { switch s, err := rd.ReadString(); { - case err == Nil: - cmd.val[i].Flags[j] = "" case err != nil: return err default: diff --git a/commands_test.go b/commands_test.go index 0605a9c84..26ddb7081 100644 --- a/commands_test.go +++ b/commands_test.go @@ -153,11 +153,11 @@ var _ = Describe("Commands", func() { Expect(keysAndFlags).To(Equal([]redis.KeyFlags{ { Key: "mylist1", - Flags: []string{"RW", "", ""}, + Flags: []string{"RW", "access", "delete"}, }, { - Key: "", - Flags: nil, + Key: "mylist2", + Flags: []string{"RW", "insert"}, }, })) }) From 0213e08a2d23dd1f2d5f5594e7c6099ad2daf52d Mon Sep 17 00:00:00 2001 From: Anuragkillswitch <70265851+Anuragkillswitch@users.noreply.github.com> Date: Fri, 24 Mar 2023 16:15:46 +0530 Subject: [PATCH 06/23] fix: test --- command.go | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/command.go b/command.go index a54a50477..5e290185d 100644 --- a/command.go +++ b/command.go @@ -4215,7 +4215,7 @@ func (cmd *KeyFlagsCmd) readReply(rd *proto.Reader) error { return err } - // If the n is 0, can't continue reading. + // If n is 0, can't continue reading. if n == 0 { cmd.val = make([]KeyFlags, 0) return nil @@ -4244,24 +4244,34 @@ func (cmd *KeyFlagsCmd) readReply(rd *proto.Reader) error { return err } - flagsLen, err := rd.ReadArrayLen() + n, err := rd.ReadArrayLen() + if err != nil { + return err + } + typ, err := rd.PeekReplyType() if err != nil { return err } + array := typ == proto.RespArray - cmd.val[i].Flags = make([]string, flagsLen) + if array { + cmd.val[i].Flags = make([]string, n) + } else { + cmd.val[i].Flags = make([]string, n/2) + } - for j := 0; j < len(cmd.val[i].Flags); i++ { - switch s, err := rd.ReadString(); { - case err != nil: + for j := 0; j < len(cmd.val[i].Flags); j++ { + if array { + if err = rd.ReadFixedArrayLen(2); err != nil { + return err + } + } + + if cmd.val[i].Flags[j], err = rd.ReadString(); err != nil { return err - default: - cmd.val[i].Flags[j] = s } } - } - return nil } From 4eb7f8224b6787908ec373db246b585c45fb2355 Mon Sep 17 00:00:00 2001 From: Anuragkillswitch <70265851+Anuragkillswitch@users.noreply.github.com> Date: Fri, 24 Mar 2023 17:29:40 +0530 Subject: [PATCH 07/23] fix: fix the test case --- command.go | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/command.go b/command.go index 5e290185d..890625c64 100644 --- a/command.go +++ b/command.go @@ -4244,34 +4244,23 @@ func (cmd *KeyFlagsCmd) readReply(rd *proto.Reader) error { return err } - n, err := rd.ReadArrayLen() + flagsLen, err := rd.ReadArrayLen() if err != nil { return err } - typ, err := rd.PeekReplyType() - if err != nil { - return err - } - array := typ == proto.RespArray - - if array { - cmd.val[i].Flags = make([]string, n) - } else { - cmd.val[i].Flags = make([]string, n/2) - } + cmd.val[i].Flags = make([]string, flagsLen) - for j := 0; j < len(cmd.val[i].Flags); j++ { - if array { - if err = rd.ReadFixedArrayLen(2); err != nil { - return err - } - } - - if cmd.val[i].Flags[j], err = rd.ReadString(); err != nil { + for j := 0; j < len(cmd.val[i].Flags); i++ { + switch s, err := rd.ReadString(); { + case err != nil: return err + default: + cmd.val[i].Flags[j] = s } } + } + return nil } From c18b8d1e0514c7b3b902970ea4c3862a29954797 Mon Sep 17 00:00:00 2001 From: Anuragkillswitch <70265851+Anuragkillswitch@users.noreply.github.com> Date: Fri, 24 Mar 2023 17:51:17 +0530 Subject: [PATCH 08/23] feat: fixing reading style --- command.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/command.go b/command.go index 890625c64..bb864cc1c 100644 --- a/command.go +++ b/command.go @@ -4235,7 +4235,7 @@ func (cmd *KeyFlagsCmd) readReply(rd *proto.Reader) error { for i := 0; i < len(cmd.val); i++ { if array { - if err = rd.ReadFixedArrayLen(2); err != nil { + if err = rd.ReadFixedMapLen(2); err != nil { return err } } @@ -4251,13 +4251,12 @@ func (cmd *KeyFlagsCmd) readReply(rd *proto.Reader) error { cmd.val[i].Flags = make([]string, flagsLen) - for j := 0; j < len(cmd.val[i].Flags); i++ { - switch s, err := rd.ReadString(); { - case err != nil: - return err - default: - cmd.val[i].Flags[j] = s - } + for j := 0; j < flagsLen; j++ { + + if cmd.val[i].Flags[j] , err = rd.ReadString(); err !=nil { + return err + } + } } From 5165b599cf2a611bd4f227770bf1e0abda7e6036 Mon Sep 17 00:00:00 2001 From: Anuragkillswitch <70265851+Anuragkillswitch@users.noreply.github.com> Date: Fri, 24 Mar 2023 17:56:59 +0530 Subject: [PATCH 09/23] feat: fixing reading style --- command.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/command.go b/command.go index bb864cc1c..7a896dbd3 100644 --- a/command.go +++ b/command.go @@ -4235,7 +4235,7 @@ func (cmd *KeyFlagsCmd) readReply(rd *proto.Reader) error { for i := 0; i < len(cmd.val); i++ { if array { - if err = rd.ReadFixedMapLen(2); err != nil { + if err = rd.ReadFixedArrayLen(2); err != nil { return err } } From 8ee58a96b9a0cc6b644dc5b5647f6511ecc6ec20 Mon Sep 17 00:00:00 2001 From: Anuragkillswitch <70265851+Anuragkillswitch@users.noreply.github.com> Date: Fri, 24 Mar 2023 18:15:02 +0530 Subject: [PATCH 10/23] fix: final cleanup and check --- command.go | 13 +++---------- commands_test.go | 29 ++++++++++++++--------------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/command.go b/command.go index 7a896dbd3..541103c06 100644 --- a/command.go +++ b/command.go @@ -4215,7 +4215,6 @@ func (cmd *KeyFlagsCmd) readReply(rd *proto.Reader) error { return err } - // If n is 0, can't continue reading. if n == 0 { cmd.val = make([]KeyFlags, 0) return nil @@ -4239,26 +4238,20 @@ func (cmd *KeyFlagsCmd) readReply(rd *proto.Reader) error { return err } } - if cmd.val[i].Key, err = rd.ReadString(); err != nil { return err } - flagsLen, err := rd.ReadArrayLen() if err != nil { return err } - cmd.val[i].Flags = make([]string, flagsLen) for j := 0; j < flagsLen; j++ { - - if cmd.val[i].Flags[j] , err = rd.ReadString(); err !=nil { - return err - } - + if cmd.val[i].Flags[j], err = rd.ReadString(); err != nil { + return err + } } - } return nil diff --git a/commands_test.go b/commands_test.go index 26ddb7081..744bc4f91 100644 --- a/commands_test.go +++ b/commands_test.go @@ -136,30 +136,29 @@ var _ = Describe("Commands", func() { keys, err := client.CommandGetKeys(ctx, "MSET", "a", "b", "c", "d", "e", "f").Result() Expect(err).NotTo(HaveOccurred()) Expect(keys).To(Equal([]string{"a", "c", "e"})) - + keys, err = client.CommandGetKeys(ctx, "EVAL", "not consulted", "3", "key1", "key2", "key3", "arg1", "arg2", "arg3", "argN").Result() Expect(err).NotTo(HaveOccurred()) Expect(keys).To(Equal([]string{"key1", "key2", "key3"})) - + keys, err = client.CommandGetKeys(ctx, "SORT", "mylist", "ALPHA", "STORE", "outlist").Result() Expect(err).NotTo(HaveOccurred()) Expect(keys).To(Equal([]string{"mylist", "outlist"})) }) - It("should CommandGetKeysAndFlags", func() { - keysAndFlags, err := client.CommandGetKeysAndFlags(ctx, "LMOVE", "mylist1", "mylist2", "left", "left").Result() - Expect(err).NotTo(HaveOccurred()) - Expect(keysAndFlags).To(Equal([]redis.KeyFlags{ - { - Key: "mylist1", - Flags: []string{"RW", "access", "delete"}, - }, - { - Key: "mylist2", - Flags: []string{"RW", "insert"}, - }, - })) + keysAndFlags, err := client.CommandGetKeysAndFlags(ctx, "LMOVE", "mylist1", "mylist2", "left", "left").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(keysAndFlags).To(Equal([]redis.KeyFlags{ + { + Key: "mylist1", + Flags: []string{"RW", "access", "delete"}, + }, + { + Key: "mylist2", + Flags: []string{"RW", "insert"}, + }, + })) }) It("should ClientKill", func() { From 1fd12dd1a9a425b0a77bad744b1371cfc1529462 Mon Sep 17 00:00:00 2001 From: Anuragkillswitch <70265851+Anuragkillswitch@users.noreply.github.com> Date: Fri, 24 Mar 2023 20:36:15 +0530 Subject: [PATCH 11/23] feat: Making changes acc. to comments --- command.go | 20 +++++--------------- commands.go | 22 ++++++++-------------- 2 files changed, 13 insertions(+), 29 deletions(-) diff --git a/command.go b/command.go index 541103c06..6eba56404 100644 --- a/command.go +++ b/command.go @@ -4220,24 +4220,14 @@ func (cmd *KeyFlagsCmd) readReply(rd *proto.Reader) error { return nil } - typ, err := rd.PeekReplyType() - if err != nil { - return err - } - array := typ == proto.RespArray - - if array { - cmd.val = make([]KeyFlags, n) - } else { - cmd.val = make([]KeyFlags, n/2) - } + cmd.val = make([]KeyFlags, n) for i := 0; i < len(cmd.val); i++ { - if array { - if err = rd.ReadFixedArrayLen(2); err != nil { - return err - } + + if err = rd.ReadFixedArrayLen(2); err != nil { + return err } + if cmd.val[i].Key, err = rd.ReadString(); err != nil { return err } diff --git a/commands.go b/commands.go index b5ba38c64..792e26949 100644 --- a/commands.go +++ b/commands.go @@ -129,8 +129,8 @@ type Cmdable interface { Command(ctx context.Context) *CommandsInfoCmd CommandList(ctx context.Context, filter *FilterBy) *StringSliceCmd - CommandGetKeys(ctx context.Context, command string, args ...string) *StringSliceCmd - CommandGetKeysAndFlags(ctx context.Context, command string, args ...string) *KeyFlagsCmd + CommandGetKeys(ctx context.Context, commands ...interface{}) *StringSliceCmd + CommandGetKeysAndFlags(ctx context.Context, commands ...interface{}) *KeyFlagsCmd ClientGetName(ctx context.Context) *StringCmd Echo(ctx context.Context, message interface{}) *StringCmd Ping(ctx context.Context) *StatusCmd @@ -570,27 +570,21 @@ func (c cmdable) CommandList(ctx context.Context, filter *FilterBy) *StringSlice return cmd } -func (c cmdable) CommandGetKeys(ctx context.Context, command string, cmdArgs ...string) *StringSliceCmd { - args := make([]interface{}, 3+len(cmdArgs)) +func (c cmdable) CommandGetKeys(ctx context.Context, commands ...interface{}) *StringSliceCmd { + args := make([]interface{}, 2+len(commands)) args[0] = "command" args[1] = "getkeys" - args[2] = command - for i, cmdArg := range cmdArgs { - args[3+i] = cmdArg - } + args = append(args, commands) cmd := NewStringSliceCmd(ctx, args...) _ = c(ctx, cmd) return cmd } -func (c cmdable) CommandGetKeysAndFlags(ctx context.Context, command string, cmdArgs ...string) *KeyFlagsCmd { - args := make([]interface{}, 3+len(cmdArgs)) +func (c cmdable) CommandGetKeysAndFlags(ctx context.Context, commands ...interface{}) *KeyFlagsCmd { + args := make([]interface{}, 2+len(commands)) args[0] = "command" args[1] = "getkeysandflags" - args[2] = command - for i, cmdArg := range cmdArgs { - args[3+i] = cmdArg - } + args = append(args, commands) cmd := NewKeyFlagsCmd(ctx, args...) _ = c(ctx, cmd) return cmd From 6edc955edea9313059ce8b4a0ec60d96dd9119b2 Mon Sep 17 00:00:00 2001 From: Anuragkillswitch <70265851+Anuragkillswitch@users.noreply.github.com> Date: Fri, 24 Mar 2023 20:44:36 +0530 Subject: [PATCH 12/23] fix: read --- command.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/command.go b/command.go index 6eba56404..581b2b7d8 100644 --- a/command.go +++ b/command.go @@ -4224,10 +4224,6 @@ func (cmd *KeyFlagsCmd) readReply(rd *proto.Reader) error { for i := 0; i < len(cmd.val); i++ { - if err = rd.ReadFixedArrayLen(2); err != nil { - return err - } - if cmd.val[i].Key, err = rd.ReadString(); err != nil { return err } From c79e8b697346acfaf3dde11b8be58e06c5d40e03 Mon Sep 17 00:00:00 2001 From: Anuragkillswitch <70265851+Anuragkillswitch@users.noreply.github.com> Date: Fri, 24 Mar 2023 20:51:25 +0530 Subject: [PATCH 13/23] fix: fixing appendArgs --- commands.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/commands.go b/commands.go index 792e26949..c33d125ed 100644 --- a/commands.go +++ b/commands.go @@ -574,7 +574,7 @@ func (c cmdable) CommandGetKeys(ctx context.Context, commands ...interface{}) *S args := make([]interface{}, 2+len(commands)) args[0] = "command" args[1] = "getkeys" - args = append(args, commands) + args = appendArgs(args, commands) cmd := NewStringSliceCmd(ctx, args...) _ = c(ctx, cmd) return cmd @@ -584,7 +584,7 @@ func (c cmdable) CommandGetKeysAndFlags(ctx context.Context, commands ...interfa args := make([]interface{}, 2+len(commands)) args[0] = "command" args[1] = "getkeysandflags" - args = append(args, commands) + args = appendArgs(args, commands) cmd := NewKeyFlagsCmd(ctx, args...) _ = c(ctx, cmd) return cmd From a49ec0267c6b25c482c37f5aaceb973b2d91725e Mon Sep 17 00:00:00 2001 From: Anuragkillswitch <70265851+Anuragkillswitch@users.noreply.github.com> Date: Fri, 24 Mar 2023 20:59:22 +0530 Subject: [PATCH 14/23] fix: read --- commands.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/commands.go b/commands.go index c33d125ed..5dfc344cc 100644 --- a/commands.go +++ b/commands.go @@ -574,7 +574,7 @@ func (c cmdable) CommandGetKeys(ctx context.Context, commands ...interface{}) *S args := make([]interface{}, 2+len(commands)) args[0] = "command" args[1] = "getkeys" - args = appendArgs(args, commands) + args = append(args, commands...) cmd := NewStringSliceCmd(ctx, args...) _ = c(ctx, cmd) return cmd @@ -584,7 +584,7 @@ func (c cmdable) CommandGetKeysAndFlags(ctx context.Context, commands ...interfa args := make([]interface{}, 2+len(commands)) args[0] = "command" args[1] = "getkeysandflags" - args = appendArgs(args, commands) + args = append(args, commands...) cmd := NewKeyFlagsCmd(ctx, args...) _ = c(ctx, cmd) return cmd From 9773fc014b37f27dedce3fe494824528923411f7 Mon Sep 17 00:00:00 2001 From: Anuragkillswitch <70265851+Anuragkillswitch@users.noreply.github.com> Date: Fri, 24 Mar 2023 21:35:51 +0530 Subject: [PATCH 15/23] fix: tests fix --- commands_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/commands_test.go b/commands_test.go index 744bc4f91..a4d456f26 100644 --- a/commands_test.go +++ b/commands_test.go @@ -133,21 +133,21 @@ var _ = Describe("Commands", func() { }) It("Should CommandGetKeys", func() { - keys, err := client.CommandGetKeys(ctx, "MSET", "a", "b", "c", "d", "e", "f").Result() + keys, err := client.CommandGetKeys(ctx, "mset", "a", "b", "c", "d", "e", "f").Result() Expect(err).NotTo(HaveOccurred()) Expect(keys).To(Equal([]string{"a", "c", "e"})) - keys, err = client.CommandGetKeys(ctx, "EVAL", "not consulted", "3", "key1", "key2", "key3", "arg1", "arg2", "arg3", "argN").Result() + keys, err = client.CommandGetKeys(ctx, "eval", "not consulted", "3", "key1", "key2", "key3", "arg1", "arg2", "arg3", "argN").Result() Expect(err).NotTo(HaveOccurred()) Expect(keys).To(Equal([]string{"key1", "key2", "key3"})) - keys, err = client.CommandGetKeys(ctx, "SORT", "mylist", "ALPHA", "STORE", "outlist").Result() + keys, err = client.CommandGetKeys(ctx, "sort", "mylist", "ALPHA", "STORE", "outlist").Result() Expect(err).NotTo(HaveOccurred()) Expect(keys).To(Equal([]string{"mylist", "outlist"})) }) It("should CommandGetKeysAndFlags", func() { - keysAndFlags, err := client.CommandGetKeysAndFlags(ctx, "LMOVE", "mylist1", "mylist2", "left", "left").Result() + keysAndFlags, err := client.CommandGetKeysAndFlags(ctx, "lmove", "mylist1", "mylist2", "left", "left").Result() Expect(err).NotTo(HaveOccurred()) Expect(keysAndFlags).To(Equal([]redis.KeyFlags{ { From 43f10c77f6d2154bef608a5d305cbbf1a53ea23f Mon Sep 17 00:00:00 2001 From: Anuragkillswitch <70265851+Anuragkillswitch@users.noreply.github.com> Date: Sat, 25 Mar 2023 09:45:30 +0530 Subject: [PATCH 16/23] fix: attempt --- command.go | 4 ++++ commands.go | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/command.go b/command.go index 581b2b7d8..6eba56404 100644 --- a/command.go +++ b/command.go @@ -4224,6 +4224,10 @@ func (cmd *KeyFlagsCmd) readReply(rd *proto.Reader) error { for i := 0; i < len(cmd.val); i++ { + if err = rd.ReadFixedArrayLen(2); err != nil { + return err + } + if cmd.val[i].Key, err = rd.ReadString(); err != nil { return err } diff --git a/commands.go b/commands.go index 5dfc344cc..c33d125ed 100644 --- a/commands.go +++ b/commands.go @@ -574,7 +574,7 @@ func (c cmdable) CommandGetKeys(ctx context.Context, commands ...interface{}) *S args := make([]interface{}, 2+len(commands)) args[0] = "command" args[1] = "getkeys" - args = append(args, commands...) + args = appendArgs(args, commands) cmd := NewStringSliceCmd(ctx, args...) _ = c(ctx, cmd) return cmd @@ -584,7 +584,7 @@ func (c cmdable) CommandGetKeysAndFlags(ctx context.Context, commands ...interfa args := make([]interface{}, 2+len(commands)) args[0] = "command" args[1] = "getkeysandflags" - args = append(args, commands...) + args = appendArgs(args, commands) cmd := NewKeyFlagsCmd(ctx, args...) _ = c(ctx, cmd) return cmd From 238acf0a998ae1fbcd1b2aa168dd8fd2cc2b697c Mon Sep 17 00:00:00 2001 From: Anuragkillswitch <70265851+Anuragkillswitch@users.noreply.github.com> Date: Sat, 25 Mar 2023 09:51:59 +0530 Subject: [PATCH 17/23] reverting tests to before state --- commands_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/commands_test.go b/commands_test.go index a4d456f26..744bc4f91 100644 --- a/commands_test.go +++ b/commands_test.go @@ -133,21 +133,21 @@ var _ = Describe("Commands", func() { }) It("Should CommandGetKeys", func() { - keys, err := client.CommandGetKeys(ctx, "mset", "a", "b", "c", "d", "e", "f").Result() + keys, err := client.CommandGetKeys(ctx, "MSET", "a", "b", "c", "d", "e", "f").Result() Expect(err).NotTo(HaveOccurred()) Expect(keys).To(Equal([]string{"a", "c", "e"})) - keys, err = client.CommandGetKeys(ctx, "eval", "not consulted", "3", "key1", "key2", "key3", "arg1", "arg2", "arg3", "argN").Result() + keys, err = client.CommandGetKeys(ctx, "EVAL", "not consulted", "3", "key1", "key2", "key3", "arg1", "arg2", "arg3", "argN").Result() Expect(err).NotTo(HaveOccurred()) Expect(keys).To(Equal([]string{"key1", "key2", "key3"})) - keys, err = client.CommandGetKeys(ctx, "sort", "mylist", "ALPHA", "STORE", "outlist").Result() + keys, err = client.CommandGetKeys(ctx, "SORT", "mylist", "ALPHA", "STORE", "outlist").Result() Expect(err).NotTo(HaveOccurred()) Expect(keys).To(Equal([]string{"mylist", "outlist"})) }) It("should CommandGetKeysAndFlags", func() { - keysAndFlags, err := client.CommandGetKeysAndFlags(ctx, "lmove", "mylist1", "mylist2", "left", "left").Result() + keysAndFlags, err := client.CommandGetKeysAndFlags(ctx, "LMOVE", "mylist1", "mylist2", "left", "left").Result() Expect(err).NotTo(HaveOccurred()) Expect(keysAndFlags).To(Equal([]redis.KeyFlags{ { From 51a31e84eced1309a2932f05ba35c9021c3060ba Mon Sep 17 00:00:00 2001 From: Anuragkillswitch <70265851+Anuragkillswitch@users.noreply.github.com> Date: Sat, 25 Mar 2023 09:56:14 +0530 Subject: [PATCH 18/23] debugging --- command.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/command.go b/command.go index 6eba56404..541103c06 100644 --- a/command.go +++ b/command.go @@ -4220,14 +4220,24 @@ func (cmd *KeyFlagsCmd) readReply(rd *proto.Reader) error { return nil } - cmd.val = make([]KeyFlags, n) + typ, err := rd.PeekReplyType() + if err != nil { + return err + } + array := typ == proto.RespArray - for i := 0; i < len(cmd.val); i++ { + if array { + cmd.val = make([]KeyFlags, n) + } else { + cmd.val = make([]KeyFlags, n/2) + } - if err = rd.ReadFixedArrayLen(2); err != nil { - return err + for i := 0; i < len(cmd.val); i++ { + if array { + if err = rd.ReadFixedArrayLen(2); err != nil { + return err + } } - if cmd.val[i].Key, err = rd.ReadString(); err != nil { return err } From f5885914ffda85580b99aa53c430f8a67a6ccfc6 Mon Sep 17 00:00:00 2001 From: Anuragkillswitch <70265851+Anuragkillswitch@users.noreply.github.com> Date: Sat, 25 Mar 2023 10:11:05 +0530 Subject: [PATCH 19/23] reverting to debug --- command.go | 18 ++---------------- commands.go | 22 ++++++++++++++-------- 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/command.go b/command.go index 541103c06..581b2b7d8 100644 --- a/command.go +++ b/command.go @@ -4220,24 +4220,10 @@ func (cmd *KeyFlagsCmd) readReply(rd *proto.Reader) error { return nil } - typ, err := rd.PeekReplyType() - if err != nil { - return err - } - array := typ == proto.RespArray - - if array { - cmd.val = make([]KeyFlags, n) - } else { - cmd.val = make([]KeyFlags, n/2) - } + cmd.val = make([]KeyFlags, n) for i := 0; i < len(cmd.val); i++ { - if array { - if err = rd.ReadFixedArrayLen(2); err != nil { - return err - } - } + if cmd.val[i].Key, err = rd.ReadString(); err != nil { return err } diff --git a/commands.go b/commands.go index c33d125ed..ad9f3552d 100644 --- a/commands.go +++ b/commands.go @@ -129,8 +129,8 @@ type Cmdable interface { Command(ctx context.Context) *CommandsInfoCmd CommandList(ctx context.Context, filter *FilterBy) *StringSliceCmd - CommandGetKeys(ctx context.Context, commands ...interface{}) *StringSliceCmd - CommandGetKeysAndFlags(ctx context.Context, commands ...interface{}) *KeyFlagsCmd + CommandGetKeys(ctx context.Context, command string, args ...string) *StringSliceCmd + CommandGetKeysAndFlags(ctx context.Context, command string, args ...string) *KeyFlagsCmd ClientGetName(ctx context.Context) *StringCmd Echo(ctx context.Context, message interface{}) *StringCmd Ping(ctx context.Context) *StatusCmd @@ -570,21 +570,27 @@ func (c cmdable) CommandList(ctx context.Context, filter *FilterBy) *StringSlice return cmd } -func (c cmdable) CommandGetKeys(ctx context.Context, commands ...interface{}) *StringSliceCmd { - args := make([]interface{}, 2+len(commands)) +func (c cmdable) CommandGetKeys(ctx context.Context, command string, cmdArgs ...string) *StringSliceCmd { + args := make([]interface{}, 3+len(cmdArgs)) args[0] = "command" args[1] = "getkeys" - args = appendArgs(args, commands) + args[2] = command + for i, cmdArg := range cmdArgs { + args[3+i] = cmdArg + } cmd := NewStringSliceCmd(ctx, args...) _ = c(ctx, cmd) return cmd } -func (c cmdable) CommandGetKeysAndFlags(ctx context.Context, commands ...interface{}) *KeyFlagsCmd { - args := make([]interface{}, 2+len(commands)) +func (c cmdable) CommandGetKeysAndFlags(ctx context.Context, command string, cmdArgs ...string) *KeyFlagsCmd { + args := make([]interface{}, 3+len(cmdArgs)) args[0] = "command" args[1] = "getkeysandflags" - args = appendArgs(args, commands) + args[2] = command + for i, cmdArg := range cmdArgs { + args[3+i] = cmdArg + } cmd := NewKeyFlagsCmd(ctx, args...) _ = c(ctx, cmd) return cmd From aa25b6bcd2f2e426ab83906dafd15f24c41811a5 Mon Sep 17 00:00:00 2001 From: Anuragkillswitch <70265851+Anuragkillswitch@users.noreply.github.com> Date: Sat, 25 Mar 2023 10:18:46 +0530 Subject: [PATCH 20/23] reverting tests to before state --- command.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/command.go b/command.go index 581b2b7d8..541103c06 100644 --- a/command.go +++ b/command.go @@ -4220,10 +4220,24 @@ func (cmd *KeyFlagsCmd) readReply(rd *proto.Reader) error { return nil } - cmd.val = make([]KeyFlags, n) + typ, err := rd.PeekReplyType() + if err != nil { + return err + } + array := typ == proto.RespArray - for i := 0; i < len(cmd.val); i++ { + if array { + cmd.val = make([]KeyFlags, n) + } else { + cmd.val = make([]KeyFlags, n/2) + } + for i := 0; i < len(cmd.val); i++ { + if array { + if err = rd.ReadFixedArrayLen(2); err != nil { + return err + } + } if cmd.val[i].Key, err = rd.ReadString(); err != nil { return err } From df0bbc65914a9899e66373f94d12a5c59c8460a2 Mon Sep 17 00:00:00 2001 From: Anuragkillswitch <70265851+Anuragkillswitch@users.noreply.github.com> Date: Sat, 25 Mar 2023 10:47:33 +0530 Subject: [PATCH 21/23] migrating command --- command.go | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/command.go b/command.go index 541103c06..6eba56404 100644 --- a/command.go +++ b/command.go @@ -4220,24 +4220,14 @@ func (cmd *KeyFlagsCmd) readReply(rd *proto.Reader) error { return nil } - typ, err := rd.PeekReplyType() - if err != nil { - return err - } - array := typ == proto.RespArray - - if array { - cmd.val = make([]KeyFlags, n) - } else { - cmd.val = make([]KeyFlags, n/2) - } + cmd.val = make([]KeyFlags, n) for i := 0; i < len(cmd.val); i++ { - if array { - if err = rd.ReadFixedArrayLen(2); err != nil { - return err - } + + if err = rd.ReadFixedArrayLen(2); err != nil { + return err } + if cmd.val[i].Key, err = rd.ReadString(); err != nil { return err } From 061c074c22803856524e78524810efffb70da9f7 Mon Sep 17 00:00:00 2001 From: Anuragkillswitch <70265851+Anuragkillswitch@users.noreply.github.com> Date: Sat, 25 Mar 2023 11:06:31 +0530 Subject: [PATCH 22/23] using copy for combining interfaces --- commands.go | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/commands.go b/commands.go index ad9f3552d..200ec510e 100644 --- a/commands.go +++ b/commands.go @@ -129,8 +129,8 @@ type Cmdable interface { Command(ctx context.Context) *CommandsInfoCmd CommandList(ctx context.Context, filter *FilterBy) *StringSliceCmd - CommandGetKeys(ctx context.Context, command string, args ...string) *StringSliceCmd - CommandGetKeysAndFlags(ctx context.Context, command string, args ...string) *KeyFlagsCmd + CommandGetKeys(ctx context.Context, commands ...interface{}) *StringSliceCmd + CommandGetKeysAndFlags(ctx context.Context, commands ...interface{}) *KeyFlagsCmd ClientGetName(ctx context.Context) *StringCmd Echo(ctx context.Context, message interface{}) *StringCmd Ping(ctx context.Context) *StatusCmd @@ -570,27 +570,21 @@ func (c cmdable) CommandList(ctx context.Context, filter *FilterBy) *StringSlice return cmd } -func (c cmdable) CommandGetKeys(ctx context.Context, command string, cmdArgs ...string) *StringSliceCmd { - args := make([]interface{}, 3+len(cmdArgs)) +func (c cmdable) CommandGetKeys(ctx context.Context, commands ...interface{}) *StringSliceCmd { + args := make([]interface{}, 2+len(commands)) args[0] = "command" args[1] = "getkeys" - args[2] = command - for i, cmdArg := range cmdArgs { - args[3+i] = cmdArg - } + copy(args[2:], commands) cmd := NewStringSliceCmd(ctx, args...) _ = c(ctx, cmd) return cmd } -func (c cmdable) CommandGetKeysAndFlags(ctx context.Context, command string, cmdArgs ...string) *KeyFlagsCmd { - args := make([]interface{}, 3+len(cmdArgs)) +func (c cmdable) CommandGetKeysAndFlags(ctx context.Context, commands ...interface{}) *KeyFlagsCmd { + args := make([]interface{}, 2+len(commands)) args[0] = "command" args[1] = "getkeysandflags" - args[2] = command - for i, cmdArg := range cmdArgs { - args[3+i] = cmdArg - } + copy(args[2:], commands) cmd := NewKeyFlagsCmd(ctx, args...) _ = c(ctx, cmd) return cmd From 99a24b0e4d93fd6a3b54acf89c3b7c0bb4405cbf Mon Sep 17 00:00:00 2001 From: Anuragkillswitch <70265851+Anuragkillswitch@users.noreply.github.com> Date: Sat, 25 Mar 2023 11:25:58 +0530 Subject: [PATCH 23/23] feat: adding unhappy flows in test --- commands_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/commands_test.go b/commands_test.go index 744bc4f91..b64486095 100644 --- a/commands_test.go +++ b/commands_test.go @@ -144,6 +144,10 @@ var _ = Describe("Commands", func() { keys, err = client.CommandGetKeys(ctx, "SORT", "mylist", "ALPHA", "STORE", "outlist").Result() Expect(err).NotTo(HaveOccurred()) Expect(keys).To(Equal([]string{"mylist", "outlist"})) + + _, err = client.CommandGetKeys(ctx, "FAKECOMMAND", "arg1", "arg2").Result() + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError("ERR Invalid command specified")) }) It("should CommandGetKeysAndFlags", func() { @@ -159,6 +163,10 @@ var _ = Describe("Commands", func() { Flags: []string{"RW", "insert"}, }, })) + + _, err = client.CommandGetKeysAndFlags(ctx, "FAKECOMMAND", "arg1", "arg2").Result() + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError("ERR Invalid command specified")) }) It("should ClientKill", func() {