forked from mirror/redis
Merge pull request #33 from go-redis/feature/string_cmd_helpers
Add StringCmd helpers.
This commit is contained in:
commit
4f3b1d7bf4
21
command.go
21
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)
|
||||
}
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue