Merge pull request #2 from thinxer/bulkbytes

add Conn.WriteBulkBytes
This commit is contained in:
Josh Baker 2016-08-23 06:51:19 -07:00 committed by GitHub
commit eadbf61d4c
2 changed files with 26 additions and 0 deletions

View File

@ -21,6 +21,8 @@ type Conn interface {
WriteString(str string)
// WriteBulk writes a bulk string to the client.
WriteBulk(bulk string)
// WriteBulkBytes writes bulk bytes to the client.
WriteBulkBytes(bulk []byte)
// WriteInt writes an integer to the client.
WriteInt(num int)
// WriteArray writes an array header. You must then write addtional
@ -232,6 +234,9 @@ func (c *conn) WriteString(str string) {
func (c *conn) WriteBulk(bulk string) {
c.wr.WriteBulk(bulk)
}
func (c *conn) WriteBulkBytes(bulk []byte) {
c.wr.WriteBulkBytes(bulk)
}
func (c *conn) WriteInt(num int) {
c.wr.WriteInt(num)
}
@ -486,6 +491,18 @@ func (w *writer) WriteBulk(bulk string) error {
return nil
}
func (w *writer) WriteBulkBytes(bulk []byte) error {
if w.err != nil {
return w.err
}
w.b = append(w.b, '$')
w.b = append(w.b, []byte(strconv.FormatInt(int64(len(bulk)), 10))...)
w.b = append(w.b, '\r', '\n')
w.b = append(w.b, bulk...)
w.b = append(w.b, '\r', '\n')
return nil
}
func (w *writer) Flush() error {
if w.err != nil {
return w.err

View File

@ -196,6 +196,8 @@ func TestServer(t *testing.T) {
conn.WriteInt(100)
case "bulk":
conn.WriteBulk("bulk")
case "bulkbytes":
conn.WriteBulkBytes([]byte("bulkbytes"))
case "null":
conn.WriteNull()
case "err":
@ -263,6 +265,13 @@ func TestServer(t *testing.T) {
if res != "$4\r\nbulk\r\n" {
t.Fatal("expecting bulk, got '%v'", res)
}
res, err = do("BULKBYTES\r\n")
if err != nil {
t.Fatal(err)
}
if res != "$9\r\nbulkbytes\r\n" {
t.Fatal("expecting bulkbytes, got '%v'", res)
}
res, err = do("INT\r\n")
if err != nil {
t.Fatal(err)