gconv.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. // Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. // Package gconv implements powerful and convenient converting functionality for any types of variables.
  7. //
  8. // This package should keep much less dependencies with other packages.
  9. package gconv
  10. import (
  11. "fmt"
  12. "github.com/gogf/gf/internal/json"
  13. "github.com/gogf/gf/os/gtime"
  14. "reflect"
  15. "strconv"
  16. "strings"
  17. "time"
  18. "github.com/gogf/gf/encoding/gbinary"
  19. )
  20. type (
  21. // errorStack is the interface for Stack feature.
  22. errorStack interface {
  23. Error() string
  24. Stack() string
  25. }
  26. )
  27. var (
  28. // Empty strings.
  29. emptyStringMap = map[string]struct{}{
  30. "": {},
  31. "0": {},
  32. "no": {},
  33. "off": {},
  34. "false": {},
  35. }
  36. // Priority tags for Map*/Struct* functions.
  37. // Note, the "gconv", "param", "params" tags are used by old version of package.
  38. // It is strongly recommended using short tag "c" or "p" instead in the future.
  39. StructTagPriority = []string{"gconv", "param", "params", "c", "p", "json"}
  40. )
  41. // Convert converts the variable <i> to the type <t>, the type <t> is specified by string.
  42. // The optional parameter <params> is used for additional necessary parameter for this conversion.
  43. // It supports common types conversion as its conversion based on type name string.
  44. func Convert(i interface{}, t string, params ...interface{}) interface{} {
  45. switch t {
  46. case "int":
  47. return Int(i)
  48. case "*int":
  49. if _, ok := i.(*int); ok {
  50. return i
  51. }
  52. v := Int(i)
  53. return &v
  54. case "int8":
  55. return Int8(i)
  56. case "*int8":
  57. if _, ok := i.(*int8); ok {
  58. return i
  59. }
  60. v := Int8(i)
  61. return &v
  62. case "int16":
  63. return Int16(i)
  64. case "*int16":
  65. if _, ok := i.(*int16); ok {
  66. return i
  67. }
  68. v := Int16(i)
  69. return &v
  70. case "int32":
  71. return Int32(i)
  72. case "*int32":
  73. if _, ok := i.(*int32); ok {
  74. return i
  75. }
  76. v := Int32(i)
  77. return &v
  78. case "int64":
  79. return Int64(i)
  80. case "*int64":
  81. if _, ok := i.(*int64); ok {
  82. return i
  83. }
  84. v := Int64(i)
  85. return &v
  86. case "uint":
  87. return Uint(i)
  88. case "*uint":
  89. if _, ok := i.(*uint); ok {
  90. return i
  91. }
  92. v := Uint(i)
  93. return &v
  94. case "uint8":
  95. return Uint8(i)
  96. case "*uint8":
  97. if _, ok := i.(*uint8); ok {
  98. return i
  99. }
  100. v := Uint8(i)
  101. return &v
  102. case "uint16":
  103. return Uint16(i)
  104. case "*uint16":
  105. if _, ok := i.(*uint16); ok {
  106. return i
  107. }
  108. v := Uint16(i)
  109. return &v
  110. case "uint32":
  111. return Uint32(i)
  112. case "*uint32":
  113. if _, ok := i.(*uint32); ok {
  114. return i
  115. }
  116. v := Uint32(i)
  117. return &v
  118. case "uint64":
  119. return Uint64(i)
  120. case "*uint64":
  121. if _, ok := i.(*uint64); ok {
  122. return i
  123. }
  124. v := Uint64(i)
  125. return &v
  126. case "float32":
  127. return Float32(i)
  128. case "*float32":
  129. if _, ok := i.(*float32); ok {
  130. return i
  131. }
  132. v := Float32(i)
  133. return &v
  134. case "float64":
  135. return Float64(i)
  136. case "*float64":
  137. if _, ok := i.(*float64); ok {
  138. return i
  139. }
  140. v := Float64(i)
  141. return &v
  142. case "bool":
  143. return Bool(i)
  144. case "*bool":
  145. if _, ok := i.(*bool); ok {
  146. return i
  147. }
  148. v := Bool(i)
  149. return &v
  150. case "string":
  151. return String(i)
  152. case "*string":
  153. if _, ok := i.(*string); ok {
  154. return i
  155. }
  156. v := String(i)
  157. return &v
  158. case "[]byte":
  159. return Bytes(i)
  160. case "[]int":
  161. return Ints(i)
  162. case "[]int32":
  163. return Int32s(i)
  164. case "[]int64":
  165. return Int64s(i)
  166. case "[]uint":
  167. return Uints(i)
  168. case "[]uint32":
  169. return Uint32s(i)
  170. case "[]uint64":
  171. return Uint64s(i)
  172. case "[]float32":
  173. return Float32s(i)
  174. case "[]float64":
  175. return Float64s(i)
  176. case "[]string":
  177. return Strings(i)
  178. case "Time", "time.Time":
  179. if len(params) > 0 {
  180. return Time(i, String(params[0]))
  181. }
  182. return Time(i)
  183. case "*time.Time":
  184. var v interface{}
  185. if len(params) > 0 {
  186. v = Time(i, String(params[0]))
  187. } else {
  188. if _, ok := i.(*time.Time); ok {
  189. return i
  190. }
  191. v = Time(i)
  192. }
  193. return &v
  194. case "GTime", "gtime.Time":
  195. if len(params) > 0 {
  196. if v := GTime(i, String(params[0])); v != nil {
  197. return *v
  198. } else {
  199. return *gtime.New()
  200. }
  201. }
  202. if v := GTime(i); v != nil {
  203. return *v
  204. } else {
  205. return *gtime.New()
  206. }
  207. case "*gtime.Time":
  208. if len(params) > 0 {
  209. if v := GTime(i, String(params[0])); v != nil {
  210. return v
  211. } else {
  212. return gtime.New()
  213. }
  214. }
  215. if v := GTime(i); v != nil {
  216. return v
  217. } else {
  218. return gtime.New()
  219. }
  220. case "Duration", "time.Duration":
  221. return Duration(i)
  222. case "*time.Duration":
  223. if _, ok := i.(*time.Duration); ok {
  224. return i
  225. }
  226. v := Duration(i)
  227. return &v
  228. case "map[string]string":
  229. return MapStrStr(i)
  230. case "map[string]interface{}":
  231. return Map(i)
  232. case "[]map[string]interface{}":
  233. return Maps(i)
  234. //case "gvar.Var":
  235. // // TODO remove reflect usage to create gvar.Var, considering using unsafe pointer
  236. // rv := reflect.New(intstore.ReflectTypeVarImp)
  237. // ri := rv.Interface()
  238. // if v, ok := ri.(apiSet); ok {
  239. // v.Set(i)
  240. // } else if v, ok := ri.(apiUnmarshalValue); ok {
  241. // v.UnmarshalValue(i)
  242. // } else {
  243. // rv.Set(reflect.ValueOf(i))
  244. // }
  245. // return ri
  246. default:
  247. return i
  248. }
  249. }
  250. // Byte converts <i> to byte.
  251. func Byte(i interface{}) byte {
  252. if v, ok := i.(byte); ok {
  253. return v
  254. }
  255. return Uint8(i)
  256. }
  257. // Bytes converts <i> to []byte.
  258. func Bytes(i interface{}) []byte {
  259. if i == nil {
  260. return nil
  261. }
  262. switch value := i.(type) {
  263. case string:
  264. return []byte(value)
  265. case []byte:
  266. return value
  267. default:
  268. return gbinary.Encode(i)
  269. }
  270. }
  271. // Rune converts <i> to rune.
  272. func Rune(i interface{}) rune {
  273. if v, ok := i.(rune); ok {
  274. return v
  275. }
  276. return rune(Int32(i))
  277. }
  278. // Runes converts <i> to []rune.
  279. func Runes(i interface{}) []rune {
  280. if v, ok := i.([]rune); ok {
  281. return v
  282. }
  283. return []rune(String(i))
  284. }
  285. // String converts <i> to string.
  286. // It's most common used converting function.
  287. func String(i interface{}) string {
  288. if i == nil {
  289. return ""
  290. }
  291. switch value := i.(type) {
  292. case int:
  293. return strconv.Itoa(value)
  294. case int8:
  295. return strconv.Itoa(int(value))
  296. case int16:
  297. return strconv.Itoa(int(value))
  298. case int32:
  299. return strconv.Itoa(int(value))
  300. case int64:
  301. return strconv.FormatInt(value, 10)
  302. case uint:
  303. return strconv.FormatUint(uint64(value), 10)
  304. case uint8:
  305. return strconv.FormatUint(uint64(value), 10)
  306. case uint16:
  307. return strconv.FormatUint(uint64(value), 10)
  308. case uint32:
  309. return strconv.FormatUint(uint64(value), 10)
  310. case uint64:
  311. return strconv.FormatUint(value, 10)
  312. case float32:
  313. return strconv.FormatFloat(float64(value), 'f', -1, 32)
  314. case float64:
  315. return strconv.FormatFloat(value, 'f', -1, 64)
  316. case bool:
  317. return strconv.FormatBool(value)
  318. case string:
  319. return value
  320. case []byte:
  321. return string(value)
  322. case time.Time:
  323. if value.IsZero() {
  324. return ""
  325. }
  326. return value.String()
  327. case *time.Time:
  328. if value == nil {
  329. return ""
  330. }
  331. return value.String()
  332. case gtime.Time:
  333. if value.IsZero() {
  334. return ""
  335. }
  336. return value.String()
  337. case *gtime.Time:
  338. if value == nil {
  339. return ""
  340. }
  341. return value.String()
  342. default:
  343. // Empty checks.
  344. if value == nil {
  345. return ""
  346. }
  347. if f, ok := value.(apiString); ok {
  348. // If the variable implements the String() interface,
  349. // then use that interface to perform the conversion
  350. return f.String()
  351. }
  352. if f, ok := value.(apiError); ok {
  353. // If the variable implements the Error() interface,
  354. // then use that interface to perform the conversion
  355. return f.Error()
  356. }
  357. // Reflect checks.
  358. var (
  359. rv = reflect.ValueOf(value)
  360. kind = rv.Kind()
  361. )
  362. switch kind {
  363. case reflect.Chan,
  364. reflect.Map,
  365. reflect.Slice,
  366. reflect.Func,
  367. reflect.Ptr,
  368. reflect.Interface,
  369. reflect.UnsafePointer:
  370. if rv.IsNil() {
  371. return ""
  372. }
  373. case reflect.String:
  374. return rv.String()
  375. }
  376. if kind == reflect.Ptr {
  377. return String(rv.Elem().Interface())
  378. }
  379. // Finally we use json.Marshal to convert.
  380. if jsonContent, err := json.Marshal(value); err != nil {
  381. return fmt.Sprint(value)
  382. } else {
  383. return string(jsonContent)
  384. }
  385. }
  386. }
  387. // Bool converts <i> to bool.
  388. // It returns false if <i> is: false, "", 0, "false", "off", "no", empty slice/map.
  389. func Bool(i interface{}) bool {
  390. if i == nil {
  391. return false
  392. }
  393. switch value := i.(type) {
  394. case bool:
  395. return value
  396. case []byte:
  397. if _, ok := emptyStringMap[strings.ToLower(string(value))]; ok {
  398. return false
  399. }
  400. return true
  401. case string:
  402. if _, ok := emptyStringMap[strings.ToLower(value)]; ok {
  403. return false
  404. }
  405. return true
  406. default:
  407. rv := reflect.ValueOf(i)
  408. switch rv.Kind() {
  409. case reflect.Ptr:
  410. return !rv.IsNil()
  411. case reflect.Map:
  412. fallthrough
  413. case reflect.Array:
  414. fallthrough
  415. case reflect.Slice:
  416. return rv.Len() != 0
  417. case reflect.Struct:
  418. return true
  419. default:
  420. s := strings.ToLower(String(i))
  421. if _, ok := emptyStringMap[s]; ok {
  422. return false
  423. }
  424. return true
  425. }
  426. }
  427. }
  428. // Int converts <i> to int.
  429. func Int(i interface{}) int {
  430. if i == nil {
  431. return 0
  432. }
  433. if v, ok := i.(int); ok {
  434. return v
  435. }
  436. return int(Int64(i))
  437. }
  438. // Int8 converts <i> to int8.
  439. func Int8(i interface{}) int8 {
  440. if i == nil {
  441. return 0
  442. }
  443. if v, ok := i.(int8); ok {
  444. return v
  445. }
  446. return int8(Int64(i))
  447. }
  448. // Int16 converts <i> to int16.
  449. func Int16(i interface{}) int16 {
  450. if i == nil {
  451. return 0
  452. }
  453. if v, ok := i.(int16); ok {
  454. return v
  455. }
  456. return int16(Int64(i))
  457. }
  458. // Int32 converts <i> to int32.
  459. func Int32(i interface{}) int32 {
  460. if i == nil {
  461. return 0
  462. }
  463. if v, ok := i.(int32); ok {
  464. return v
  465. }
  466. return int32(Int64(i))
  467. }
  468. // Int64 converts <i> to int64.
  469. func Int64(i interface{}) int64 {
  470. if i == nil {
  471. return 0
  472. }
  473. switch value := i.(type) {
  474. case int:
  475. return int64(value)
  476. case int8:
  477. return int64(value)
  478. case int16:
  479. return int64(value)
  480. case int32:
  481. return int64(value)
  482. case int64:
  483. return value
  484. case uint:
  485. return int64(value)
  486. case uint8:
  487. return int64(value)
  488. case uint16:
  489. return int64(value)
  490. case uint32:
  491. return int64(value)
  492. case uint64:
  493. return int64(value)
  494. case float32:
  495. return int64(value)
  496. case float64:
  497. return int64(value)
  498. case bool:
  499. if value {
  500. return 1
  501. }
  502. return 0
  503. case []byte:
  504. return gbinary.DecodeToInt64(value)
  505. default:
  506. s := String(value)
  507. isMinus := false
  508. if len(s) > 0 {
  509. if s[0] == '-' {
  510. isMinus = true
  511. s = s[1:]
  512. } else if s[0] == '+' {
  513. s = s[1:]
  514. }
  515. }
  516. // Hexadecimal
  517. if len(s) > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X') {
  518. if v, e := strconv.ParseInt(s[2:], 16, 64); e == nil {
  519. if isMinus {
  520. return -v
  521. }
  522. return v
  523. }
  524. }
  525. // Octal
  526. if len(s) > 1 && s[0] == '0' {
  527. if v, e := strconv.ParseInt(s[1:], 8, 64); e == nil {
  528. if isMinus {
  529. return -v
  530. }
  531. return v
  532. }
  533. }
  534. // Decimal
  535. if v, e := strconv.ParseInt(s, 10, 64); e == nil {
  536. if isMinus {
  537. return -v
  538. }
  539. return v
  540. }
  541. // Float64
  542. return int64(Float64(value))
  543. }
  544. }
  545. // Uint converts <i> to uint.
  546. func Uint(i interface{}) uint {
  547. if i == nil {
  548. return 0
  549. }
  550. if v, ok := i.(uint); ok {
  551. return v
  552. }
  553. return uint(Uint64(i))
  554. }
  555. // Uint8 converts <i> to uint8.
  556. func Uint8(i interface{}) uint8 {
  557. if i == nil {
  558. return 0
  559. }
  560. if v, ok := i.(uint8); ok {
  561. return v
  562. }
  563. return uint8(Uint64(i))
  564. }
  565. // Uint16 converts <i> to uint16.
  566. func Uint16(i interface{}) uint16 {
  567. if i == nil {
  568. return 0
  569. }
  570. if v, ok := i.(uint16); ok {
  571. return v
  572. }
  573. return uint16(Uint64(i))
  574. }
  575. // Uint32 converts <i> to uint32.
  576. func Uint32(i interface{}) uint32 {
  577. if i == nil {
  578. return 0
  579. }
  580. if v, ok := i.(uint32); ok {
  581. return v
  582. }
  583. return uint32(Uint64(i))
  584. }
  585. // Uint64 converts <i> to uint64.
  586. func Uint64(i interface{}) uint64 {
  587. if i == nil {
  588. return 0
  589. }
  590. switch value := i.(type) {
  591. case int:
  592. return uint64(value)
  593. case int8:
  594. return uint64(value)
  595. case int16:
  596. return uint64(value)
  597. case int32:
  598. return uint64(value)
  599. case int64:
  600. return uint64(value)
  601. case uint:
  602. return uint64(value)
  603. case uint8:
  604. return uint64(value)
  605. case uint16:
  606. return uint64(value)
  607. case uint32:
  608. return uint64(value)
  609. case uint64:
  610. return value
  611. case float32:
  612. return uint64(value)
  613. case float64:
  614. return uint64(value)
  615. case bool:
  616. if value {
  617. return 1
  618. }
  619. return 0
  620. case []byte:
  621. return gbinary.DecodeToUint64(value)
  622. default:
  623. s := String(value)
  624. // Hexadecimal
  625. if len(s) > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X') {
  626. if v, e := strconv.ParseUint(s[2:], 16, 64); e == nil {
  627. return v
  628. }
  629. }
  630. // Octal
  631. if len(s) > 1 && s[0] == '0' {
  632. if v, e := strconv.ParseUint(s[1:], 8, 64); e == nil {
  633. return v
  634. }
  635. }
  636. // Decimal
  637. if v, e := strconv.ParseUint(s, 10, 64); e == nil {
  638. return v
  639. }
  640. // Float64
  641. return uint64(Float64(value))
  642. }
  643. }
  644. // Float32 converts <i> to float32.
  645. func Float32(i interface{}) float32 {
  646. if i == nil {
  647. return 0
  648. }
  649. switch value := i.(type) {
  650. case float32:
  651. return value
  652. case float64:
  653. return float32(value)
  654. case []byte:
  655. return gbinary.DecodeToFloat32(value)
  656. default:
  657. v, _ := strconv.ParseFloat(String(i), 64)
  658. return float32(v)
  659. }
  660. }
  661. // Float64 converts <i> to float64.
  662. func Float64(i interface{}) float64 {
  663. if i == nil {
  664. return 0
  665. }
  666. switch value := i.(type) {
  667. case float32:
  668. return float64(value)
  669. case float64:
  670. return value
  671. case []byte:
  672. return gbinary.DecodeToFloat64(value)
  673. default:
  674. v, _ := strconv.ParseFloat(String(i), 64)
  675. return v
  676. }
  677. }