Use string val in StringCmd.

This commit is contained in:
Vladimir Mihailenco 2016-11-20 09:50:49 +02:00
parent c985686167
commit a20665f042
5 changed files with 36 additions and 44 deletions

View File

@ -457,7 +457,7 @@ func (cmd *BoolCmd) readReply(cn *pool.Conn) error {
type StringCmd struct {
baseCmd
val []byte
val string
}
func NewStringCmd(args ...interface{}) *StringCmd {
@ -466,12 +466,12 @@ func NewStringCmd(args ...interface{}) *StringCmd {
}
func (cmd *StringCmd) reset() {
cmd.val = nil
cmd.val = ""
cmd.err = nil
}
func (cmd *StringCmd) Val() string {
return string(cmd.val)
return cmd.val
}
func (cmd *StringCmd) Result() (string, error) {
@ -479,7 +479,7 @@ func (cmd *StringCmd) Result() (string, error) {
}
func (cmd *StringCmd) Bytes() ([]byte, error) {
return cmd.val, cmd.err
return []byte(cmd.val), cmd.err
}
func (cmd *StringCmd) Int64() (int64, error) {
@ -520,11 +520,7 @@ func (cmd *StringCmd) readReply(cn *pool.Conn) error {
cmd.err = err
return err
}
new := make([]byte, len(b))
copy(new, b)
cmd.val = new
cmd.val = string(b)
return nil
}

View File

@ -8,97 +8,85 @@ import (
"gopkg.in/redis.v5/internal"
)
const (
ErrorReply = '-'
StatusReply = '+'
IntReply = ':'
StringReply = '$'
ArrayReply = '*'
)
const defaultBufSize = 4096
const errScanNil = internal.RedisError("redis: Scan(nil)")
func Scan(b []byte, val interface{}) error {
func Scan(s string, val interface{}) error {
switch v := val.(type) {
case nil:
return errScanNil
return internal.RedisError("redis: Scan(nil)")
case *string:
*v = string(b)
*v = s
return nil
case *[]byte:
*v = b
*v = []byte(s)
return nil
case *int:
var err error
*v, err = strconv.Atoi(string(b))
*v, err = strconv.Atoi(s)
return err
case *int8:
n, err := strconv.ParseInt(string(b), 10, 8)
n, err := strconv.ParseInt(s, 10, 8)
if err != nil {
return err
}
*v = int8(n)
return nil
case *int16:
n, err := strconv.ParseInt(string(b), 10, 16)
n, err := strconv.ParseInt(s, 10, 16)
if err != nil {
return err
}
*v = int16(n)
return nil
case *int32:
n, err := strconv.ParseInt(string(b), 10, 32)
n, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return err
}
*v = int32(n)
return nil
case *int64:
n, err := strconv.ParseInt(string(b), 10, 64)
n, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return err
}
*v = n
return nil
case *uint:
n, err := strconv.ParseUint(string(b), 10, 64)
n, err := strconv.ParseUint(s, 10, 64)
if err != nil {
return err
}
*v = uint(n)
return nil
case *uint8:
n, err := strconv.ParseUint(string(b), 10, 8)
n, err := strconv.ParseUint(s, 10, 8)
if err != nil {
return err
}
*v = uint8(n)
return nil
case *uint16:
n, err := strconv.ParseUint(string(b), 10, 16)
n, err := strconv.ParseUint(s, 10, 16)
if err != nil {
return err
}
*v = uint16(n)
return nil
case *uint32:
n, err := strconv.ParseUint(string(b), 10, 32)
n, err := strconv.ParseUint(s, 10, 32)
if err != nil {
return err
}
*v = uint32(n)
return nil
case *uint64:
n, err := strconv.ParseUint(string(b), 10, 64)
n, err := strconv.ParseUint(s, 10, 64)
if err != nil {
return err
}
*v = n
return nil
case *float32:
n, err := strconv.ParseFloat(string(b), 32)
n, err := strconv.ParseFloat(s, 32)
if err != nil {
return err
}
@ -106,14 +94,14 @@ func Scan(b []byte, val interface{}) error {
return err
case *float64:
var err error
*v, err = strconv.ParseFloat(string(b), 64)
*v, err = strconv.ParseFloat(s, 64)
return err
case *bool:
*v = len(b) == 1 && b[0] == '1'
*v = len(s) == 1 && s[0] == '1'
return nil
default:
if bu, ok := val.(encoding.BinaryUnmarshaler); ok {
return bu.UnmarshalBinary(b)
return bu.UnmarshalBinary([]byte(s))
}
err := fmt.Errorf(
"redis: can't unmarshal %T (consider implementing BinaryUnmarshaler)", val)

View File

@ -11,7 +11,13 @@ import (
const bytesAllocLimit = 1024 * 1024 // 1mb
const errEmptyReply = internal.RedisError("redis: reply is empty")
const (
ErrorReply = '-'
StatusReply = '+'
IntReply = ':'
StringReply = '$'
ArrayReply = '*'
)
type MultiBulkParse func(*Reader, int64) (interface{}, error)
@ -23,7 +29,7 @@ type Reader struct {
func NewReader(rd io.Reader) *Reader {
return &Reader{
src: bufio.NewReader(rd),
buf: make([]byte, 0, defaultBufSize),
buf: make([]byte, 0, bufferSize),
}
}
@ -48,7 +54,7 @@ func (p *Reader) ReadLine() ([]byte, error) {
return nil, bufio.ErrBufferFull
}
if len(line) == 0 {
return nil, errEmptyReply
return nil, internal.RedisError("redis: reply is empty")
}
if isNilReply(line) {
return nil, internal.Nil

View File

@ -6,11 +6,13 @@ import (
"strconv"
)
const bufferSize = 4096
type WriteBuffer struct{ b []byte }
func NewWriteBuffer() *WriteBuffer {
return &WriteBuffer{
b: make([]byte, 0, defaultBufSize),
b: make([]byte, 0, bufferSize),
}
}

View File

@ -51,7 +51,7 @@ func NewBoolResult(val bool, err error) *BoolCmd {
}
// NewStringResult returns a StringCmd initalised with val and err for testing
func NewStringResult(val []byte, err error) *StringCmd {
func NewStringResult(val string, err error) *StringCmd {
var cmd StringCmd
cmd.val = val
cmd.setErr(err)