redis/internal/proto/scan.go

186 lines
3.5 KiB
Go
Raw Normal View History

2016-07-02 15:52:10 +03:00
package proto
import (
"encoding"
"fmt"
"net"
2017-02-01 11:36:33 +03:00
"reflect"
"time"
2016-07-02 15:52:10 +03:00
2022-06-04 17:39:21 +03:00
"github.com/go-redis/redis/v9/internal/util"
2016-07-02 15:52:10 +03:00
)
// Scan parses bytes `b` to `v` with appropriate type.
//
2021-10-24 09:55:27 +03:00
//nolint:gocyclo
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:
2017-08-31 15:22:47 +03:00
return fmt.Errorf("redis: Scan(nil)")
2016-07-02 15:52:10 +03:00
case *string:
2018-02-22 15:14:30 +03:00
*v = util.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
2018-02-22 15:14:30 +03:00
*v, err = util.Atoi(b)
2016-07-02 15:52:10 +03:00
return err
case *int8:
2018-02-22 15:14:30 +03:00
n, err := util.ParseInt(b, 10, 8)
2016-07-02 15:52:10 +03:00
if err != nil {
return err
}
*v = int8(n)
return nil
case *int16:
2018-02-22 15:14:30 +03:00
n, err := util.ParseInt(b, 10, 16)
2016-07-02 15:52:10 +03:00
if err != nil {
return err
}
*v = int16(n)
return nil
case *int32:
2018-02-22 15:14:30 +03:00
n, err := util.ParseInt(b, 10, 32)
2016-07-02 15:52:10 +03:00
if err != nil {
return err
}
*v = int32(n)
return nil
case *int64:
2018-02-22 15:14:30 +03:00
n, err := util.ParseInt(b, 10, 64)
2016-07-02 15:52:10 +03:00
if err != nil {
return err
}
*v = n
return nil
case *uint:
2018-02-22 15:14:30 +03:00
n, err := util.ParseUint(b, 10, 64)
2016-07-02 15:52:10 +03:00
if err != nil {
return err
}
*v = uint(n)
return nil
case *uint8:
2018-02-22 15:14:30 +03:00
n, err := util.ParseUint(b, 10, 8)
2016-07-02 15:52:10 +03:00
if err != nil {
return err
}
*v = uint8(n)
return nil
case *uint16:
2018-02-22 15:14:30 +03:00
n, err := util.ParseUint(b, 10, 16)
2016-07-02 15:52:10 +03:00
if err != nil {
return err
}
*v = uint16(n)
return nil
case *uint32:
2018-02-22 15:14:30 +03:00
n, err := util.ParseUint(b, 10, 32)
2016-07-02 15:52:10 +03:00
if err != nil {
return err
}
*v = uint32(n)
return nil
case *uint64:
2018-02-22 15:14:30 +03:00
n, err := util.ParseUint(b, 10, 64)
2016-07-02 15:52:10 +03:00
if err != nil {
return err
}
*v = n
return nil
case *float32:
2018-02-22 15:14:30 +03:00
n, err := util.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
2018-02-22 15:14:30 +03:00
*v, err = util.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
case *time.Time:
var err error
*v, err = time.Parse(time.RFC3339Nano, util.BytesToString(b))
return err
case *time.Duration:
n, err := util.ParseInt(b, 10, 64)
if err != nil {
return err
}
*v = time.Duration(n)
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)
case *net.IP:
*v = b
return nil
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
}
}
2017-02-01 11:36:33 +03:00
2017-02-18 13:10:47 +03:00
func ScanSlice(data []string, slice interface{}) error {
v := reflect.ValueOf(slice)
if !v.IsValid() {
2017-02-01 11:36:33 +03:00
return fmt.Errorf("redis: ScanSlice(nil)")
}
2017-02-18 13:10:47 +03:00
if v.Kind() != reflect.Ptr {
return fmt.Errorf("redis: ScanSlice(non-pointer %T)", slice)
2017-02-01 11:36:33 +03:00
}
2017-02-18 13:10:47 +03:00
v = v.Elem()
if v.Kind() != reflect.Slice {
return fmt.Errorf("redis: ScanSlice(non-slice %T)", slice)
2017-02-01 11:36:33 +03:00
}
2018-02-22 15:14:30 +03:00
next := makeSliceNextElemFunc(v)
2017-02-18 13:10:47 +03:00
for i, s := range data {
2017-11-01 16:33:53 +03:00
elem := next()
2018-01-15 17:15:20 +03:00
if err := Scan([]byte(s), elem.Addr().Interface()); err != nil {
2020-11-17 17:08:15 +03:00
err = fmt.Errorf("redis: ScanSlice index=%d value=%q failed: %w", i, s, err)
2018-01-15 17:15:20 +03:00
return err
2017-02-01 11:36:33 +03:00
}
}
2017-02-18 13:10:47 +03:00
2017-02-01 11:36:33 +03:00
return nil
}
2018-02-22 15:14:30 +03:00
func makeSliceNextElemFunc(v reflect.Value) func() reflect.Value {
elemType := v.Type().Elem()
if elemType.Kind() == reflect.Ptr {
elemType = elemType.Elem()
return func() reflect.Value {
if v.Len() < v.Cap() {
v.Set(v.Slice(0, v.Len()+1))
elem := v.Index(v.Len() - 1)
if elem.IsNil() {
elem.Set(reflect.New(elemType))
}
return elem.Elem()
}
elem := reflect.New(elemType)
v.Set(reflect.Append(v, elem))
return elem.Elem()
}
}
zero := reflect.Zero(elemType)
return func() reflect.Value {
if v.Len() < v.Cap() {
v.Set(v.Slice(0, v.Len()+1))
return v.Index(v.Len() - 1)
}
v.Set(reflect.Append(v, zero))
return v.Index(v.Len() - 1)
}
}