diff --git a/command.go b/command.go index 99bbd2b..d7c76cf 100644 --- a/command.go +++ b/command.go @@ -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) } diff --git a/example_test.go b/example_test.go index 0a7f0bc..dbc9513 100644 --- a/example_test.go +++ b/example_test.go @@ -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: - // "" 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 } func ExampleClient_Pipelined() { diff --git a/redis_test.go b/redis_test.go index 720d63c..9bd1e09 100644 --- a/redis_test.go +++ b/redis_test.go @@ -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) {