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

185 lines
2.6 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package hscan
import (
"encoding"
"fmt"
"reflect"
"strings"
"sync"
"github.com/redis/go-redis/v9/internal/util"
)
// structMap contains the map of struct fields for target structs
2024-02-18 10:42:21 +00:00
// indexed by the struct type.
2024-02-18 10:42:21 +00:00
type structMap struct {
m sync.Map
}
func newStructMap() *structMap {
2024-02-18 10:42:21 +00:00
return new(structMap)
2024-02-18 10:42:21 +00:00
}
func (s *structMap) get(t reflect.Type) *structSpec {
2024-02-18 10:42:21 +00:00
if v, ok := s.m.Load(t); ok {
2024-02-18 10:42:21 +00:00
return v.(*structSpec)
2024-02-18 10:42:21 +00:00
}
spec := newStructSpec(t, "redis")
2024-02-18 10:42:21 +00:00
s.m.Store(t, spec)
2024-02-18 10:42:21 +00:00
return spec
2024-02-18 10:42:21 +00:00
}
//------------------------------------------------------------------------------
// structSpec contains the list of all fields in a target struct.
2024-02-18 10:42:21 +00:00
type structSpec struct {
m map[string]*structField
}
func (s *structSpec) set(tag string, sf *structField) {
2024-02-18 10:42:21 +00:00
s.m[tag] = sf
2024-02-18 10:42:21 +00:00
}
func newStructSpec(t reflect.Type, fieldTag string) *structSpec {
2024-02-18 10:42:21 +00:00
numField := t.NumField()
2024-02-18 10:42:21 +00:00
out := &structSpec{
2024-02-18 10:42:21 +00:00
m: make(map[string]*structField, numField),
}
for i := 0; i < numField; i++ {
2024-02-18 10:42:21 +00:00
f := t.Field(i)
tag := f.Tag.Get(fieldTag)
2024-02-18 10:42:21 +00:00
if tag == "" || tag == "-" {
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
tag = strings.Split(tag, ",")[0]
2024-02-18 10:42:21 +00:00
if tag == "" {
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
// Use the built-in decoder.
2024-02-18 10:42:21 +00:00
kind := f.Type.Kind()
2024-02-18 10:42:21 +00:00
if kind == reflect.Pointer {
2024-02-18 10:42:21 +00:00
kind = f.Type.Elem().Kind()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
out.set(tag, &structField{index: i, fn: decoders[kind]})
2024-02-18 10:42:21 +00:00
}
return out
2024-02-18 10:42:21 +00:00
}
//------------------------------------------------------------------------------
// structField represents a single field in a target struct.
2024-02-18 10:42:21 +00:00
type structField struct {
index int
fn decoderFunc
2024-02-18 10:42:21 +00:00
}
//------------------------------------------------------------------------------
type StructValue struct {
spec *structSpec
2024-02-18 10:42:21 +00:00
value reflect.Value
}
func (s StructValue) Scan(key string, value string) error {
2024-02-18 10:42:21 +00:00
field, ok := s.spec.m[key]
2024-02-18 10:42:21 +00:00
if !ok {
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
v := s.value.Field(field.index)
2024-02-18 10:42:21 +00:00
isPtr := v.Kind() == reflect.Ptr
if isPtr && v.IsNil() {
2024-02-18 10:42:21 +00:00
v.Set(reflect.New(v.Type().Elem()))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !isPtr && v.Type().Name() != "" && v.CanAddr() {
2024-02-18 10:42:21 +00:00
v = v.Addr()
2024-02-18 10:42:21 +00:00
isPtr = true
2024-02-18 10:42:21 +00:00
}
if isPtr && v.Type().NumMethod() > 0 && v.CanInterface() {
2024-02-18 10:42:21 +00:00
switch scan := v.Interface().(type) {
2024-02-18 10:42:21 +00:00
case Scanner:
2024-02-18 10:42:21 +00:00
return scan.ScanRedis(value)
2024-02-18 10:42:21 +00:00
case encoding.TextUnmarshaler:
2024-02-18 10:42:21 +00:00
return scan.UnmarshalText(util.StringToBytes(value))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
if isPtr {
2024-02-18 10:42:21 +00:00
v = v.Elem()
2024-02-18 10:42:21 +00:00
}
if err := field.fn(v, value); err != nil {
2024-02-18 10:42:21 +00:00
t := s.value.Type()
2024-02-18 10:42:21 +00:00
return fmt.Errorf("cannot scan redis.result %s into struct field %s.%s of type %s, error-%s",
2024-02-18 10:42:21 +00:00
value, t.Name(), t.Field(field.index).Name, t.Field(field.index).Type, err.Error())
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
}