2016-07-02 15:52:10 +03:00
|
|
|
package proto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding"
|
|
|
|
"fmt"
|
|
|
|
|
2016-10-09 14:38:31 +03:00
|
|
|
"gopkg.in/redis.v5/internal"
|
2016-07-02 15:52:10 +03:00
|
|
|
)
|
|
|
|
|
2017-01-13 14:39:59 +03:00
|
|
|
func Scan(b []byte, v interface{}) error {
|
2016-11-26 11:33:06 +03:00
|
|
|
switch v := v.(type) {
|
2016-07-02 15:52:10 +03:00
|
|
|
case nil:
|
2016-11-20 10:50:49 +03:00
|
|
|
return internal.RedisError("redis: Scan(nil)")
|
2016-07-02 15:52:10 +03:00
|
|
|
case *string:
|
2017-01-13 14:39:59 +03:00
|
|
|
*v = internal.BytesToString(b)
|
2016-07-02 15:52:10 +03:00
|
|
|
return nil
|
|
|
|
case *[]byte:
|
2017-01-13 14:39:59 +03:00
|
|
|
*v = b
|
2016-07-02 15:52:10 +03:00
|
|
|
return nil
|
|
|
|
case *int:
|
|
|
|
var err error
|
2017-01-13 14:39:59 +03:00
|
|
|
*v, err = atoi(b)
|
2016-07-02 15:52:10 +03:00
|
|
|
return err
|
|
|
|
case *int8:
|
2017-01-13 14:39:59 +03:00
|
|
|
n, err := parseInt(b, 10, 8)
|
2016-07-02 15:52:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*v = int8(n)
|
|
|
|
return nil
|
|
|
|
case *int16:
|
2017-01-13 14:39:59 +03:00
|
|
|
n, err := parseInt(b, 10, 16)
|
2016-07-02 15:52:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*v = int16(n)
|
|
|
|
return nil
|
|
|
|
case *int32:
|
2017-01-13 14:39:59 +03:00
|
|
|
n, err := parseInt(b, 10, 32)
|
2016-07-02 15:52:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*v = int32(n)
|
|
|
|
return nil
|
|
|
|
case *int64:
|
2017-01-13 14:39:59 +03:00
|
|
|
n, err := parseInt(b, 10, 64)
|
2016-07-02 15:52:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*v = n
|
|
|
|
return nil
|
|
|
|
case *uint:
|
2017-01-13 14:39:59 +03:00
|
|
|
n, err := parseUint(b, 10, 64)
|
2016-07-02 15:52:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*v = uint(n)
|
|
|
|
return nil
|
|
|
|
case *uint8:
|
2017-01-13 14:39:59 +03:00
|
|
|
n, err := parseUint(b, 10, 8)
|
2016-07-02 15:52:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*v = uint8(n)
|
|
|
|
return nil
|
|
|
|
case *uint16:
|
2017-01-13 14:39:59 +03:00
|
|
|
n, err := parseUint(b, 10, 16)
|
2016-07-02 15:52:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*v = uint16(n)
|
|
|
|
return nil
|
|
|
|
case *uint32:
|
2017-01-13 14:39:59 +03:00
|
|
|
n, err := parseUint(b, 10, 32)
|
2016-07-02 15:52:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*v = uint32(n)
|
|
|
|
return nil
|
|
|
|
case *uint64:
|
2017-01-13 14:39:59 +03:00
|
|
|
n, err := parseUint(b, 10, 64)
|
2016-07-02 15:52:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*v = n
|
|
|
|
return nil
|
|
|
|
case *float32:
|
2017-01-13 14:39:59 +03:00
|
|
|
n, err := parseFloat(b, 32)
|
2016-07-02 15:52:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*v = float32(n)
|
|
|
|
return err
|
|
|
|
case *float64:
|
|
|
|
var err error
|
2017-01-13 14:39:59 +03:00
|
|
|
*v, err = parseFloat(b, 64)
|
2016-07-02 15:52:10 +03:00
|
|
|
return err
|
|
|
|
case *bool:
|
2017-01-13 14:39:59 +03:00
|
|
|
*v = len(b) == 1 && b[0] == '1'
|
2016-07-02 15:52:10 +03:00
|
|
|
return nil
|
2016-11-26 11:33:06 +03:00
|
|
|
case encoding.BinaryUnmarshaler:
|
2017-01-13 14:39:59 +03:00
|
|
|
return v.UnmarshalBinary(b)
|
2016-07-02 15:52:10 +03:00
|
|
|
default:
|
2016-11-26 11:33:06 +03:00
|
|
|
return fmt.Errorf(
|
|
|
|
"redis: can't unmarshal %T (consider implementing BinaryUnmarshaler)", v)
|
2016-07-02 15:52:10 +03:00
|
|
|
}
|
|
|
|
}
|