瀏覽代碼

Add Uint64 reply helper.

Gary Burd 11 年之前
父節點
當前提交
a6a0a737c0
共有 3 個文件被更改,包括 46 次插入0 次删除
  1. 32 0
      redis/reply.go
  2. 10 0
      redis/reply_test.go
  3. 4 0
      redis/test_test.go

+ 32 - 0
redis/reply.go

@@ -81,6 +81,38 @@ func Int64(reply interface{}, err error) (int64, error) {
 	return 0, fmt.Errorf("redigo: unexpected type for Int64, got type %T", reply)
 }
 
+var errNegativeInt = errors.New("redigo: unexpected value for Uint64")
+
+// Uint64 is a helper that converts a command reply to 64 bit integer. If err is
+// not equal to nil, then Int returns 0, err. Otherwise, Int64 converts the
+// reply to an int64 as follows:
+//
+//  Reply type    Result
+//  integer       reply, nil
+//  bulk string   parsed reply, nil
+//  nil           0, ErrNil
+//  other         0, error
+func Uint64(reply interface{}, err error) (uint64, error) {
+	if err != nil {
+		return 0, err
+	}
+	switch reply := reply.(type) {
+	case int64:
+		if reply < 0 {
+			return 0, errNegativeInt
+		}
+		return uint64(reply), nil
+	case []byte:
+		n, err := strconv.ParseUint(string(reply), 10, 64)
+		return n, err
+	case nil:
+		return 0, ErrNil
+	case Error:
+		return 0, reply
+	}
+	return 0, fmt.Errorf("redigo: unexpected type for Uint64, got type %T", reply)
+}
+
 // Float64 is a helper that converts a command reply to 64 bit float. If err is
 // not equal to nil, then Float64 returns 0, err. Otherwise, Float64 converts
 // the reply to an int as follows:

+ 10 - 0
redis/reply_test.go

@@ -66,6 +66,16 @@ var replyTests = []struct {
 		ve(redis.Float64(nil, nil)),
 		ve(float64(0.0), redis.ErrNil),
 	},
+	{
+		"uint64(1)",
+		ve(redis.Uint64(int64(1), nil)),
+		ve(uint64(1), nil),
+	},
+	{
+		"uint64(-1)",
+		ve(redis.Uint64(int64(-1), nil)),
+		ve(uint64(0), redis.ErrNegativeInt),
+	},
 }
 
 func TestReply(t *testing.T) {

+ 4 - 0
redis/test_test.go

@@ -71,3 +71,7 @@ func (dummyClose) Close() error { return nil }
 func NewConnBufio(rw bufio.ReadWriter) Conn {
 	return &conn{br: rw.Reader, bw: rw.Writer, conn: dummyClose{}}
 }
+
+var (
+	ErrNegativeInt = errNegativeInt
+)