Skip to content

Commit a388a63

Browse files
authored
feat: check pipeline.Do to prevent confusion with Exec (#2517)
Signed-off-by: monkey92t <[email protected]>
1 parent e96c7b5 commit a388a63

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

pipeline.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package redis
22

33
import (
44
"context"
5+
"errors"
56
)
67

78
type pipelineExecer func(context.Context, []Cmder) error
@@ -21,10 +22,21 @@ type pipelineExecer func(context.Context, []Cmder) error
2122
// depends of your batch size and/or use TxPipeline.
2223
type Pipeliner interface {
2324
StatefulCmdable
25+
26+
// Len is to obtain the number of commands in the pipeline that have not yet been executed.
2427
Len() int
28+
29+
// Do is an API for executing any command.
30+
// If a certain Redis command is not yet supported, you can use Do to execute it.
2531
Do(ctx context.Context, args ...interface{}) *Cmd
32+
33+
// Process is to put the commands to be executed into the pipeline buffer.
2634
Process(ctx context.Context, cmd Cmder) error
35+
36+
// Discard is to discard all commands in the cache that have not yet been executed.
2737
Discard()
38+
39+
// Exec is to send all the commands buffered in the pipeline to the redis-server.
2840
Exec(ctx context.Context) ([]Cmder, error)
2941
}
3042

@@ -54,6 +66,10 @@ func (c *Pipeline) Len() int {
5466
// Do queues the custom command for later execution.
5567
func (c *Pipeline) Do(ctx context.Context, args ...interface{}) *Cmd {
5668
cmd := NewCmd(ctx, args...)
69+
if len(args) == 0 {
70+
cmd.SetErr(errors.New("redis: please enter the command to be executed"))
71+
return cmd
72+
}
5773
_ = c.Process(ctx, cmd)
5874
return cmd
5975
}

pipeline_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package redis_test
22

33
import (
4+
"errors"
45
"strconv"
56

67
. "github.com/bsm/ginkgo/v2"
@@ -84,6 +85,11 @@ var _ = Describe("pipelining", func() {
8485
}
8586
}
8687
})
88+
89+
It("should Exec, not Do", func() {
90+
err := pipe.Do(ctx).Err()
91+
Expect(err).To(Equal(errors.New("redis: please enter the command to be executed")))
92+
})
8793
}
8894

8995
Describe("Pipeline", func() {

0 commit comments

Comments
 (0)