You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
1.9 KiB

  1. package hscan
  2. import (
  3. "fmt"
  4. "reflect"
  5. "strings"
  6. "sync"
  7. )
  8. // structMap contains the map of struct fields for target structs
  9. // indexed by the struct type.
  10. type structMap struct {
  11. m sync.Map
  12. }
  13. func newStructMap() *structMap {
  14. return new(structMap)
  15. }
  16. func (s *structMap) get(t reflect.Type) *structSpec {
  17. if v, ok := s.m.Load(t); ok {
  18. return v.(*structSpec)
  19. }
  20. spec := newStructSpec(t, "redis")
  21. s.m.Store(t, spec)
  22. return spec
  23. }
  24. //------------------------------------------------------------------------------
  25. // structSpec contains the list of all fields in a target struct.
  26. type structSpec struct {
  27. m map[string]*structField
  28. }
  29. func (s *structSpec) set(tag string, sf *structField) {
  30. s.m[tag] = sf
  31. }
  32. func newStructSpec(t reflect.Type, fieldTag string) *structSpec {
  33. numField := t.NumField()
  34. out := &structSpec{
  35. m: make(map[string]*structField, numField),
  36. }
  37. for i := 0; i < numField; i++ {
  38. f := t.Field(i)
  39. tag := f.Tag.Get(fieldTag)
  40. if tag == "" || tag == "-" {
  41. continue
  42. }
  43. tag = strings.Split(tag, ",")[0]
  44. if tag == "" {
  45. continue
  46. }
  47. // Use the built-in decoder.
  48. out.set(tag, &structField{index: i, fn: decoders[f.Type.Kind()]})
  49. }
  50. return out
  51. }
  52. //------------------------------------------------------------------------------
  53. // structField represents a single field in a target struct.
  54. type structField struct {
  55. index int
  56. fn decoderFunc
  57. }
  58. //------------------------------------------------------------------------------
  59. type StructValue struct {
  60. spec *structSpec
  61. value reflect.Value
  62. }
  63. func (s StructValue) Scan(key string, value string) error {
  64. field, ok := s.spec.m[key]
  65. if !ok {
  66. return nil
  67. }
  68. if err := field.fn(s.value.Field(field.index), value); err != nil {
  69. t := s.value.Type()
  70. return fmt.Errorf("cannot scan redis.result %s into struct field %s.%s of type %s, error-%s",
  71. value, t.Name(), t.Field(field.index).Name, t.Field(field.index).Type, err.Error())
  72. }
  73. return nil
  74. }