Skip to content

Commit

Permalink
Enable StatusString for all readers (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
greedy52 authored Nov 27, 2023
1 parent 47d561e commit bd8aa68
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 25 deletions.
26 changes: 4 additions & 22 deletions internal/proto/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,13 @@ func ParseErrorReply(line []byte) error {

//------------------------------------------------------------------------------

type readerOpt struct {
// useStatusStringType uses `StatusString` type instead of string for RespStatus.
useStatusStringType bool
}

func ReaderOptUseStatusStringType(o *readerOpt) {
o.useStatusStringType = true
}

type Reader struct {
rd *bufio.Reader
opt readerOpt
rd *bufio.Reader
}

func NewReader(rd io.Reader, opts ...func(o *readerOpt)) *Reader {
opt := readerOpt{}
for _, applyOpt := range opts {
applyOpt(&opt)
}
func NewReader(rd io.Reader) *Reader {
return &Reader{
rd: bufio.NewReader(rd),
opt: opt,
rd: bufio.NewReader(rd),
}
}

Expand Down Expand Up @@ -177,10 +162,7 @@ func (r *Reader) ReadReply() (interface{}, error) {

switch line[0] {
case RespStatus:
if r.opt.useStatusStringType {
return StatusString(line[1:]), nil
}
return string(line[1:]), nil
return StatusString(line[1:]), nil
case RespInt:
return util.ParseInt(line[1:], 10, 64)
case RespFloat:
Expand Down
2 changes: 1 addition & 1 deletion internal/proto/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestWriteStatus(t *testing.T) {
inputStatusBytes := []byte("+status\r\n")

// Read it.
reader := proto.NewReader(bytes.NewReader(inputStatusBytes), proto.ReaderOptUseStatusStringType)
reader := proto.NewReader(bytes.NewReader(inputStatusBytes))
readStatus, err := reader.ReadReply()
if err != nil {
t.Errorf("Failed to ReadReply: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const RespArray = proto.RespArray
const RespInt = proto.RespInt

func NewReader(rd io.Reader) *Reader {
return proto.NewReader(rd, proto.ReaderOptUseStatusStringType)
return proto.NewReader(rd)
}

func NewWriter(wr *bytes.Buffer) *Writer {
Expand Down
4 changes: 4 additions & 0 deletions pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ func (p *Pong) String() string {

func (c *PubSub) newMessage(reply interface{}) (interface{}, error) {
switch reply := reply.(type) {
case proto.StatusString:
return &Pong{
Payload: string(reply),
}, nil
case string:
return &Pong{
Payload: reply,
Expand Down
3 changes: 2 additions & 1 deletion redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
. "github.com/bsm/gomega"

"github.com/redis/go-redis/v9"
"github.com/redis/go-redis/v9/internal/proto"
)

type redisHookError struct{}
Expand Down Expand Up @@ -192,7 +193,7 @@ var _ = Describe("Client", func() {
Expect(client.Echo(ctx, "hello").Err()).NotTo(HaveOccurred())

Expect(cmd.Err()).NotTo(HaveOccurred())
Expect(cmd.Val()).To(Equal("PONG"))
Expect(cmd.Val()).To(Equal(proto.StatusString("PONG")))
})

It("should retry command on network error", func() {
Expand Down

0 comments on commit bd8aa68

Please sign in to comment.