define redisError type

master
Jordan Orelli 10 years ago
parent 2013eb44cd
commit a1cdead607

@ -16,8 +16,6 @@ var (
type value interface { type value interface {
} }
type simpleString string
func readValue(b []byte) (value, error) { func readValue(b []byte) (value, error) {
if len(b) < 2 { if len(b) < 2 {
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")
@ -25,11 +23,33 @@ func readValue(b []byte) (value, error) {
switch b[0] { switch b[0] {
case start_string: case start_string:
return readString(b[1:]) return readString(b[1:])
case start_error:
return readError(b[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", b[0])
} }
} }
// ------------------------------------------------------------------------------
type simpleString string
func readString(b []byte) (value, error) { func readString(b []byte) (value, error) {
return simpleString(strings.Trim(string(b), "\r\n")), nil return simpleString(strings.Trim(string(b), "\r\n")), nil
} }
// ------------------------------------------------------------------------------
type redisError string
func readError(b []byte) (value, error) {
return redisError(strings.Trim(string(b), "\r\n")), nil
}

@ -4,13 +4,29 @@ import (
"testing" "testing"
) )
func TestSimpleString(t *testing.T) { var valueTests = []struct {
s, err := readValue([]byte(`+hello`)) in string
out value
}{
{"+hello", simpleString("hello")},
{"+one two", simpleString("one two")}, // intermediate space
{"+one two ", simpleString("one two ")}, // trailing space
{"+ one two", simpleString(" one two")}, // leading space
{"-hello", redisError("hello")},
{"-one two", redisError("one two")}, // intermediate space
{"-one two ", redisError("one two ")}, // trailing space
{"- one two", redisError(" one two")}, // leading space
}
func TestValues(t *testing.T) {
for _, test := range valueTests {
v, err := readValue([]byte(test.in))
if err != nil { if err != nil {
t.Errorf("bad input: %v", err) t.Errorf("failed value test: %v", err)
}
if v != test.out {
t.Errorf("expected %v, got %v", test.out, v)
} }
if s != simpleString("hello") {
t.Errorf("expected 'hello', got '%s'", s)
} }
} }

Loading…
Cancel
Save