|
|
@@ -22,6 +22,7 @@ import (
|
|
|
|
|
|
var ErrNil = errors.New("redigo: nil returned")
|
|
|
|
|
|
+// Values is deprecated. Use Scan instead.
|
|
|
func Values(multiBulk []interface{}, values ...interface{}) ([]interface{}, error) {
|
|
|
if len(multiBulk) < len(values) {
|
|
|
return nil, errors.New("redigo Values: short multibulk")
|
|
|
@@ -50,130 +51,134 @@ func Values(multiBulk []interface{}, values ...interface{}) ([]interface{}, erro
|
|
|
return multiBulk[len(values):], err
|
|
|
}
|
|
|
|
|
|
-// Int is a helper that converts a Redis reply to an int.
|
|
|
+// Int is a helper that converts a command reply to an integer. If err is not
|
|
|
+// equal to nil, then Int returns 0, err. Otherwise, Int converts the
|
|
|
+// reply to an int as follows:
|
|
|
//
|
|
|
// Reply type Result
|
|
|
-// integer return reply as int
|
|
|
-// bulk parse decimal integer from reply
|
|
|
-// nil return error ErrNil
|
|
|
-// other return error
|
|
|
-//
|
|
|
-func Int(v interface{}, err error) (int, error) {
|
|
|
+// integer int(reply), nil
|
|
|
+// bulk strconv.ParseInt(reply, 10, 0)
|
|
|
+// nil 0, ErrNil
|
|
|
+// other 0, error
|
|
|
+func Int(reply interface{}, err error) (int, error) {
|
|
|
if err != nil {
|
|
|
return 0, err
|
|
|
}
|
|
|
- switch v := v.(type) {
|
|
|
+ switch reply := reply.(type) {
|
|
|
case int64:
|
|
|
- return int(v), nil
|
|
|
+ x := int(reply)
|
|
|
+ if int64(x) != reply {
|
|
|
+ return 0, strconv.ErrRange
|
|
|
+ }
|
|
|
+ return x, nil
|
|
|
case []byte:
|
|
|
- n, err := strconv.ParseInt(string(v), 10, 0)
|
|
|
+ n, err := strconv.ParseInt(string(reply), 10, 0)
|
|
|
return int(n), err
|
|
|
case nil:
|
|
|
return 0, ErrNil
|
|
|
case Error:
|
|
|
- return 0, v
|
|
|
+ return 0, reply
|
|
|
}
|
|
|
- return 0, fmt.Errorf("redigo: unexpected type for Int, got type %T", v)
|
|
|
+ return 0, fmt.Errorf("redigo: unexpected type for Int, got type %T", reply)
|
|
|
}
|
|
|
|
|
|
-// String is a helper that converts a Redis reply to a string.
|
|
|
+// String is a helper that converts a command reply to a string. If err is not
|
|
|
+// equal to nil, then String returns "", err. Otherwise String converts the
|
|
|
+// reply to a string as follows:
|
|
|
//
|
|
|
// Reply type Result
|
|
|
-// integer format as decimal string
|
|
|
-// bulk return reply as string
|
|
|
-// string return as is
|
|
|
-// nil return error ErrNil
|
|
|
-// other return error
|
|
|
-func String(v interface{}, err error) (string, error) {
|
|
|
+// bulk string(reply), nil
|
|
|
+// string reply, nil
|
|
|
+// nil "", ErrNil
|
|
|
+// other "", error
|
|
|
+func String(reply interface{}, err error) (string, error) {
|
|
|
if err != nil {
|
|
|
return "", err
|
|
|
}
|
|
|
- switch v := v.(type) {
|
|
|
+ switch reply := reply.(type) {
|
|
|
case []byte:
|
|
|
- return string(v), nil
|
|
|
+ return string(reply), nil
|
|
|
case string:
|
|
|
- return v, nil
|
|
|
- case int64:
|
|
|
- return strconv.FormatInt(v, 10), nil
|
|
|
+ return reply, nil
|
|
|
case nil:
|
|
|
return "", ErrNil
|
|
|
case Error:
|
|
|
- return "", v
|
|
|
+ return "", reply
|
|
|
}
|
|
|
- return "", fmt.Errorf("redigo: unexpected type for String, got type %T", v)
|
|
|
+ return "", fmt.Errorf("redigo: unexpected type for String, got type %T", reply)
|
|
|
}
|
|
|
|
|
|
-// Bytes is a helper that converts a Redis reply to slice of bytes.
|
|
|
+// Bytes is a helper that converts a command reply to a slice of bytes. If err
|
|
|
+// is not equal to nil, then Bytes returns nil, err. Otherwise Bytes converts
|
|
|
+// the reply to a slice of bytes as follows:
|
|
|
//
|
|
|
// Reply type Result
|
|
|
-// integer format as decimal string
|
|
|
-// bulk return reply as slice of bytes
|
|
|
-// string return reply as slice of bytes
|
|
|
-// nil return error ErrNil
|
|
|
-// other return error
|
|
|
-func Bytes(v interface{}, err error) ([]byte, error) {
|
|
|
+// bulk reply, nil
|
|
|
+// string []byte(reply), nil
|
|
|
+// nil nil, ErrNil
|
|
|
+// other nil, error
|
|
|
+func Bytes(reply interface{}, err error) ([]byte, error) {
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- switch v := v.(type) {
|
|
|
+ switch reply := reply.(type) {
|
|
|
case []byte:
|
|
|
- return v, nil
|
|
|
+ return reply, nil
|
|
|
case string:
|
|
|
- return []byte(v), nil
|
|
|
- case int64:
|
|
|
- return strconv.AppendInt(nil, v, 10), nil
|
|
|
+ return []byte(reply), nil
|
|
|
case nil:
|
|
|
return nil, ErrNil
|
|
|
case Error:
|
|
|
- return nil, v
|
|
|
+ return nil, reply
|
|
|
}
|
|
|
- return nil, fmt.Errorf("redigo: unexpected type for Bytes, got type %T", v)
|
|
|
+ return nil, fmt.Errorf("redigo: unexpected type for Bytes, got type %T", reply)
|
|
|
}
|
|
|
|
|
|
-// Bool is a helper that converts a Redis reply to a bool.
|
|
|
+// Bool is a helper that converts a command reply to a boolean. If err is not
|
|
|
+// equal to nil, then Bool returns false, err. Otherwise Bool converts the
|
|
|
+// reply to boolean as follows:
|
|
|
//
|
|
|
// Reply type Result
|
|
|
-// integer return value != 0
|
|
|
-// bulk return false if reply is "" or "0", otherwise return true.
|
|
|
-// nil return error ErrNil
|
|
|
-// other return error
|
|
|
-func Bool(v interface{}, err error) (bool, error) {
|
|
|
+// integer value != 0, nil
|
|
|
+// bulk strconv.ParseBool(reply)
|
|
|
+// nil false, ErrNil
|
|
|
+// other false, error
|
|
|
+func Bool(reply interface{}, err error) (bool, error) {
|
|
|
if err != nil {
|
|
|
return false, err
|
|
|
}
|
|
|
- switch v := v.(type) {
|
|
|
+ switch reply := reply.(type) {
|
|
|
case int64:
|
|
|
- return v != 0, nil
|
|
|
+ return reply != 0, nil
|
|
|
case []byte:
|
|
|
- if len(v) == 0 || (len(v) == 1 && v[0] == '0') {
|
|
|
- return false, nil
|
|
|
- }
|
|
|
- return true, nil
|
|
|
+ return strconv.ParseBool(string(reply))
|
|
|
case nil:
|
|
|
return false, ErrNil
|
|
|
case Error:
|
|
|
- return false, v
|
|
|
+ return false, reply
|
|
|
}
|
|
|
- return false, fmt.Errorf("redigo: unexpected type for Bool, got type %T", v)
|
|
|
+ return false, fmt.Errorf("redigo: unexpected type for Bool, got type %T", reply)
|
|
|
}
|
|
|
|
|
|
-// MultiBulk is a helper that converts a Redis reply to a []interface{}.
|
|
|
+// MultiBulk is a helper that converts a command reply to a []interface{}. If
|
|
|
+// err is not equal to nil, then MultiBulk returns nil, err. Otherwise,
|
|
|
+// MultiBulk converts the reply as follows:
|
|
|
//
|
|
|
// Reply type Result
|
|
|
-// multi-bulk return []interface{}
|
|
|
-// nil return error ErrNil
|
|
|
-// other return error
|
|
|
-func MultiBulk(v interface{}, err error) ([]interface{}, error) {
|
|
|
+// multi-bulk reply, nil
|
|
|
+// nil nil, ErrNil
|
|
|
+// other nil, error
|
|
|
+func MultiBulk(reply interface{}, err error) ([]interface{}, error) {
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- switch v := v.(type) {
|
|
|
+ switch reply := reply.(type) {
|
|
|
case []interface{}:
|
|
|
- return v, nil
|
|
|
+ return reply, nil
|
|
|
case nil:
|
|
|
return nil, ErrNil
|
|
|
case Error:
|
|
|
- return nil, v
|
|
|
+ return nil, reply
|
|
|
}
|
|
|
- return nil, fmt.Errorf("redigo: unexpected type for MultiBulk, got type %T", v)
|
|
|
+ return nil, fmt.Errorf("redigo: unexpected type for MultiBulk, got type %T", reply)
|
|
|
}
|