integers are now []byte

master
Jordan Orelli 11 years ago
parent 1841de74e7
commit fa895b37d6

@ -108,8 +108,7 @@ func readValue(r io.Reader) (value, error) {
case start_error:
return ErrorVal(line), nil
case start_integer:
line = line[:len(line)-2]
return Integer(line[1:]), nil
return IntVal(line), nil
case start_bulkstring:
line = line[:len(line)-2]
return readBulkString(line[1:], br)
@ -131,8 +130,8 @@ func (s StringVal) Write(w io.Writer) (int, error) {
func String(s string) value {
b := make(StringVal, len(s)+3)
b[0] = '+'
copy(b[1:], []byte(s))
b[0] = start_string
copy(b[1:], s)
b[len(b)-2] = '\r'
b[len(b)-1] = '\n'
return b
@ -143,17 +142,13 @@ func String(s string) value {
type ErrorVal []byte
func (e ErrorVal) Write(w io.Writer) (int, error) {
// w.Write([]byte{'-'})
// w.Write([]byte(e))
// w.Write([]byte{'\r', '\n'})
// return 0, nil
return w.Write(e)
}
func Error(s string) value {
b := make(ErrorVal, len(s)+3)
b[0] = '-'
copy(b[1:], []byte(s))
b[0] = start_error
copy(b[1:], s)
b[len(b)-2] = '\r'
b[len(b)-1] = '\n'
return b
@ -161,13 +156,20 @@ func Error(s string) value {
// ------------------------------------------------------------------------------
type Integer []byte
type IntVal []byte
func (i Integer) Write(w io.Writer) (int, error) {
w.Write([]byte{':'})
w.Write(i)
w.Write([]byte{'\r', '\n'})
return 0, nil
func (i IntVal) Write(w io.Writer) (int, error) {
return w.Write(i)
}
func Int(i int) value {
s := strconv.Itoa(i)
b := make(ErrorVal, len(s)+3)
b[0] = start_integer
copy(b[1:], s)
b[len(b)-2] = '\r'
b[len(b)-1] = '\n'
return b
}
// ------------------------------------------------------------------------------

@ -56,13 +56,13 @@ var valueTests = []valueTest{
{"$4\r\netsy\r\n", BulkString("etsy")},
{"$12\r\nSaskatchewan\r\n", BulkString("Saskatchewan")},
// {":0", Integer(0)},
// {":1", Integer(1)},
// {":-1", Integer(-1)},
// {":12345", Integer(12345)},
// {":-12345", Integer(-12345)},
// {":9223372036854775807", Integer(9223372036854775807)}, // int64 max
// {":-9223372036854775808", Integer(-9223372036854775808)}, // int64 min
{":0\r\n", Int(0)},
{":1\r\n", Int(1)},
{":-1\r\n", Int(-1)},
{":12345\r\n", Int(12345)},
{":-12345\r\n", Int(-12345)},
{":9223372036854775807\r\n", Int(9223372036854775807)}, // int64 max
{":-9223372036854775808\r\n", Int(-9223372036854775808)}, // int64 min
{"+hello\r\n+extra\r\n", String("hello")},
{"+one two\r\n+extra\r\n", String("one two")}, // intermediate space
@ -74,13 +74,13 @@ var valueTests = []valueTest{
{"-one two \r\n+extra\r\n", Error("one two ")}, // trailing space
{"- one two\r\n+extra\r\n", Error(" one two")}, // leading space
// {":0\r\n+extra\r\n", Integer(0)},
// {":1\r\n+extra\r\n", Integer(1)},
// {":-1\r\n+extra\r\n", Integer(-1)},
// {":12345\r\n+extra\r\n", Integer(12345)},
// {":-12345\r\n+extra\r\n", Integer(-12345)},
// {":9223372036854775807\r\n+extra\r\n", Integer(9223372036854775807)}, // int64 max
// {":-9223372036854775808\r\n+extra\r\n", Integer(-9223372036854775808)}, // int64 min
{":0\r\n+extra\r\n", Int(0)},
{":1\r\n+extra\r\n", Int(1)},
{":-1\r\n+extra\r\n", Int(-1)},
{":12345\r\n+extra\r\n", Int(12345)},
{":-12345\r\n+extra\r\n", Int(-12345)},
{":9223372036854775807\r\n+extra\r\n", Int(9223372036854775807)}, // int64 max
{":-9223372036854775808\r\n+extra\r\n", Int(-9223372036854775808)}, // int64 min
{"*-1\r\n", nil}, // nil array
// {"*0\r\n", Array{}}, // is this a thing? I have no idea.
@ -100,7 +100,7 @@ type streamTest []interface{}
var streamTests = []streamTest{
{"+hello\r\n", String("hello")},
// {":1\r\n:2\r\n:3\r\n", Integer(1), Integer(2), Integer(3)},
{":1\r\n:2\r\n:3\r\n", Int(1), Int(2), Int(3)},
// {"*0\r\n", Array{}},
// {"*1\r\n+one\r\n", Array{String("one")}},
// {"*2\r\n+one\r\n+two\r\n", Array{String("one"), String("two")}},

Loading…
Cancel
Save