Skip to content

Commit

Permalink
feat: check pipeline.Do to prevent confusion with Exec (redis#2517)
Browse files Browse the repository at this point in the history
Signed-off-by: monkey92t <[email protected]>
  • Loading branch information
monkey92t authored Apr 1, 2023
1 parent e96c7b5 commit a388a63
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package redis

import (
"context"
"errors"
)

type pipelineExecer func(context.Context, []Cmder) error
Expand All @@ -21,10 +22,21 @@ type pipelineExecer func(context.Context, []Cmder) error
// depends of your batch size and/or use TxPipeline.
type Pipeliner interface {
StatefulCmdable

// Len is to obtain the number of commands in the pipeline that have not yet been executed.
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

// Process is to put the commands to be executed into the pipeline buffer.
Process(ctx context.Context, cmd Cmder) error

// Discard is to discard all commands in the cache that have not yet been executed.
Discard()

// Exec is to send all the commands buffered in the pipeline to the redis-server.
Exec(ctx context.Context) ([]Cmder, error)
}

Expand Down Expand Up @@ -54,6 +66,10 @@ func (c *Pipeline) Len() int {
// Do queues the custom command for later execution.
func (c *Pipeline) Do(ctx context.Context, args ...interface{}) *Cmd {
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)
return cmd
}
Expand Down
6 changes: 6 additions & 0 deletions pipeline_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package redis_test

import (
"errors"
"strconv"

. "github.com/bsm/ginkgo/v2"
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit a388a63

Please sign in to comment.