1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-28 06:42:00 +03:00

Fix proto.RedisError in slices

This commit is contained in:
Vladimir Mihailenco
2018-02-22 14:14:30 +02:00
parent 71ed499c46
commit 56dea1f39a
14 changed files with 122 additions and 105 deletions

View File

@ -6,7 +6,7 @@ import (
"io"
"strconv"
"github.com/go-redis/redis/internal"
"github.com/go-redis/redis/internal/util"
)
const bytesAllocLimit = 1024 * 1024 // 1mb
@ -19,6 +19,16 @@ const (
ArrayReply = '*'
)
//------------------------------------------------------------------------------
const Nil = RedisError("redis: nil")
type RedisError string
func (e RedisError) Error() string { return string(e) }
//------------------------------------------------------------------------------
type MultiBulkParse func(*Reader, int64) (interface{}, error)
type Reader struct {
@ -66,7 +76,7 @@ func (r *Reader) ReadLine() ([]byte, error) {
return nil, fmt.Errorf("redis: reply is empty")
}
if isNilReply(line) {
return nil, internal.Nil
return nil, Nil
}
return line, nil
}
@ -83,7 +93,7 @@ func (r *Reader) ReadReply(m MultiBulkParse) (interface{}, error) {
case StatusReply:
return parseStatusValue(line), nil
case IntReply:
return parseInt(line[1:], 10, 64)
return util.ParseInt(line[1:], 10, 64)
case StringReply:
return r.readTmpBytesValue(line)
case ArrayReply:
@ -105,7 +115,7 @@ func (r *Reader) ReadIntReply() (int64, error) {
case ErrorReply:
return 0, ParseErrorReply(line)
case IntReply:
return parseInt(line[1:], 10, 64)
return util.ParseInt(line[1:], 10, 64)
default:
return 0, fmt.Errorf("redis: can't parse int reply: %.100q", line)
}
@ -151,7 +161,7 @@ func (r *Reader) ReadFloatReply() (float64, error) {
if err != nil {
return 0, err
}
return parseFloat(b, 64)
return util.ParseFloat(b, 64)
}
func (r *Reader) ReadArrayReply(m MultiBulkParse) (interface{}, error) {
@ -221,7 +231,7 @@ func (r *Reader) ReadScanReply() ([]string, uint64, error) {
func (r *Reader) readTmpBytesValue(line []byte) ([]byte, error) {
if isNilReply(line) {
return nil, internal.Nil
return nil, Nil
}
replyLen, err := strconv.Atoi(string(line[1:]))
@ -241,7 +251,7 @@ func (r *Reader) ReadInt() (int64, error) {
if err != nil {
return 0, err
}
return parseInt(b, 10, 64)
return util.ParseInt(b, 10, 64)
}
func (r *Reader) ReadUint() (uint64, error) {
@ -249,7 +259,7 @@ func (r *Reader) ReadUint() (uint64, error) {
if err != nil {
return 0, err
}
return parseUint(b, 10, 64)
return util.ParseUint(b, 10, 64)
}
// --------------------------------------------------------------------
@ -303,7 +313,7 @@ func isNilReply(b []byte) bool {
}
func ParseErrorReply(line []byte) error {
return internal.RedisError(string(line[1:]))
return RedisError(string(line[1:]))
}
func parseStatusValue(line []byte) []byte {
@ -312,23 +322,7 @@ func parseStatusValue(line []byte) []byte {
func parseArrayLen(line []byte) (int64, error) {
if isNilReply(line) {
return 0, internal.Nil
return 0, Nil
}
return parseInt(line[1:], 10, 64)
}
func atoi(b []byte) (int, error) {
return strconv.Atoi(internal.BytesToString(b))
}
func parseInt(b []byte, base int, bitSize int) (int64, error) {
return strconv.ParseInt(internal.BytesToString(b), base, bitSize)
}
func parseUint(b []byte, base int, bitSize int) (uint64, error) {
return strconv.ParseUint(internal.BytesToString(b), base, bitSize)
}
func parseFloat(b []byte, bitSize int) (float64, error) {
return strconv.ParseFloat(internal.BytesToString(b), bitSize)
return util.ParseInt(line[1:], 10, 64)
}