define Integer type

master
Jordan Orelli 10 years ago
parent f09229431d
commit 0239debc1e

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"strconv"
"strings" "strings"
) )
@ -23,8 +24,10 @@ 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: case start_error:
return readError(b[1:]) return readError(b[1:])
case start_integer:
return readInteger(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])
} }
@ -43,15 +46,17 @@ func readString(b []byte) (value, error) {
type Error string type Error string
func readError(b []byte) (value, error) { func readError(b []byte) (value, error) {
return Error(strings.Trim(string(b), "\r\n")), nil return Error(strings.Trim(string(b), "\r\n")), nil
} }
// ------------------------------------------------------------------------------
type Integer int64
func readInteger(b []byte) (value, error) {
i, err := strconv.ParseInt(strings.Trim(string(b), "\r\n"), 10, 64)
if err != nil {
return nil, fmt.Errorf("unable to read integer in redis protocol format: %v", err)
}
return Integer(i), nil
}

@ -17,6 +17,14 @@ var valueTests = []struct {
{"-one two", Error("one two")}, // intermediate space {"-one two", Error("one two")}, // intermediate space
{"-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", Integer(0)},
{":1", Integer(1)},
{":-1", Integer(-1)},
{":12345", Integer(12345)},
{":-12345", Integer(-12345)},
{":9223372036854775807", Integer(9223372036854775807)}, // int64 max
{":-9223372036854775808", Integer(-9223372036854775808)}, // int64 min
} }
func TestValues(t *testing.T) { func TestValues(t *testing.T) {

Loading…
Cancel
Save