define Integer type

master
Jordan Orelli 11 years ago
parent f09229431d
commit 0239debc1e

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"strconv"
"strings" "strings"
) )
@ -25,6 +26,8 @@ func readValue(b []byte) (value, error) {
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])
} }
@ -46,12 +49,14 @@ 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