scan.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package proto
  2. import (
  3. "encoding"
  4. "fmt"
  5. "reflect"
  6. "github.com/go-redis/redis/internal/util"
  7. )
  8. func Scan(b []byte, v interface{}) error {
  9. switch v := v.(type) {
  10. case nil:
  11. return fmt.Errorf("redis: Scan(nil)")
  12. case *string:
  13. *v = util.BytesToString(b)
  14. return nil
  15. case *[]byte:
  16. *v = b
  17. return nil
  18. case *int:
  19. var err error
  20. *v, err = util.Atoi(b)
  21. return err
  22. case *int8:
  23. n, err := util.ParseInt(b, 10, 8)
  24. if err != nil {
  25. return err
  26. }
  27. *v = int8(n)
  28. return nil
  29. case *int16:
  30. n, err := util.ParseInt(b, 10, 16)
  31. if err != nil {
  32. return err
  33. }
  34. *v = int16(n)
  35. return nil
  36. case *int32:
  37. n, err := util.ParseInt(b, 10, 32)
  38. if err != nil {
  39. return err
  40. }
  41. *v = int32(n)
  42. return nil
  43. case *int64:
  44. n, err := util.ParseInt(b, 10, 64)
  45. if err != nil {
  46. return err
  47. }
  48. *v = n
  49. return nil
  50. case *uint:
  51. n, err := util.ParseUint(b, 10, 64)
  52. if err != nil {
  53. return err
  54. }
  55. *v = uint(n)
  56. return nil
  57. case *uint8:
  58. n, err := util.ParseUint(b, 10, 8)
  59. if err != nil {
  60. return err
  61. }
  62. *v = uint8(n)
  63. return nil
  64. case *uint16:
  65. n, err := util.ParseUint(b, 10, 16)
  66. if err != nil {
  67. return err
  68. }
  69. *v = uint16(n)
  70. return nil
  71. case *uint32:
  72. n, err := util.ParseUint(b, 10, 32)
  73. if err != nil {
  74. return err
  75. }
  76. *v = uint32(n)
  77. return nil
  78. case *uint64:
  79. n, err := util.ParseUint(b, 10, 64)
  80. if err != nil {
  81. return err
  82. }
  83. *v = n
  84. return nil
  85. case *float32:
  86. n, err := util.ParseFloat(b, 32)
  87. if err != nil {
  88. return err
  89. }
  90. *v = float32(n)
  91. return err
  92. case *float64:
  93. var err error
  94. *v, err = util.ParseFloat(b, 64)
  95. return err
  96. case *bool:
  97. *v = len(b) == 1 && b[0] == '1'
  98. return nil
  99. case encoding.BinaryUnmarshaler:
  100. return v.UnmarshalBinary(b)
  101. default:
  102. return fmt.Errorf(
  103. "redis: can't unmarshal %T (consider implementing BinaryUnmarshaler)", v)
  104. }
  105. }
  106. func ScanSlice(data []string, slice interface{}) error {
  107. v := reflect.ValueOf(slice)
  108. if !v.IsValid() {
  109. return fmt.Errorf("redis: ScanSlice(nil)")
  110. }
  111. if v.Kind() != reflect.Ptr {
  112. return fmt.Errorf("redis: ScanSlice(non-pointer %T)", slice)
  113. }
  114. v = v.Elem()
  115. if v.Kind() != reflect.Slice {
  116. return fmt.Errorf("redis: ScanSlice(non-slice %T)", slice)
  117. }
  118. next := makeSliceNextElemFunc(v)
  119. for i, s := range data {
  120. elem := next()
  121. if err := Scan([]byte(s), elem.Addr().Interface()); err != nil {
  122. err = fmt.Errorf("redis: ScanSlice index=%d value=%q failed: %s", i, s, err)
  123. return err
  124. }
  125. }
  126. return nil
  127. }
  128. func makeSliceNextElemFunc(v reflect.Value) func() reflect.Value {
  129. elemType := v.Type().Elem()
  130. if elemType.Kind() == reflect.Ptr {
  131. elemType = elemType.Elem()
  132. return func() reflect.Value {
  133. if v.Len() < v.Cap() {
  134. v.Set(v.Slice(0, v.Len()+1))
  135. elem := v.Index(v.Len() - 1)
  136. if elem.IsNil() {
  137. elem.Set(reflect.New(elemType))
  138. }
  139. return elem.Elem()
  140. }
  141. elem := reflect.New(elemType)
  142. v.Set(reflect.Append(v, elem))
  143. return elem.Elem()
  144. }
  145. }
  146. zero := reflect.Zero(elemType)
  147. return func() reflect.Value {
  148. if v.Len() < v.Cap() {
  149. v.Set(v.Slice(0, v.Len()+1))
  150. return v.Index(v.Len() - 1)
  151. }
  152. v.Set(reflect.Append(v, zero))
  153. return v.Index(v.Len() - 1)
  154. }
  155. }