feat: check pipeline.Do to prevent confusion with Exec (#2517)

Signed-off-by: monkey92t <golang@88.com>
This commit is contained in:
Monkey 2023-04-01 14:44:06 +08:00 committed by GitHub
parent e96c7b5f58
commit a388a637ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package redis
import ( import (
"context" "context"
"errors"
) )
type pipelineExecer func(context.Context, []Cmder) error type pipelineExecer func(context.Context, []Cmder) error
@ -21,10 +22,21 @@ type pipelineExecer func(context.Context, []Cmder) error
// depends of your batch size and/or use TxPipeline. // depends of your batch size and/or use TxPipeline.
type Pipeliner interface { type Pipeliner interface {
StatefulCmdable StatefulCmdable
// Len is to obtain the number of commands in the pipeline that have not yet been executed.
Len() int Len() int
// Do is an API for executing any command.
// If a certain Redis command is not yet supported, you can use Do to execute it.
Do(ctx context.Context, args ...interface{}) *Cmd Do(ctx context.Context, args ...interface{}) *Cmd
// Process is to put the commands to be executed into the pipeline buffer.
Process(ctx context.Context, cmd Cmder) error Process(ctx context.Context, cmd Cmder) error
// Discard is to discard all commands in the cache that have not yet been executed.
Discard() Discard()
// Exec is to send all the commands buffered in the pipeline to the redis-server.
Exec(ctx context.Context) ([]Cmder, error) Exec(ctx context.Context) ([]Cmder, error)
} }
@ -54,6 +66,10 @@ func (c *Pipeline) Len() int {
// Do queues the custom command for later execution. // Do queues the custom command for later execution.
func (c *Pipeline) Do(ctx context.Context, args ...interface{}) *Cmd { func (c *Pipeline) Do(ctx context.Context, args ...interface{}) *Cmd {
cmd := NewCmd(ctx, args...) cmd := NewCmd(ctx, args...)
if len(args) == 0 {
cmd.SetErr(errors.New("redis: please enter the command to be executed"))
return cmd
}
_ = c.Process(ctx, cmd) _ = c.Process(ctx, cmd)
return cmd return cmd
} }

View File

@ -1,6 +1,7 @@
package redis_test package redis_test
import ( import (
"errors"
"strconv" "strconv"
. "github.com/bsm/ginkgo/v2" . "github.com/bsm/ginkgo/v2"
@ -84,6 +85,11 @@ var _ = Describe("pipelining", func() {
} }
} }
}) })
It("should Exec, not Do", func() {
err := pipe.Do(ctx).Err()
Expect(err).To(Equal(errors.New("redis: please enter the command to be executed")))
})
} }
Describe("Pipeline", func() { Describe("Pipeline", func() {