refactor: change ListElementCmd to KeyValuesCmd. (#2443)

* refactor: change ListElementCmd to KeyValuesCmd

Signed-off-by: monkey92t <golang@88.com>

* KeyValuesCmd.val are modified to pointers

Signed-off-by: monkey92t <golang@88.com>

* recover KeyValuesCmd API

Signed-off-by: monkey92t <golang@88.com>

---------

Signed-off-by: monkey92t <golang@88.com>
This commit is contained in:
Monkey 2023-02-14 21:52:56 +08:00 committed by GitHub
parent 3532f2a414
commit d2c53bd2a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 20 deletions

View File

@ -3693,17 +3693,17 @@ func (cmd *MapStringStringSliceCmd) readReply(rd *proto.Reader) error {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
type ListElementCmd struct { type KeyValuesCmd struct {
baseCmd baseCmd
key string key string
val []string val []string
} }
var _ Cmder = (*ListElementCmd)(nil) var _ Cmder = (*KeyValuesCmd)(nil)
func NewListElementCmd(ctx context.Context, args ...interface{}) *ListElementCmd { func NewKeyValuesCmd(ctx context.Context, args ...interface{}) *KeyValuesCmd {
return &ListElementCmd{ return &KeyValuesCmd{
baseCmd: baseCmd{ baseCmd: baseCmd{
ctx: ctx, ctx: ctx,
args: args, args: args,
@ -3711,24 +3711,24 @@ func NewListElementCmd(ctx context.Context, args ...interface{}) *ListElementCmd
} }
} }
func (cmd *ListElementCmd) SetVal(key string, val []string) { func (cmd *KeyValuesCmd) SetVal(key string, val []string) {
cmd.key = key cmd.key = key
cmd.val = val cmd.val = val
} }
func (cmd *ListElementCmd) Val() (string, []string) { func (cmd *KeyValuesCmd) Val() (string, []string) {
return cmd.key, cmd.val return cmd.key, cmd.val
} }
func (cmd *ListElementCmd) Result() (string, []string, error) { func (cmd *KeyValuesCmd) Result() (string, []string, error) {
return cmd.key, cmd.val, cmd.err return cmd.key, cmd.val, cmd.err
} }
func (cmd *ListElementCmd) String() string { func (cmd *KeyValuesCmd) String() string {
return cmdString(cmd, cmd.val) return cmdString(cmd, cmd.val)
} }
func (cmd *ListElementCmd) readReply(rd *proto.Reader) (err error) { func (cmd *KeyValuesCmd) readReply(rd *proto.Reader) (err error) {
if err = rd.ReadFixedArrayLen(2); err != nil { if err = rd.ReadFixedArrayLen(2); err != nil {
return err return err
} }

View File

@ -225,7 +225,7 @@ type Cmdable interface {
LInsertBefore(ctx context.Context, key string, pivot, value interface{}) *IntCmd LInsertBefore(ctx context.Context, key string, pivot, value interface{}) *IntCmd
LInsertAfter(ctx context.Context, key string, pivot, value interface{}) *IntCmd LInsertAfter(ctx context.Context, key string, pivot, value interface{}) *IntCmd
LLen(ctx context.Context, key string) *IntCmd LLen(ctx context.Context, key string) *IntCmd
LMPop(ctx context.Context, direction string, count int64, keys ...string) *ListElementCmd LMPop(ctx context.Context, direction string, count int64, keys ...string) *KeyValuesCmd
LPop(ctx context.Context, key string) *StringCmd LPop(ctx context.Context, key string) *StringCmd
LPopCount(ctx context.Context, key string, count int) *StringSliceCmd LPopCount(ctx context.Context, key string, count int) *StringSliceCmd
LPos(ctx context.Context, key string, value string, args LPosArgs) *IntCmd LPos(ctx context.Context, key string, value string, args LPosArgs) *IntCmd
@ -1467,7 +1467,7 @@ func (c cmdable) LIndex(ctx context.Context, key string, index int64) *StringCmd
// LMPop Pops one or more elements from the first non-empty list key from the list of provided key names. // LMPop Pops one or more elements from the first non-empty list key from the list of provided key names.
// direction: left or right, count: > 0 // direction: left or right, count: > 0
// example: client.LMPop(ctx, "left", 3, "key1", "key2") // example: client.LMPop(ctx, "left", 3, "key1", "key2")
func (c cmdable) LMPop(ctx context.Context, direction string, count int64, keys ...string) *ListElementCmd { func (c cmdable) LMPop(ctx context.Context, direction string, count int64, keys ...string) *KeyValuesCmd {
args := make([]interface{}, 2+len(keys), 5+len(keys)) args := make([]interface{}, 2+len(keys), 5+len(keys))
args[0] = "lmpop" args[0] = "lmpop"
args[1] = len(keys) args[1] = len(keys)
@ -1475,7 +1475,7 @@ func (c cmdable) LMPop(ctx context.Context, direction string, count int64, keys
args[2+i] = key args[2+i] = key
} }
args = append(args, strings.ToLower(direction), "count", count) args = append(args, strings.ToLower(direction), "count", count)
cmd := NewListElementCmd(ctx, args...) cmd := NewKeyValuesCmd(ctx, args...)
_ = c(ctx, cmd) _ = c(ctx, cmd)
return cmd return cmd
} }

View File

@ -2279,25 +2279,25 @@ var _ = Describe("Commands", func() {
err = client.LPush(ctx, "list2", "a", "b", "c", "d", "e").Err() err = client.LPush(ctx, "list2", "a", "b", "c", "d", "e").Err()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
key, elems, err := client.LMPop(ctx, "left", 3, "list1", "list2").Result() key, val, err := client.LMPop(ctx, "left", 3, "list1", "list2").Result()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(key).To(Equal("list1")) Expect(key).To(Equal("list1"))
Expect(elems).To(Equal([]string{"five", "four", "three"})) Expect(val).To(Equal([]string{"five", "four", "three"}))
key, elems, err = client.LMPop(ctx, "right", 3, "list1", "list2").Result() key, val, err = client.LMPop(ctx, "right", 3, "list1", "list2").Result()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(key).To(Equal("list1")) Expect(key).To(Equal("list1"))
Expect(elems).To(Equal([]string{"one", "two"})) Expect(val).To(Equal([]string{"one", "two"}))
key, elems, err = client.LMPop(ctx, "left", 1, "list1", "list2").Result() key, val, err = client.LMPop(ctx, "left", 1, "list1", "list2").Result()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(key).To(Equal("list2")) Expect(key).To(Equal("list2"))
Expect(elems).To(Equal([]string{"e"})) Expect(val).To(Equal([]string{"e"}))
key, elems, err = client.LMPop(ctx, "right", 10, "list1", "list2").Result() key, val, err = client.LMPop(ctx, "right", 10, "list1", "list2").Result()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(key).To(Equal("list2")) Expect(key).To(Equal("list2"))
Expect(elems).To(Equal([]string{"a", "b", "c", "d"})) Expect(val).To(Equal([]string{"a", "b", "c", "d"}))
err = client.LMPop(ctx, "left", 10, "list1", "list2").Err() err = client.LMPop(ctx, "left", 10, "list1", "list2").Err()
Expect(err).To(Equal(redis.Nil)) Expect(err).To(Equal(redis.Nil))