forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/redis/go-redis/v9/internal/proto/scan.go

342 lines
3.6 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package proto
import (
"encoding"
"fmt"
"net"
"reflect"
"time"
"github.com/redis/go-redis/v9/internal/util"
)
// Scan parses bytes `b` to `v` with appropriate type.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
//nolint:gocyclo
2024-02-18 10:42:21 +00:00
func Scan(b []byte, v interface{}) error {
2024-02-18 10:42:21 +00:00
switch v := v.(type) {
2024-02-18 10:42:21 +00:00
case nil:
2024-02-18 10:42:21 +00:00
return fmt.Errorf("redis: Scan(nil)")
2024-02-18 10:42:21 +00:00
case *string:
2024-02-18 10:42:21 +00:00
*v = util.BytesToString(b)
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
case *[]byte:
2024-02-18 10:42:21 +00:00
*v = b
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
case *int:
2024-02-18 10:42:21 +00:00
var err error
2024-02-18 10:42:21 +00:00
*v, err = util.Atoi(b)
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
case *int8:
2024-02-18 10:42:21 +00:00
n, err := util.ParseInt(b, 10, 8)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
*v = int8(n)
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
case *int16:
2024-02-18 10:42:21 +00:00
n, err := util.ParseInt(b, 10, 16)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
*v = int16(n)
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
case *int32:
2024-02-18 10:42:21 +00:00
n, err := util.ParseInt(b, 10, 32)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
*v = int32(n)
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
case *int64:
2024-02-18 10:42:21 +00:00
n, err := util.ParseInt(b, 10, 64)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
*v = n
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
case *uint:
2024-02-18 10:42:21 +00:00
n, err := util.ParseUint(b, 10, 64)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
*v = uint(n)
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
case *uint8:
2024-02-18 10:42:21 +00:00
n, err := util.ParseUint(b, 10, 8)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
*v = uint8(n)
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
case *uint16:
2024-02-18 10:42:21 +00:00
n, err := util.ParseUint(b, 10, 16)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
*v = uint16(n)
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
case *uint32:
2024-02-18 10:42:21 +00:00
n, err := util.ParseUint(b, 10, 32)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
*v = uint32(n)
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
case *uint64:
2024-02-18 10:42:21 +00:00
n, err := util.ParseUint(b, 10, 64)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
*v = n
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
case *float32:
2024-02-18 10:42:21 +00:00
n, err := util.ParseFloat(b, 32)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
*v = float32(n)
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
case *float64:
2024-02-18 10:42:21 +00:00
var err error
2024-02-18 10:42:21 +00:00
*v, err = util.ParseFloat(b, 64)
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
case *bool:
2024-02-18 10:42:21 +00:00
*v = len(b) == 1 && b[0] == '1'
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
case *time.Time:
2024-02-18 10:42:21 +00:00
var err error
2024-02-18 10:42:21 +00:00
*v, err = time.Parse(time.RFC3339Nano, util.BytesToString(b))
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
case *time.Duration:
2024-02-18 10:42:21 +00:00
n, err := util.ParseInt(b, 10, 64)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
*v = time.Duration(n)
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
case encoding.BinaryUnmarshaler:
2024-02-18 10:42:21 +00:00
return v.UnmarshalBinary(b)
2024-02-18 10:42:21 +00:00
case *net.IP:
2024-02-18 10:42:21 +00:00
*v = b
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return fmt.Errorf(
2024-02-18 10:42:21 +00:00
"redis: can't unmarshal %T (consider implementing BinaryUnmarshaler)", v)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
func ScanSlice(data []string, slice interface{}) error {
2024-02-18 10:42:21 +00:00
v := reflect.ValueOf(slice)
2024-02-18 10:42:21 +00:00
if !v.IsValid() {
2024-02-18 10:42:21 +00:00
return fmt.Errorf("redis: ScanSlice(nil)")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if v.Kind() != reflect.Ptr {
2024-02-18 10:42:21 +00:00
return fmt.Errorf("redis: ScanSlice(non-pointer %T)", slice)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
v = v.Elem()
2024-02-18 10:42:21 +00:00
if v.Kind() != reflect.Slice {
2024-02-18 10:42:21 +00:00
return fmt.Errorf("redis: ScanSlice(non-slice %T)", slice)
2024-02-18 10:42:21 +00:00
}
next := makeSliceNextElemFunc(v)
2024-02-18 10:42:21 +00:00
for i, s := range data {
2024-02-18 10:42:21 +00:00
elem := next()
2024-02-18 10:42:21 +00:00
if err := Scan([]byte(s), elem.Addr().Interface()); err != nil {
2024-02-18 10:42:21 +00:00
err = fmt.Errorf("redis: ScanSlice index=%d value=%q failed: %w", i, s, err)
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
return nil
2024-02-18 10:42:21 +00:00
}
func makeSliceNextElemFunc(v reflect.Value) func() reflect.Value {
2024-02-18 10:42:21 +00:00
elemType := v.Type().Elem()
if elemType.Kind() == reflect.Ptr {
2024-02-18 10:42:21 +00:00
elemType = elemType.Elem()
2024-02-18 10:42:21 +00:00
return func() reflect.Value {
2024-02-18 10:42:21 +00:00
if v.Len() < v.Cap() {
2024-02-18 10:42:21 +00:00
v.Set(v.Slice(0, v.Len()+1))
2024-02-18 10:42:21 +00:00
elem := v.Index(v.Len() - 1)
2024-02-18 10:42:21 +00:00
if elem.IsNil() {
2024-02-18 10:42:21 +00:00
elem.Set(reflect.New(elemType))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return elem.Elem()
2024-02-18 10:42:21 +00:00
}
elem := reflect.New(elemType)
2024-02-18 10:42:21 +00:00
v.Set(reflect.Append(v, elem))
2024-02-18 10:42:21 +00:00
return elem.Elem()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
zero := reflect.Zero(elemType)
2024-02-18 10:42:21 +00:00
return func() reflect.Value {
2024-02-18 10:42:21 +00:00
if v.Len() < v.Cap() {
2024-02-18 10:42:21 +00:00
v.Set(v.Slice(0, v.Len()+1))
2024-02-18 10:42:21 +00:00
return v.Index(v.Len() - 1)
2024-02-18 10:42:21 +00:00
}
v.Set(reflect.Append(v, zero))
2024-02-18 10:42:21 +00:00
return v.Index(v.Len() - 1)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}