Skip to content

Commit

Permalink
Fix an issue status type (+) is not read/write properly (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
greedy52 authored Dec 14, 2022
1 parent d5ec4a6 commit 8ff76d5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
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 @@ -69,6 +69,8 @@ func (w *Writer) WriteArg(v interface{}) error {
return w.string("")
case string:
return w.string(v)
case StatusString:
return w.status(v)
case []byte:
return w.bytes(v)
case int:
Expand Down Expand Up @@ -135,6 +137,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 @@ -100,3 +100,30 @@ func BenchmarkWriteBuffer_Append(b *testing.B) {
}
}
}

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())
}
}

0 comments on commit 8ff76d5

Please sign in to comment.