Skip to content

Commit

Permalink
Convert bytes to string in Cmd.
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Apr 12, 2016
1 parent 889409d commit 8187855
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
7 changes: 6 additions & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,12 @@ func (cmd *Cmd) readReply(cn *pool.Conn) error {
cmd.err = err
return cmd.err
}
cmd.val = val
if b, ok := val.([]byte); ok {
// Bytes must be copied, because underlying memory is reused.
cmd.val = string(b)
} else {
cmd.val = val
}
return nil
}

Expand Down
8 changes: 4 additions & 4 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"gopkg.in/redis.v4"
)

var _ = Describe("Command", func() {
var _ = Describe("Cmd", func() {
var client *redis.Client

BeforeEach(func() {
Expand All @@ -19,15 +19,15 @@ var _ = Describe("Command", func() {
Expect(client.Close()).NotTo(HaveOccurred())
})

It("should implement Stringer", func() {
It("implements Stringer", func() {
set := client.Set("foo", "bar", 0)
Expect(set.String()).To(Equal("SET foo bar: OK"))

get := client.Get("foo")
Expect(get.String()).To(Equal("GET foo: bar"))
})

It("should have correct val/err states", func() {
It("has val/err", func() {
set := client.Set("key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
Expand All @@ -40,7 +40,7 @@ var _ = Describe("Command", func() {
Expect(set.Val()).To(Equal("OK"))
})

It("should convert strings via helpers", func() {
It("has helpers", func() {
set := client.Set("key", "10", 0)
Expect(set.Err()).NotTo(HaveOccurred())

Expand Down
8 changes: 6 additions & 2 deletions redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,15 @@ var _ = Describe("Client", func() {
Expect(db2.Close()).NotTo(HaveOccurred())
})

It("should process custom commands", func() {
It("processes custom commands", func() {
cmd := redis.NewCmd("PING")
client.Process(cmd)

// Flush buffers.
Expect(client.Echo("hello").Err()).NotTo(HaveOccurred())

Expect(cmd.Err()).NotTo(HaveOccurred())
Expect(cmd.Val()).To(Equal([]byte("PONG")))
Expect(cmd.Val()).To(Equal("PONG"))
})

It("should retry command on network error", func() {
Expand Down

0 comments on commit 8187855

Please sign in to comment.