Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Apr 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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