蛋蛋星球 后台端
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.
 
 
 
 
 

325 lines
5.8 KiB

  1. package utils
  2. import (
  3. "encoding/binary"
  4. "encoding/json"
  5. "fmt"
  6. "math"
  7. "strconv"
  8. "strings"
  9. )
  10. func ToString(raw interface{}, e error) (res string) {
  11. if e != nil {
  12. return ""
  13. }
  14. return AnyToString(raw)
  15. }
  16. func ToInt64(raw interface{}, e error) int64 {
  17. if e != nil {
  18. return 0
  19. }
  20. return AnyToInt64(raw)
  21. }
  22. func AnyToBool(raw interface{}) bool {
  23. switch i := raw.(type) {
  24. case float32, float64, int, int64, uint, uint8, uint16, uint32, uint64, int8, int16, int32:
  25. return i != 0
  26. case []byte:
  27. return i != nil
  28. case string:
  29. if i == "false" {
  30. return false
  31. }
  32. return i != ""
  33. case error:
  34. return false
  35. case nil:
  36. return true
  37. }
  38. val := fmt.Sprint(raw)
  39. val = strings.TrimLeft(val, "&")
  40. if strings.TrimLeft(val, "{}") == "" {
  41. return false
  42. }
  43. if strings.TrimLeft(val, "[]") == "" {
  44. return false
  45. }
  46. // ptr type
  47. b, err := json.Marshal(raw)
  48. if err != nil {
  49. return false
  50. }
  51. if strings.TrimLeft(string(b), "\"\"") == "" {
  52. return false
  53. }
  54. if strings.TrimLeft(string(b), "{}") == "" {
  55. return false
  56. }
  57. return true
  58. }
  59. func AnyToInt64(raw interface{}) int64 {
  60. switch i := raw.(type) {
  61. case string:
  62. res, _ := strconv.ParseInt(i, 10, 64)
  63. return res
  64. case []byte:
  65. return BytesToInt64(i)
  66. case int:
  67. return int64(i)
  68. case int64:
  69. return i
  70. case uint:
  71. return int64(i)
  72. case uint8:
  73. return int64(i)
  74. case uint16:
  75. return int64(i)
  76. case uint32:
  77. return int64(i)
  78. case uint64:
  79. return int64(i)
  80. case int8:
  81. return int64(i)
  82. case int16:
  83. return int64(i)
  84. case int32:
  85. return int64(i)
  86. case float32:
  87. return int64(i)
  88. case float64:
  89. return int64(i)
  90. case error:
  91. return 0
  92. case bool:
  93. if i {
  94. return 1
  95. }
  96. return 0
  97. }
  98. return 0
  99. }
  100. func AnyToString(raw interface{}) string {
  101. switch i := raw.(type) {
  102. case []byte:
  103. return string(i)
  104. case int:
  105. return strconv.FormatInt(int64(i), 10)
  106. case int64:
  107. return strconv.FormatInt(i, 10)
  108. case float32:
  109. return Float64ToStr(float64(i))
  110. case float64:
  111. return Float64ToStr(i)
  112. case uint:
  113. return strconv.FormatInt(int64(i), 10)
  114. case uint8:
  115. return strconv.FormatInt(int64(i), 10)
  116. case uint16:
  117. return strconv.FormatInt(int64(i), 10)
  118. case uint32:
  119. return strconv.FormatInt(int64(i), 10)
  120. case uint64:
  121. return strconv.FormatInt(int64(i), 10)
  122. case int8:
  123. return strconv.FormatInt(int64(i), 10)
  124. case int16:
  125. return strconv.FormatInt(int64(i), 10)
  126. case int32:
  127. return strconv.FormatInt(int64(i), 10)
  128. case string:
  129. return i
  130. case error:
  131. return i.Error()
  132. case bool:
  133. return strconv.FormatBool(i)
  134. }
  135. return fmt.Sprintf("%#v", raw)
  136. }
  137. func AnyToFloat64(raw interface{}) float64 {
  138. switch i := raw.(type) {
  139. case []byte:
  140. f, _ := strconv.ParseFloat(string(i), 64)
  141. return f
  142. case int:
  143. return float64(i)
  144. case int64:
  145. return float64(i)
  146. case float32:
  147. return float64(i)
  148. case float64:
  149. return i
  150. case uint:
  151. return float64(i)
  152. case uint8:
  153. return float64(i)
  154. case uint16:
  155. return float64(i)
  156. case uint32:
  157. return float64(i)
  158. case uint64:
  159. return float64(i)
  160. case int8:
  161. return float64(i)
  162. case int16:
  163. return float64(i)
  164. case int32:
  165. return float64(i)
  166. case string:
  167. f, _ := strconv.ParseFloat(i, 64)
  168. return f
  169. case bool:
  170. if i {
  171. return 1
  172. }
  173. }
  174. return 0
  175. }
  176. func ToByte(raw interface{}, e error) []byte {
  177. if e != nil {
  178. return []byte{}
  179. }
  180. switch i := raw.(type) {
  181. case string:
  182. return []byte(i)
  183. case int:
  184. return Int64ToBytes(int64(i))
  185. case int64:
  186. return Int64ToBytes(i)
  187. case float32:
  188. return Float32ToByte(i)
  189. case float64:
  190. return Float64ToByte(i)
  191. case uint:
  192. return Int64ToBytes(int64(i))
  193. case uint8:
  194. return Int64ToBytes(int64(i))
  195. case uint16:
  196. return Int64ToBytes(int64(i))
  197. case uint32:
  198. return Int64ToBytes(int64(i))
  199. case uint64:
  200. return Int64ToBytes(int64(i))
  201. case int8:
  202. return Int64ToBytes(int64(i))
  203. case int16:
  204. return Int64ToBytes(int64(i))
  205. case int32:
  206. return Int64ToBytes(int64(i))
  207. case []byte:
  208. return i
  209. case error:
  210. return []byte(i.Error())
  211. case bool:
  212. if i {
  213. return []byte("true")
  214. }
  215. return []byte("false")
  216. }
  217. return []byte(fmt.Sprintf("%#v", raw))
  218. }
  219. func Int64ToBytes(i int64) []byte {
  220. var buf = make([]byte, 8)
  221. binary.BigEndian.PutUint64(buf, uint64(i))
  222. return buf
  223. }
  224. func BytesToInt64(buf []byte) int64 {
  225. return int64(binary.BigEndian.Uint64(buf))
  226. }
  227. func StrToInt(s string) int {
  228. res, _ := strconv.Atoi(s)
  229. return res
  230. }
  231. func StrToInt64(s string) int64 {
  232. res, _ := strconv.ParseInt(s, 10, 64)
  233. return res
  234. }
  235. func Float32ToByte(float float32) []byte {
  236. bits := math.Float32bits(float)
  237. bytes := make([]byte, 4)
  238. binary.LittleEndian.PutUint32(bytes, bits)
  239. return bytes
  240. }
  241. func ByteToFloat32(bytes []byte) float32 {
  242. bits := binary.LittleEndian.Uint32(bytes)
  243. return math.Float32frombits(bits)
  244. }
  245. func Float64ToByte(float float64) []byte {
  246. bits := math.Float64bits(float)
  247. bytes := make([]byte, 8)
  248. binary.LittleEndian.PutUint64(bytes, bits)
  249. return bytes
  250. }
  251. func ByteToFloat64(bytes []byte) float64 {
  252. bits := binary.LittleEndian.Uint64(bytes)
  253. return math.Float64frombits(bits)
  254. }
  255. func Float64ToStr(f float64) string {
  256. return strconv.FormatFloat(f, 'f', 2, 64)
  257. }
  258. func Float64ToStrPrec1(f float64) string {
  259. return strconv.FormatFloat(f, 'f', 1, 64)
  260. }
  261. func Float64ToStrPrec10(f float64) string {
  262. return strconv.FormatFloat(f, 'f', 10, 64)
  263. }
  264. func Float32ToStr(f float32) string {
  265. return Float64ToStr(float64(f))
  266. }
  267. func StrToFloat64(s string) float64 {
  268. res, err := strconv.ParseFloat(s, 64)
  269. if err != nil {
  270. return 0
  271. }
  272. return res
  273. }
  274. func StrToFloat32(s string) float32 {
  275. res, err := strconv.ParseFloat(s, 32)
  276. if err != nil {
  277. return 0
  278. }
  279. return float32(res)
  280. }
  281. func StrToBool(s string) bool {
  282. b, _ := strconv.ParseBool(s)
  283. return b
  284. }
  285. func BoolToStr(b bool) string {
  286. if b {
  287. return "true"
  288. }
  289. return "false"
  290. }
  291. func FloatToInt64(f float64) int64 {
  292. return int64(f)
  293. }
  294. func IntToStr(i int) string {
  295. return strconv.Itoa(i)
  296. }
  297. func Int64ToStr(i int64) string {
  298. return strconv.FormatInt(i, 10)
  299. }