orm.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // copy from core/stores/sqlx/orm.go
  2. package mocksql
  3. import (
  4. "errors"
  5. "reflect"
  6. "strings"
  7. "git.i2edu.net/i2/go-zero/core/mapping"
  8. )
  9. const tagName = "db"
  10. var (
  11. // ErrNotMatchDestination defines an error for mismatching case
  12. ErrNotMatchDestination = errors.New("not matching destination to scan")
  13. // ErrNotReadableValue defines an error for the value is not addressable or interfaceable
  14. ErrNotReadableValue = errors.New("value not addressable or interfaceable")
  15. // ErrNotSettable defines an error for the variable is not settable
  16. ErrNotSettable = errors.New("passed in variable is not settable")
  17. // ErrUnsupportedValueType deinfes an error for unsupported unmarshal type
  18. ErrUnsupportedValueType = errors.New("unsupported unmarshal type")
  19. )
  20. type rowsScanner interface {
  21. Columns() ([]string, error)
  22. Err() error
  23. Next() bool
  24. Scan(v ...interface{}) error
  25. }
  26. func getTaggedFieldValueMap(v reflect.Value) (map[string]interface{}, error) {
  27. rt := mapping.Deref(v.Type())
  28. size := rt.NumField()
  29. result := make(map[string]interface{}, size)
  30. for i := 0; i < size; i++ {
  31. key := parseTagName(rt.Field(i))
  32. if len(key) == 0 {
  33. return nil, nil
  34. }
  35. valueField := reflect.Indirect(v).Field(i)
  36. switch valueField.Kind() {
  37. case reflect.Ptr:
  38. if !valueField.CanInterface() {
  39. return nil, ErrNotReadableValue
  40. }
  41. if valueField.IsNil() {
  42. baseValueType := mapping.Deref(valueField.Type())
  43. valueField.Set(reflect.New(baseValueType))
  44. }
  45. result[key] = valueField.Interface()
  46. default:
  47. if !valueField.CanAddr() || !valueField.Addr().CanInterface() {
  48. return nil, ErrNotReadableValue
  49. }
  50. result[key] = valueField.Addr().Interface()
  51. }
  52. }
  53. return result, nil
  54. }
  55. func mapStructFieldsIntoSlice(v reflect.Value, columns []string, strict bool) ([]interface{}, error) {
  56. fields := unwrapFields(v)
  57. if strict && len(columns) < len(fields) {
  58. return nil, ErrNotMatchDestination
  59. }
  60. taggedMap, err := getTaggedFieldValueMap(v)
  61. if err != nil {
  62. return nil, err
  63. }
  64. values := make([]interface{}, len(columns))
  65. if len(taggedMap) == 0 {
  66. for i := 0; i < len(values); i++ {
  67. valueField := fields[i]
  68. switch valueField.Kind() {
  69. case reflect.Ptr:
  70. if !valueField.CanInterface() {
  71. return nil, ErrNotReadableValue
  72. }
  73. if valueField.IsNil() {
  74. baseValueType := mapping.Deref(valueField.Type())
  75. valueField.Set(reflect.New(baseValueType))
  76. }
  77. values[i] = valueField.Interface()
  78. default:
  79. if !valueField.CanAddr() || !valueField.Addr().CanInterface() {
  80. return nil, ErrNotReadableValue
  81. }
  82. values[i] = valueField.Addr().Interface()
  83. }
  84. }
  85. } else {
  86. for i, column := range columns {
  87. if tagged, ok := taggedMap[column]; ok {
  88. values[i] = tagged
  89. } else {
  90. var anonymous interface{}
  91. values[i] = &anonymous
  92. }
  93. }
  94. }
  95. return values, nil
  96. }
  97. func parseTagName(field reflect.StructField) string {
  98. key := field.Tag.Get(tagName)
  99. if len(key) == 0 {
  100. return ""
  101. }
  102. options := strings.Split(key, ",")
  103. return options[0]
  104. }
  105. func unmarshalRow(v interface{}, scanner rowsScanner, strict bool) error {
  106. if !scanner.Next() {
  107. if err := scanner.Err(); err != nil {
  108. return err
  109. }
  110. return ErrNotFound
  111. }
  112. rv := reflect.ValueOf(v)
  113. if err := mapping.ValidatePtr(&rv); err != nil {
  114. return err
  115. }
  116. rte := reflect.TypeOf(v).Elem()
  117. rve := rv.Elem()
  118. switch rte.Kind() {
  119. case reflect.Bool,
  120. reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
  121. reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
  122. reflect.Float32, reflect.Float64,
  123. reflect.String:
  124. if rve.CanSet() {
  125. return scanner.Scan(v)
  126. }
  127. return ErrNotSettable
  128. case reflect.Struct:
  129. columns, err := scanner.Columns()
  130. if err != nil {
  131. return err
  132. }
  133. values, err := mapStructFieldsIntoSlice(rve, columns, strict)
  134. if err != nil {
  135. return err
  136. }
  137. return scanner.Scan(values...)
  138. default:
  139. return ErrUnsupportedValueType
  140. }
  141. }
  142. func unmarshalRows(v interface{}, scanner rowsScanner, strict bool) error {
  143. rv := reflect.ValueOf(v)
  144. if err := mapping.ValidatePtr(&rv); err != nil {
  145. return err
  146. }
  147. rt := reflect.TypeOf(v)
  148. rte := rt.Elem()
  149. rve := rv.Elem()
  150. switch rte.Kind() {
  151. case reflect.Slice:
  152. if rve.CanSet() {
  153. ptr := rte.Elem().Kind() == reflect.Ptr
  154. appendFn := func(item reflect.Value) {
  155. if ptr {
  156. rve.Set(reflect.Append(rve, item))
  157. } else {
  158. rve.Set(reflect.Append(rve, reflect.Indirect(item)))
  159. }
  160. }
  161. fillFn := func(value interface{}) error {
  162. if rve.CanSet() {
  163. if err := scanner.Scan(value); err != nil {
  164. return err
  165. }
  166. appendFn(reflect.ValueOf(value))
  167. return nil
  168. }
  169. return ErrNotSettable
  170. }
  171. base := mapping.Deref(rte.Elem())
  172. switch base.Kind() {
  173. case reflect.Bool,
  174. reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
  175. reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
  176. reflect.Float32, reflect.Float64,
  177. reflect.String:
  178. for scanner.Next() {
  179. value := reflect.New(base)
  180. if err := fillFn(value.Interface()); err != nil {
  181. return err
  182. }
  183. }
  184. case reflect.Struct:
  185. columns, err := scanner.Columns()
  186. if err != nil {
  187. return err
  188. }
  189. for scanner.Next() {
  190. value := reflect.New(base)
  191. values, err := mapStructFieldsIntoSlice(value, columns, strict)
  192. if err != nil {
  193. return err
  194. }
  195. if err := scanner.Scan(values...); err != nil {
  196. return err
  197. }
  198. appendFn(value)
  199. }
  200. default:
  201. return ErrUnsupportedValueType
  202. }
  203. return nil
  204. }
  205. return ErrNotSettable
  206. default:
  207. return ErrUnsupportedValueType
  208. }
  209. }
  210. func unwrapFields(v reflect.Value) []reflect.Value {
  211. var fields []reflect.Value
  212. indirect := reflect.Indirect(v)
  213. for i := 0; i < indirect.NumField(); i++ {
  214. child := indirect.Field(i)
  215. if child.Kind() == reflect.Ptr && child.IsNil() {
  216. baseValueType := mapping.Deref(child.Type())
  217. child.Set(reflect.New(baseValueType))
  218. }
  219. child = reflect.Indirect(child)
  220. childType := indirect.Type().Field(i)
  221. if child.Kind() == reflect.Struct && childType.Anonymous {
  222. fields = append(fields, unwrapFields(child)...)
  223. } else {
  224. fields = append(fields, child)
  225. }
  226. }
  227. return fields
  228. }