From a20665f042f007727466fe2643ac0870b03ae3ae Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Sun, 20 Nov 2016 09:50:49 +0200 Subject: [PATCH] Use string val in StringCmd. --- command.go | 14 ++++------ internal/proto/proto.go | 48 +++++++++++++---------------------- internal/proto/reader.go | 12 ++++++--- internal/proto/writebuffer.go | 4 ++- result.go | 2 +- 5 files changed, 36 insertions(+), 44 deletions(-) diff --git a/command.go b/command.go index 15a04c0e..0108f8ba 100644 --- a/command.go +++ b/command.go @@ -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 } diff --git a/internal/proto/proto.go b/internal/proto/proto.go index 1d975202..e1c1ed4a 100644 --- a/internal/proto/proto.go +++ b/internal/proto/proto.go @@ -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) diff --git a/internal/proto/reader.go b/internal/proto/reader.go index bd76e3a8..7e728463 100644 --- a/internal/proto/reader.go +++ b/internal/proto/reader.go @@ -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 diff --git a/internal/proto/writebuffer.go b/internal/proto/writebuffer.go index 8164f7f2..1e0f8e6c 100644 --- a/internal/proto/writebuffer.go +++ b/internal/proto/writebuffer.go @@ -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), } } diff --git a/result.go b/result.go index 481673d2..f5c7084c 100644 --- a/result.go +++ b/result.go @@ -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)