cast.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright © 2014 Steve Francia <spf@spf13.com>.
  2. //
  3. // Use of this source code is governed by an MIT-style
  4. // license that can be found in the LICENSE file.
  5. // Package cast provides easy and safe casting in Go.
  6. package cast
  7. import "time"
  8. // ToBool casts an interface to a bool type.
  9. func ToBool(i interface{}) bool {
  10. v, _ := ToBoolE(i)
  11. return v
  12. }
  13. // ToTime casts an interface to a time.Time type.
  14. func ToTime(i interface{}) time.Time {
  15. v, _ := ToTimeE(i)
  16. return v
  17. }
  18. // ToDuration casts an interface to a time.Duration type.
  19. func ToDuration(i interface{}) time.Duration {
  20. v, _ := ToDurationE(i)
  21. return v
  22. }
  23. // ToFloat64 casts an interface to a float64 type.
  24. func ToFloat64(i interface{}) float64 {
  25. v, _ := ToFloat64E(i)
  26. return v
  27. }
  28. // ToFloat32 casts an interface to a float32 type.
  29. func ToFloat32(i interface{}) float32 {
  30. v, _ := ToFloat32E(i)
  31. return v
  32. }
  33. // ToInt64 casts an interface to an int64 type.
  34. func ToInt64(i interface{}) int64 {
  35. v, _ := ToInt64E(i)
  36. return v
  37. }
  38. // ToInt32 casts an interface to an int32 type.
  39. func ToInt32(i interface{}) int32 {
  40. v, _ := ToInt32E(i)
  41. return v
  42. }
  43. // ToInt16 casts an interface to an int16 type.
  44. func ToInt16(i interface{}) int16 {
  45. v, _ := ToInt16E(i)
  46. return v
  47. }
  48. // ToInt8 casts an interface to an int8 type.
  49. func ToInt8(i interface{}) int8 {
  50. v, _ := ToInt8E(i)
  51. return v
  52. }
  53. // ToInt casts an interface to an int type.
  54. func ToInt(i interface{}) int {
  55. v, _ := ToIntE(i)
  56. return v
  57. }
  58. // ToUint casts an interface to a uint type.
  59. func ToUint(i interface{}) uint {
  60. v, _ := ToUintE(i)
  61. return v
  62. }
  63. // ToUint64 casts an interface to a uint64 type.
  64. func ToUint64(i interface{}) uint64 {
  65. v, _ := ToUint64E(i)
  66. return v
  67. }
  68. // ToUint32 casts an interface to a uint32 type.
  69. func ToUint32(i interface{}) uint32 {
  70. v, _ := ToUint32E(i)
  71. return v
  72. }
  73. // ToUint16 casts an interface to a uint16 type.
  74. func ToUint16(i interface{}) uint16 {
  75. v, _ := ToUint16E(i)
  76. return v
  77. }
  78. // ToUint8 casts an interface to a uint8 type.
  79. func ToUint8(i interface{}) uint8 {
  80. v, _ := ToUint8E(i)
  81. return v
  82. }
  83. // ToString casts an interface to a string type.
  84. func ToString(i interface{}) string {
  85. v, _ := ToStringE(i)
  86. return v
  87. }
  88. // ToStringMapString casts an interface to a map[string]string type.
  89. func ToStringMapString(i interface{}) map[string]string {
  90. v, _ := ToStringMapStringE(i)
  91. return v
  92. }
  93. // ToStringMapStringSlice casts an interface to a map[string][]string type.
  94. func ToStringMapStringSlice(i interface{}) map[string][]string {
  95. v, _ := ToStringMapStringSliceE(i)
  96. return v
  97. }
  98. // ToStringMapBool casts an interface to a map[string]bool type.
  99. func ToStringMapBool(i interface{}) map[string]bool {
  100. v, _ := ToStringMapBoolE(i)
  101. return v
  102. }
  103. // ToStringMapInt casts an interface to a map[string]int type.
  104. func ToStringMapInt(i interface{}) map[string]int {
  105. v, _ := ToStringMapIntE(i)
  106. return v
  107. }
  108. // ToStringMapInt64 casts an interface to a map[string]int64 type.
  109. func ToStringMapInt64(i interface{}) map[string]int64 {
  110. v, _ := ToStringMapInt64E(i)
  111. return v
  112. }
  113. // ToStringMap casts an interface to a map[string]interface{} type.
  114. func ToStringMap(i interface{}) map[string]interface{} {
  115. v, _ := ToStringMapE(i)
  116. return v
  117. }
  118. // ToSlice casts an interface to a []interface{} type.
  119. func ToSlice(i interface{}) []interface{} {
  120. v, _ := ToSliceE(i)
  121. return v
  122. }
  123. // ToBoolSlice casts an interface to a []bool type.
  124. func ToBoolSlice(i interface{}) []bool {
  125. v, _ := ToBoolSliceE(i)
  126. return v
  127. }
  128. // ToStringSlice casts an interface to a []string type.
  129. func ToStringSlice(i interface{}) []string {
  130. v, _ := ToStringSliceE(i)
  131. return v
  132. }
  133. // ToIntSlice casts an interface to a []int type.
  134. func ToIntSlice(i interface{}) []int {
  135. v, _ := ToIntSliceE(i)
  136. return v
  137. }
  138. // ToDurationSlice casts an interface to a []time.Duration type.
  139. func ToDurationSlice(i interface{}) []time.Duration {
  140. v, _ := ToDurationSliceE(i)
  141. return v
  142. }