readvalue should take an io.Reader

master
Jordan Orelli 10 years ago
parent d9a64b9e50
commit a1dcee13f7

@ -1,7 +1,9 @@
package main package main
import ( import (
"bufio"
"fmt" "fmt"
"io"
"strconv" "strconv"
"strings" "strings"
) )
@ -17,19 +19,37 @@ var (
type value interface { type value interface {
} }
func readValue(b []byte) (value, error) { func readValue(r io.Reader) (value, error) {
if len(b) < 2 { br := bufio.NewReader(r)
line, err := br.ReadBytes('\n')
switch err {
case io.EOF:
if line != nil {
break
}
return nil, err
case nil:
break
default:
return nil, fmt.Errorf("unable to read value in redis protocol: %v")
}
if len(line) < 3 {
return nil, fmt.Errorf("unable to read redis protocol value: input is too small") return nil, fmt.Errorf("unable to read redis protocol value: input is too small")
} }
switch b[0] { if line[len(line)-2] != '\r' {
return nil, fmt.Errorf("unable to read redis protocol value: bad line terminator")
}
line = line[:len(line)-2]
switch line[0] {
case start_string: case start_string:
return readString(b[1:]) return readString(line[1:])
case start_error: case start_error:
return readError(b[1:]) return readError(line[1:])
case start_integer: case start_integer:
return readInteger(b[1:]) return readInteger(line[1:])
default: default:
return nil, fmt.Errorf("unable to read redis protocol value: illegal start character: %c", b[0]) return nil, fmt.Errorf("unable to read redis protocol value: illegal start character: %c", line[0])
} }
} }

@ -1,6 +1,7 @@
package main package main
import ( import (
"strings"
"testing" "testing"
) )
@ -10,7 +11,7 @@ type valueTest struct {
} }
func (test valueTest) run(t *testing.T) { func (test valueTest) run(t *testing.T) {
v, err := readValue([]byte(test.in)) v, err := readValue(strings.NewReader(test.in + "\r\n"))
if err != nil { if err != nil {
t.Errorf("valueTest error: %v", err) t.Errorf("valueTest error: %v", err)
} }

Loading…
Cancel
Save