adding some value parsing stuff
parent
c1a55b99d2
commit
2013eb44cd
@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
start_string byte = '+'
|
||||
start_error byte = '-'
|
||||
start_integer byte = ':'
|
||||
start_bulkstring byte = '$'
|
||||
start_array byte = '*'
|
||||
)
|
||||
|
||||
type value interface {
|
||||
}
|
||||
|
||||
type simpleString string
|
||||
|
||||
func readValue(b []byte) (value, error) {
|
||||
if len(b) < 2 {
|
||||
return nil, fmt.Errorf("unable to read redis protocol value: input is too small")
|
||||
}
|
||||
switch b[0] {
|
||||
case start_string:
|
||||
return readString(b[1:])
|
||||
default:
|
||||
return nil, fmt.Errorf("unable to read redis protocol value: illegal start character: %c", b[0])
|
||||
}
|
||||
}
|
||||
|
||||
func readString(b []byte) (value, error) {
|
||||
return simpleString(strings.Trim(string(b), "\r\n")), nil
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSimpleString(t *testing.T) {
|
||||
s, err := readValue([]byte(`+hello`))
|
||||
if err != nil {
|
||||
t.Errorf("bad input: %v", err)
|
||||
}
|
||||
if s != simpleString("hello") {
|
||||
t.Errorf("expected 'hello', got '%s'", s)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue