read bulkstrings

master
Jordan Orelli 10 years ago
parent fb6dff1267
commit 2377ea6078

@ -48,7 +48,7 @@ func readValue(r io.Reader) (value, error) {
case start_integer: case start_integer:
return readInteger(line[1:]) return readInteger(line[1:])
case start_bulkstring: case start_bulkstring:
return readBulkString(line[1:], r) return readBulkString(line[1:], br)
default: default:
return nil, fmt.Errorf("unable to read redis protocol value: illegal start character: %c", line[0]) return nil, fmt.Errorf("unable to read redis protocol value: illegal start character: %c", line[0])
} }
@ -87,5 +87,23 @@ func readInteger(b []byte) (value, error) {
type BulkString string type BulkString string
func readBulkString(prefix []byte, r io.Reader) (value, error) { func readBulkString(prefix []byte, r io.Reader) (value, error) {
return nil, nil n, err := strconv.ParseInt(string(prefix), 10, 64)
if err != nil {
return nil, fmt.Errorf("unable to read bulkstring in redis protocol: bad prefix: %v", err)
}
b := make([]byte, n)
n_read, err := r.Read(b)
switch err {
case io.EOF, nil:
break
default:
return nil, fmt.Errorf("unable to read bulkstring in redis protocol: error on read: %v", err)
}
if int64(n_read) != n {
return nil, fmt.Errorf("unable to read bulkstring in redis protocol: read %d bytes, expected to read %d bytes", int64(n_read), n)
}
return BulkString(b), nil
} }

@ -31,6 +31,11 @@ var valueTests = []valueTest{
{"-one two ", Error("one two ")}, // trailing space {"-one two ", Error("one two ")}, // trailing space
{"- one two", Error(" one two")}, // leading space {"- one two", Error(" one two")}, // leading space
{"$0\r\n\r\n", BulkString("")}, // is this even a thing?
{"$1\r\nx\r\n", BulkString("x")},
{"$4\r\netsy\r\n", BulkString("etsy")},
{"$12\r\nSaskatchewan\r\n", BulkString("Saskatchewan")},
{":0", Integer(0)}, {":0", Integer(0)},
{":1", Integer(1)}, {":1", Integer(1)},
{":-1", Integer(-1)}, {":-1", Integer(-1)},

Loading…
Cancel
Save