Add StringCmd helpers.

This commit is contained in:
Vladimir Mihailenco 2014-07-31 15:18:23 +03:00
parent ffe62d60e2
commit 9ac69a08db
3 changed files with 52 additions and 5 deletions

View File

@ -337,6 +337,27 @@ func (cmd *StringCmd) Result() (string, error) {
return cmd.val, cmd.err
}
func (cmd *StringCmd) Int64() (int64, error) {
if cmd.err != nil {
return 0, cmd.err
}
return strconv.ParseInt(cmd.val, 10, 64)
}
func (cmd *StringCmd) Uint64() (uint64, error) {
if cmd.err != nil {
return 0, cmd.err
}
return strconv.ParseUint(cmd.val, 10, 64)
}
func (cmd *StringCmd) Float64() (float64, error) {
if cmd.err != nil {
return 0, cmd.err
}
return strconv.ParseFloat(cmd.val, 64)
}
func (cmd *StringCmd) String() string {
return cmdString(cmd, cmd.val)
}

View File

@ -40,14 +40,23 @@ func ExampleNewFailoverClient() {
}
func ExampleClient() {
set := client.Set("foo", "bar")
fmt.Println(set.Err())
if err := client.Set("foo", "bar").Err(); err != nil {
panic(err)
}
v, err := client.Get("hello").Result()
fmt.Printf("%q %s %v", v, err, err == redis.Nil)
fmt.Printf("%q %q %v", v, err, err == redis.Nil)
// Output: "" "redis: nil" true
}
// Output: <nil>
// "" redis: nil true
func ExampleClient_Incr() {
if err := client.Incr("counter").Err(); err != nil {
panic(err)
}
n, err := client.Get("counter").Int64()
fmt.Println(n, err)
// Output: 1 <nil>
}
func ExampleClient_Pipelined() {

View File

@ -408,6 +408,23 @@ func (t *RedisTest) TestManyKeys2(c *C) {
c.Assert(vals[100001], Equals, nil)
}
func (t *RedisTest) TestStringCmdHelpers(c *C) {
set := t.client.Set("key", "10")
c.Assert(set.Err(), IsNil)
n, err := t.client.Get("key").Int64()
c.Assert(err, IsNil)
c.Assert(n, Equals, int64(10))
un, err := t.client.Get("key").Uint64()
c.Assert(err, IsNil)
c.Assert(un, Equals, uint64(10))
f, err := t.client.Get("key").Float64()
c.Assert(err, IsNil)
c.Assert(f, Equals, float64(10))
}
//------------------------------------------------------------------------------
func (t *RedisTest) TestAuth(c *C) {