diff --git a/redcon.go b/redcon.go index a033501..54f4b75 100644 --- a/redcon.go +++ b/redcon.go @@ -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 diff --git a/redcon_test.go b/redcon_test.go index b534abd..6f87153 100644 --- a/redcon_test.go +++ b/redcon_test.go @@ -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)