2021-01-28 08:28:24 +03:00
|
|
|
package hscan
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
// decoderFunc represents decoding functions for default built-in types.
|
|
|
|
type decoderFunc func(reflect.Value, string) error
|
|
|
|
|
|
|
|
var (
|
2021-02-02 10:34:52 +03:00
|
|
|
// List of built-in decoders indexed by their numeric constant values (eg: reflect.Bool = 1).
|
2021-01-28 08:28:24 +03:00
|
|
|
decoders = []decoderFunc{
|
|
|
|
reflect.Bool: decodeBool,
|
|
|
|
reflect.Int: decodeInt,
|
|
|
|
reflect.Int8: decodeInt,
|
|
|
|
reflect.Int16: decodeInt,
|
|
|
|
reflect.Int32: decodeInt,
|
|
|
|
reflect.Int64: decodeInt,
|
|
|
|
reflect.Uint: decodeUint,
|
|
|
|
reflect.Uint8: decodeUint,
|
|
|
|
reflect.Uint16: decodeUint,
|
|
|
|
reflect.Uint32: decodeUint,
|
|
|
|
reflect.Uint64: decodeUint,
|
|
|
|
reflect.Float32: decodeFloat,
|
|
|
|
reflect.Float64: decodeFloat,
|
|
|
|
reflect.Complex64: decodeUnsupported,
|
|
|
|
reflect.Complex128: decodeUnsupported,
|
|
|
|
reflect.Array: decodeUnsupported,
|
|
|
|
reflect.Chan: decodeUnsupported,
|
|
|
|
reflect.Func: decodeUnsupported,
|
|
|
|
reflect.Interface: decodeUnsupported,
|
|
|
|
reflect.Map: decodeUnsupported,
|
|
|
|
reflect.Ptr: decodeUnsupported,
|
2021-01-29 13:04:39 +03:00
|
|
|
reflect.Slice: decodeSlice,
|
2021-01-28 08:28:24 +03:00
|
|
|
reflect.String: decodeString,
|
|
|
|
reflect.Struct: decodeUnsupported,
|
|
|
|
reflect.UnsafePointer: decodeUnsupported,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Global map of struct field specs that is populated once for every new
|
|
|
|
// struct type that is scanned. This caches the field types and the corresponding
|
|
|
|
// decoder functions to avoid iterating through struct fields on subsequent scans.
|
|
|
|
structSpecs = newStructMap()
|
|
|
|
)
|
|
|
|
|
2021-02-02 13:58:10 +03:00
|
|
|
// Scan scans the results from a key-value Redis map result set to a destination struct.
|
|
|
|
// The Redis keys are matched to the struct's field with the `redis` tag.
|
|
|
|
func Scan(keys []interface{}, vals []interface{}, dest interface{}) error {
|
|
|
|
if len(keys) != len(vals) {
|
|
|
|
return errors.New("args should have the same number of keys and vals")
|
2021-01-28 08:28:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// The destination to scan into should be a struct pointer.
|
|
|
|
v := reflect.ValueOf(dest)
|
|
|
|
if v.Kind() != reflect.Ptr || v.IsNil() {
|
|
|
|
return fmt.Errorf("redis.Scan(non-pointer %T)", dest)
|
|
|
|
}
|
|
|
|
v = v.Elem()
|
|
|
|
|
|
|
|
if v.Kind() != reflect.Struct {
|
|
|
|
return fmt.Errorf("redis.Scan(non-struct %T)", dest)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the struct field spec is not cached, build and cache it to avoid
|
|
|
|
// iterating through the fields of a struct type every time values are
|
|
|
|
// scanned into it.
|
|
|
|
typ := v.Type()
|
2021-01-29 13:04:39 +03:00
|
|
|
fMap := structSpecs.get(typ)
|
2021-01-28 08:28:24 +03:00
|
|
|
|
|
|
|
// Iterate through the (key, value) sequence.
|
2021-02-02 13:58:10 +03:00
|
|
|
for i := 0; i < len(vals); i++ {
|
|
|
|
key, ok := keys[i].(string)
|
2021-01-28 08:28:24 +03:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-02-02 13:58:10 +03:00
|
|
|
val, ok := vals[i].(string)
|
2021-01-28 08:28:24 +03:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the field name is in the field spec map.
|
|
|
|
field, ok := fMap.get(key)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := field.fn(v.Field(field.index), val); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeBool(f reflect.Value, s string) error {
|
|
|
|
b, err := strconv.ParseBool(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.SetBool(b)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeInt(f reflect.Value, s string) error {
|
|
|
|
v, err := strconv.ParseInt(s, 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.SetInt(v)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeUint(f reflect.Value, s string) error {
|
|
|
|
v, err := strconv.ParseUint(s, 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.SetUint(v)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeFloat(f reflect.Value, s string) error {
|
|
|
|
v, err := strconv.ParseFloat(s, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.SetFloat(v)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeString(f reflect.Value, s string) error {
|
|
|
|
f.SetString(s)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:04:39 +03:00
|
|
|
func decodeSlice(f reflect.Value, s string) error {
|
2021-01-28 08:28:24 +03:00
|
|
|
// []byte slice ([]uint8).
|
|
|
|
if f.Type().Elem().Kind() == reflect.Uint8 {
|
|
|
|
f.SetBytes([]byte(s))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:04:39 +03:00
|
|
|
func decodeUnsupported(v reflect.Value, s string) error {
|
|
|
|
return fmt.Errorf("redis.Scan(unsupported %s)", v.Type())
|
2021-01-28 08:28:24 +03:00
|
|
|
}
|