Skip to content

Commit

Permalink
Reapply #22, #37, #50
Browse files Browse the repository at this point in the history
  • Loading branch information
greedy52 committed Oct 1, 2024
1 parent c8e9f84 commit 008736a
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 4 deletions.
2 changes: 1 addition & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ func (cmd *Cmd) readReply(rd *proto.Reader) (err error) {
}

func (cmd *Cmd) ReadReply(rd *proto.Reader) (err error) {
cmd.val, err = rd.ReadReply(sliceParser)
cmd.val, err = rd.ReadReply()
return err
}

Expand Down
5 changes: 4 additions & 1 deletion internal/proto/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ const (
RespPush = '>' // ><len>\r\n... (same as Array)
)

// StatusString is the read golang type of the RespStatus.
type StatusString string

// Not used temporarily.
// Redis has not used these two data types for the time being, and will implement them later.
// Streamed = "EOF:"
Expand Down Expand Up @@ -159,7 +162,7 @@ func (r *Reader) ReadReply() (interface{}, error) {

switch line[0] {
case RespStatus:
return string(line[1:]), nil
return StatusString(line[1:]), nil
case RespInt:
return util.ParseInt(line[1:], 10, 64)
case RespFloat:
Expand Down
14 changes: 14 additions & 0 deletions internal/proto/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ func (w *Writer) WriteArg(v interface{}) error {
return w.string(v)
case *string:
return w.string(*v)
case StatusString:
return w.status(v)
case []byte:
return w.bytes(v)
case int:
Expand Down Expand Up @@ -166,6 +168,18 @@ func (w *Writer) bytes(b []byte) error {
return w.crlf()
}

func (w *Writer) status(s StatusString) error {
if err := w.WriteByte(RespStatus); err != nil {
return err
}

if _, err := w.Write([]byte(s)); err != nil {
return err
}

return w.crlf()
}

func (w *Writer) string(s string) error {
return w.bytes(util.StringToBytes(s))
}
Expand Down
27 changes: 27 additions & 0 deletions internal/proto/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,30 @@ var _ = Describe("WriteArg", func() {
})
}
})

func TestWriteStatus(t *testing.T) {
inputStatusBytes := []byte("+status\r\n")

// Read it.
reader := proto.NewReader(bytes.NewReader(inputStatusBytes))
readStatus, err := reader.ReadReply()
if err != nil {
t.Errorf("Failed to ReadReply: %v", err)
}

if readStatus != proto.StatusString("status") {
t.Errorf("expect read %v but got %v", "status", readStatus)
}

// Write it.
outputStatusBytes := new(bytes.Buffer)
writer := proto.NewWriter(outputStatusBytes)
err = writer.WriteArg(readStatus)
if err != nil {
t.Errorf("Failed to WriteArg: %v", err)
}

if string(inputStatusBytes) != outputStatusBytes.String() {
t.Errorf("expect written %v but got %v", string(inputStatusBytes), outputStatusBytes.String())
}
}
2 changes: 1 addition & 1 deletion proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"io"

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

type Reader = proto.Reader
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 @@ -13,6 +13,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 @@ -224,7 +225,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 008736a

Please sign in to comment.