Merge pull request #423 from go-redis/fix/string-command

Use string val in StringCmd.
This commit is contained in:
Vladimir Mihailenco 2016-11-20 10:07:49 +02:00 committed by GitHub
commit f8ad82370f
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 { type StringCmd struct {
baseCmd baseCmd
val []byte val string
} }
func NewStringCmd(args ...interface{}) *StringCmd { func NewStringCmd(args ...interface{}) *StringCmd {
@ -466,12 +466,12 @@ func NewStringCmd(args ...interface{}) *StringCmd {
} }
func (cmd *StringCmd) reset() { func (cmd *StringCmd) reset() {
cmd.val = nil cmd.val = ""
cmd.err = nil cmd.err = nil
} }
func (cmd *StringCmd) Val() string { func (cmd *StringCmd) Val() string {
return string(cmd.val) return cmd.val
} }
func (cmd *StringCmd) Result() (string, error) { func (cmd *StringCmd) Result() (string, error) {
@ -479,7 +479,7 @@ func (cmd *StringCmd) Result() (string, error) {
} }
func (cmd *StringCmd) Bytes() ([]byte, error) { func (cmd *StringCmd) Bytes() ([]byte, error) {
return cmd.val, cmd.err return []byte(cmd.val), cmd.err
} }
func (cmd *StringCmd) Int64() (int64, error) { func (cmd *StringCmd) Int64() (int64, error) {
@ -520,11 +520,7 @@ func (cmd *StringCmd) readReply(cn *pool.Conn) error {
cmd.err = err cmd.err = err
return err return err
} }
cmd.val = string(b)
new := make([]byte, len(b))
copy(new, b)
cmd.val = new
return nil return nil
} }

View File

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

View File

@ -11,7 +11,13 @@ import (
const bytesAllocLimit = 1024 * 1024 // 1mb 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) type MultiBulkParse func(*Reader, int64) (interface{}, error)
@ -23,7 +29,7 @@ type Reader struct {
func NewReader(rd io.Reader) *Reader { func NewReader(rd io.Reader) *Reader {
return &Reader{ return &Reader{
src: bufio.NewReader(rd), 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 return nil, bufio.ErrBufferFull
} }
if len(line) == 0 { if len(line) == 0 {
return nil, errEmptyReply return nil, internal.RedisError("redis: reply is empty")
} }
if isNilReply(line) { if isNilReply(line) {
return nil, internal.Nil return nil, internal.Nil

View File

@ -6,11 +6,13 @@ import (
"strconv" "strconv"
) )
const bufferSize = 4096
type WriteBuffer struct{ b []byte } type WriteBuffer struct{ b []byte }
func NewWriteBuffer() *WriteBuffer { func NewWriteBuffer() *WriteBuffer {
return &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 // 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 var cmd StringCmd
cmd.val = val cmd.val = val
cmd.setErr(err) cmd.setErr(err)