|
|
|
@ -16,6 +16,7 @@ var (
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type value interface {
|
|
|
|
|
Write(io.Writer) (int, error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func streamValues(r io.Reader, c chan value, e chan error) {
|
|
|
|
@ -36,6 +37,12 @@ func streamValues(r io.Reader, c chan value, e chan error) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func writeValues(w io.Writer, c chan value) {
|
|
|
|
|
for v := range c {
|
|
|
|
|
v.Write(w)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func readValue(r io.Reader) (value, error) {
|
|
|
|
|
var br *bufio.Reader
|
|
|
|
|
switch t := r.(type) {
|
|
|
|
@ -89,6 +96,10 @@ func readString(b []byte) (value, error) {
|
|
|
|
|
return String(b), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s String) Write(w io.Writer) (int, error) {
|
|
|
|
|
return fmt.Fprintf(w, "+%s\r\n", s)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
type Error string
|
|
|
|
@ -97,6 +108,10 @@ func readError(b []byte) (value, error) {
|
|
|
|
|
return Error(b), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e Error) Write(w io.Writer) (int, error) {
|
|
|
|
|
return fmt.Fprintf(w, "-%s\r\n")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
type Integer int64
|
|
|
|
@ -109,6 +124,10 @@ func readInteger(b []byte) (value, error) {
|
|
|
|
|
return Integer(i), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i Integer) Write(w io.Writer) (int, error) {
|
|
|
|
|
return fmt.Fprintf(w, ":%d\r\n", i)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
type BulkString string
|
|
|
|
@ -147,6 +166,10 @@ func readBulkString(prefix []byte, r io.Reader) (value, error) {
|
|
|
|
|
return BulkString(b[:len(b)-2]), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s BulkString) Write(w io.Writer) (int, error) {
|
|
|
|
|
return fmt.Fprintf(w, "$%d\r\n%s\r\n", len(s), s)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
type Array []value
|
|
|
|
@ -174,3 +197,23 @@ func readArray(prefix []byte, r *bufio.Reader) (value, error) {
|
|
|
|
|
}
|
|
|
|
|
return a, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a Array) Write(w io.Writer) (int, error) {
|
|
|
|
|
n, err := fmt.Fprintf(w, "*%d\r\n", len(a))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return n, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
nn int
|
|
|
|
|
e error
|
|
|
|
|
)
|
|
|
|
|
for i := 0; i < len(a); i++ {
|
|
|
|
|
nn, e = a[i].Write(w)
|
|
|
|
|
n += nn
|
|
|
|
|
if e != nil {
|
|
|
|
|
return n, e
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return n, nil
|
|
|
|
|
}
|
|
|
|
|