2021-01-28 08:28:24 +03:00
|
|
|
package hscan
|
|
|
|
|
|
|
|
import (
|
2023-02-07 15:23:39 +03:00
|
|
|
"encoding"
|
2021-02-09 15:44:24 +03:00
|
|
|
"fmt"
|
2021-01-28 08:28:24 +03:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2023-02-07 15:23:39 +03:00
|
|
|
|
|
|
|
"github.com/redis/go-redis/v9/internal/util"
|
2021-01-28 08:28:24 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// structMap contains the map of struct fields for target structs
|
|
|
|
// indexed by the struct type.
|
|
|
|
type structMap struct {
|
|
|
|
m sync.Map
|
|
|
|
}
|
|
|
|
|
|
|
|
func newStructMap() *structMap {
|
2021-01-29 13:04:39 +03:00
|
|
|
return new(structMap)
|
2021-01-28 08:28:24 +03:00
|
|
|
}
|
|
|
|
|
2021-02-03 13:45:02 +03:00
|
|
|
func (s *structMap) get(t reflect.Type) *structSpec {
|
2021-01-29 13:04:39 +03:00
|
|
|
if v, ok := s.m.Load(t); ok {
|
2021-02-03 13:45:02 +03:00
|
|
|
return v.(*structSpec)
|
2021-01-28 08:28:24 +03:00
|
|
|
}
|
|
|
|
|
2021-02-03 13:45:02 +03:00
|
|
|
spec := newStructSpec(t, "redis")
|
|
|
|
s.m.Store(t, spec)
|
|
|
|
return spec
|
2021-01-28 08:28:24 +03:00
|
|
|
}
|
|
|
|
|
2021-02-03 13:45:02 +03:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// structSpec contains the list of all fields in a target struct.
|
|
|
|
type structSpec struct {
|
|
|
|
m map[string]*structField
|
2021-01-28 08:28:24 +03:00
|
|
|
}
|
|
|
|
|
2021-02-03 13:45:02 +03:00
|
|
|
func (s *structSpec) set(tag string, sf *structField) {
|
2021-01-28 08:28:24 +03:00
|
|
|
s.m[tag] = sf
|
|
|
|
}
|
|
|
|
|
2021-02-03 13:45:02 +03:00
|
|
|
func newStructSpec(t reflect.Type, fieldTag string) *structSpec {
|
2021-08-29 19:37:17 +03:00
|
|
|
numField := t.NumField()
|
2021-02-03 13:45:02 +03:00
|
|
|
out := &structSpec{
|
2021-08-29 19:37:17 +03:00
|
|
|
m: make(map[string]*structField, numField),
|
2021-02-03 13:45:02 +03:00
|
|
|
}
|
2021-01-28 08:28:24 +03:00
|
|
|
|
2021-08-29 19:37:17 +03:00
|
|
|
for i := 0; i < numField; i++ {
|
2021-01-29 13:12:47 +03:00
|
|
|
f := t.Field(i)
|
2021-01-28 08:28:24 +03:00
|
|
|
|
2021-02-02 10:34:52 +03:00
|
|
|
tag := f.Tag.Get(fieldTag)
|
2021-01-28 08:28:24 +03:00
|
|
|
if tag == "" || tag == "-" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
tag = strings.Split(tag, ",")[0]
|
|
|
|
if tag == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the built-in decoder.
|
2021-01-29 13:12:47 +03:00
|
|
|
out.set(tag, &structField{index: i, fn: decoders[f.Type.Kind()]})
|
2021-01-28 08:28:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
2021-02-03 13:45:02 +03:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// structField represents a single field in a target struct.
|
|
|
|
type structField struct {
|
|
|
|
index int
|
|
|
|
fn decoderFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type StructValue struct {
|
|
|
|
spec *structSpec
|
|
|
|
value reflect.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s StructValue) Scan(key string, value string) error {
|
|
|
|
field, ok := s.spec.m[key]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2022-12-24 17:29:45 +03:00
|
|
|
|
|
|
|
v := s.value.Field(field.index)
|
2023-02-24 08:47:58 +03:00
|
|
|
isPtr := v.Kind() == reflect.Ptr
|
2022-12-24 17:29:45 +03:00
|
|
|
|
|
|
|
if isPtr && v.IsNil() {
|
|
|
|
v.Set(reflect.New(v.Type().Elem()))
|
|
|
|
}
|
|
|
|
if !isPtr && v.Type().Name() != "" && v.CanAddr() {
|
|
|
|
v = v.Addr()
|
|
|
|
isPtr = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if isPtr && v.Type().NumMethod() > 0 && v.CanInterface() {
|
2023-02-07 15:23:39 +03:00
|
|
|
switch scan := v.Interface().(type) {
|
|
|
|
case Scanner:
|
2022-12-24 17:29:45 +03:00
|
|
|
return scan.ScanRedis(value)
|
2023-02-07 15:23:39 +03:00
|
|
|
case encoding.TextUnmarshaler:
|
|
|
|
return scan.UnmarshalText(util.StringToBytes(value))
|
2022-12-24 17:29:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if isPtr {
|
|
|
|
v = v.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := field.fn(v, value); err != nil {
|
2021-02-09 15:44:24 +03:00
|
|
|
t := s.value.Type()
|
|
|
|
return fmt.Errorf("cannot scan redis.result %s into struct field %s.%s of type %s, error-%s",
|
|
|
|
value, t.Name(), t.Field(field.index).Name, t.Field(field.index).Type, err.Error())
|
|
|
|
}
|
|
|
|
return nil
|
2021-02-03 13:45:02 +03:00
|
|
|
}
|