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.

conversion.go 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. // Copyright 2017 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package convert
  5. import (
  6. "database/sql"
  7. "database/sql/driver"
  8. "encoding/json"
  9. "errors"
  10. "fmt"
  11. "math/big"
  12. "reflect"
  13. "strconv"
  14. "time"
  15. )
  16. // Conversion is an interface. A type implements Conversion will according
  17. // the custom method to fill into database and retrieve from database.
  18. type Conversion interface {
  19. FromDB([]byte) error
  20. ToDB() ([]byte, error)
  21. }
  22. // ErrNilPtr represents an error
  23. var ErrNilPtr = errors.New("destination pointer is nil") // embedded in descriptive error
  24. func strconvErr(err error) error {
  25. if ne, ok := err.(*strconv.NumError); ok {
  26. return ne.Err
  27. }
  28. return err
  29. }
  30. func cloneBytes(b []byte) []byte {
  31. if b == nil {
  32. return nil
  33. }
  34. c := make([]byte, len(b))
  35. copy(c, b)
  36. return c
  37. }
  38. // Assign copies to dest the value in src, converting it if possible.
  39. // An error is returned if the copy would result in loss of information.
  40. // dest should be a pointer type.
  41. func Assign(dest, src interface{}, originalLocation *time.Location, convertedLocation *time.Location) error {
  42. // Common cases, without reflect.
  43. switch s := src.(type) {
  44. case *interface{}:
  45. return Assign(dest, *s, originalLocation, convertedLocation)
  46. case string:
  47. switch d := dest.(type) {
  48. case *string:
  49. if d == nil {
  50. return ErrNilPtr
  51. }
  52. *d = s
  53. return nil
  54. case *[]byte:
  55. if d == nil {
  56. return ErrNilPtr
  57. }
  58. *d = []byte(s)
  59. return nil
  60. }
  61. case []byte:
  62. switch d := dest.(type) {
  63. case *string:
  64. if d == nil {
  65. return ErrNilPtr
  66. }
  67. *d = string(s)
  68. return nil
  69. case *interface{}:
  70. if d == nil {
  71. return ErrNilPtr
  72. }
  73. *d = cloneBytes(s)
  74. return nil
  75. case *[]byte:
  76. if d == nil {
  77. return ErrNilPtr
  78. }
  79. *d = cloneBytes(s)
  80. return nil
  81. }
  82. case time.Time:
  83. switch d := dest.(type) {
  84. case *string:
  85. *d = s.Format(time.RFC3339Nano)
  86. return nil
  87. case *[]byte:
  88. if d == nil {
  89. return ErrNilPtr
  90. }
  91. *d = []byte(s.Format(time.RFC3339Nano))
  92. return nil
  93. }
  94. case nil:
  95. switch d := dest.(type) {
  96. case *interface{}:
  97. if d == nil {
  98. return ErrNilPtr
  99. }
  100. *d = nil
  101. return nil
  102. case *[]byte:
  103. if d == nil {
  104. return ErrNilPtr
  105. }
  106. *d = nil
  107. return nil
  108. }
  109. case *sql.NullString:
  110. switch d := dest.(type) {
  111. case *int:
  112. if s.Valid {
  113. *d, _ = strconv.Atoi(s.String)
  114. }
  115. return nil
  116. case *int64:
  117. if s.Valid {
  118. *d, _ = strconv.ParseInt(s.String, 10, 64)
  119. }
  120. return nil
  121. case *string:
  122. if s.Valid {
  123. *d = s.String
  124. }
  125. return nil
  126. case *time.Time:
  127. if s.Valid {
  128. var err error
  129. dt, err := String2Time(s.String, originalLocation, convertedLocation)
  130. if err != nil {
  131. return err
  132. }
  133. *d = *dt
  134. }
  135. return nil
  136. case *sql.NullTime:
  137. if s.Valid {
  138. var err error
  139. dt, err := String2Time(s.String, originalLocation, convertedLocation)
  140. if err != nil {
  141. return err
  142. }
  143. d.Valid = true
  144. d.Time = *dt
  145. }
  146. return nil
  147. case *big.Float:
  148. if s.Valid {
  149. if d == nil {
  150. d = big.NewFloat(0)
  151. }
  152. d.SetString(s.String)
  153. }
  154. return nil
  155. }
  156. case *sql.NullInt32:
  157. switch d := dest.(type) {
  158. case *int:
  159. if s.Valid {
  160. *d = int(s.Int32)
  161. }
  162. return nil
  163. case *int8:
  164. if s.Valid {
  165. *d = int8(s.Int32)
  166. }
  167. return nil
  168. case *int16:
  169. if s.Valid {
  170. *d = int16(s.Int32)
  171. }
  172. return nil
  173. case *int32:
  174. if s.Valid {
  175. *d = s.Int32
  176. }
  177. return nil
  178. case *int64:
  179. if s.Valid {
  180. *d = int64(s.Int32)
  181. }
  182. return nil
  183. }
  184. case *sql.NullInt64:
  185. switch d := dest.(type) {
  186. case *int:
  187. if s.Valid {
  188. *d = int(s.Int64)
  189. }
  190. return nil
  191. case *int8:
  192. if s.Valid {
  193. *d = int8(s.Int64)
  194. }
  195. return nil
  196. case *int16:
  197. if s.Valid {
  198. *d = int16(s.Int64)
  199. }
  200. return nil
  201. case *int32:
  202. if s.Valid {
  203. *d = int32(s.Int64)
  204. }
  205. return nil
  206. case *int64:
  207. if s.Valid {
  208. *d = s.Int64
  209. }
  210. return nil
  211. }
  212. case *sql.NullFloat64:
  213. switch d := dest.(type) {
  214. case *int:
  215. if s.Valid {
  216. *d = int(s.Float64)
  217. }
  218. return nil
  219. case *float64:
  220. if s.Valid {
  221. *d = s.Float64
  222. }
  223. return nil
  224. }
  225. case *sql.NullBool:
  226. switch d := dest.(type) {
  227. case *bool:
  228. if s.Valid {
  229. *d = s.Bool
  230. }
  231. return nil
  232. }
  233. case *sql.NullTime:
  234. switch d := dest.(type) {
  235. case *time.Time:
  236. if s.Valid {
  237. *d = s.Time
  238. }
  239. return nil
  240. case *string:
  241. if s.Valid {
  242. *d = s.Time.In(convertedLocation).Format("2006-01-02 15:04:05")
  243. }
  244. return nil
  245. }
  246. case *NullUint32:
  247. switch d := dest.(type) {
  248. case *uint8:
  249. if s.Valid {
  250. *d = uint8(s.Uint32)
  251. }
  252. return nil
  253. case *uint16:
  254. if s.Valid {
  255. *d = uint16(s.Uint32)
  256. }
  257. return nil
  258. case *uint:
  259. if s.Valid {
  260. *d = uint(s.Uint32)
  261. }
  262. return nil
  263. }
  264. case *NullUint64:
  265. switch d := dest.(type) {
  266. case *uint64:
  267. if s.Valid {
  268. *d = s.Uint64
  269. }
  270. return nil
  271. }
  272. case *sql.RawBytes:
  273. switch d := dest.(type) {
  274. case Conversion:
  275. return d.FromDB(*s)
  276. }
  277. }
  278. var sv reflect.Value
  279. switch d := dest.(type) {
  280. case *string:
  281. sv = reflect.ValueOf(src)
  282. switch sv.Kind() {
  283. case reflect.Bool,
  284. reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
  285. reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
  286. reflect.Float32, reflect.Float64:
  287. *d = AsString(src)
  288. return nil
  289. }
  290. case *[]byte:
  291. if b, ok := AsBytes(src); ok {
  292. *d = b
  293. return nil
  294. }
  295. case *bool:
  296. bv, err := driver.Bool.ConvertValue(src)
  297. if err == nil {
  298. *d = bv.(bool)
  299. }
  300. return err
  301. case *interface{}:
  302. *d = src
  303. return nil
  304. }
  305. return AssignValue(reflect.ValueOf(dest), src)
  306. }
  307. var (
  308. scannerTypePlaceHolder sql.Scanner
  309. scannerType = reflect.TypeOf(&scannerTypePlaceHolder).Elem()
  310. )
  311. // AssignValue assign src as dv
  312. func AssignValue(dv reflect.Value, src interface{}) error {
  313. if src == nil {
  314. return nil
  315. }
  316. if v, ok := src.(*interface{}); ok {
  317. return AssignValue(dv, *v)
  318. }
  319. if dv.Type().Implements(scannerType) {
  320. return dv.Interface().(sql.Scanner).Scan(src)
  321. }
  322. switch dv.Kind() {
  323. case reflect.Ptr:
  324. if dv.IsNil() {
  325. dv.Set(reflect.New(dv.Type().Elem()))
  326. }
  327. return AssignValue(dv.Elem(), src)
  328. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  329. i64, err := AsInt64(src)
  330. if err != nil {
  331. err = strconvErr(err)
  332. return fmt.Errorf("converting driver.Value type %T to a %s: %v", src, dv.Kind(), err)
  333. }
  334. dv.SetInt(i64)
  335. return nil
  336. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  337. u64, err := AsUint64(src)
  338. if err != nil {
  339. err = strconvErr(err)
  340. return fmt.Errorf("converting driver.Value type %T to a %s: %v", src, dv.Kind(), err)
  341. }
  342. dv.SetUint(u64)
  343. return nil
  344. case reflect.Float32, reflect.Float64:
  345. f64, err := AsFloat64(src)
  346. if err != nil {
  347. err = strconvErr(err)
  348. return fmt.Errorf("converting driver.Value type %T to a %s: %v", src, dv.Kind(), err)
  349. }
  350. dv.SetFloat(f64)
  351. return nil
  352. case reflect.String:
  353. dv.SetString(AsString(src))
  354. return nil
  355. case reflect.Bool:
  356. b, err := AsBool(src)
  357. if err != nil {
  358. return err
  359. }
  360. dv.SetBool(b)
  361. return nil
  362. case reflect.Slice, reflect.Map, reflect.Struct, reflect.Array:
  363. data, ok := AsBytes(src)
  364. if !ok {
  365. return fmt.Errorf("convert.AssignValue: src cannot be as bytes %#v", src)
  366. }
  367. if data == nil {
  368. return nil
  369. }
  370. if dv.Kind() != reflect.Ptr {
  371. dv = dv.Addr()
  372. }
  373. return json.Unmarshal(data, dv.Interface())
  374. default:
  375. return fmt.Errorf("convert.AssignValue: unsupported Scan, storing driver.Value type %T into type %T", src, dv.Interface())
  376. }
  377. }