蛋蛋星球-客户端
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.
 
 
 
 
 

361 lines
6.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. case []interface{}:
  135. return interfaceSliceToJSONStyleString(i)
  136. }
  137. return fmt.Sprintf("%#v", raw)
  138. }
  139. func interfaceSliceToJSONStyleString(slice []interface{}) string {
  140. var elements []string
  141. for _, v := range slice {
  142. switch value := v.(type) {
  143. case string:
  144. // 对字符串进行转义并加上双引号
  145. elements = append(elements, fmt.Sprintf("%q", value))
  146. case bool:
  147. // 直接转换为字符串
  148. elements = append(elements, fmt.Sprintf("%v", value))
  149. case float64, float32:
  150. // 格式化浮点数
  151. elements = append(elements, fmt.Sprintf("%v", value))
  152. case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
  153. // 格式化整数
  154. elements = append(elements, fmt.Sprintf("%v", value))
  155. default:
  156. // 其他类型也尽量尝试转换为字符串
  157. elements = append(elements, fmt.Sprintf("%v", value))
  158. }
  159. }
  160. // 使用方括号包裹,并用逗号分隔各个元素
  161. return "[" + strings.Join(elements, ", ") + "]"
  162. }
  163. func AnyToFloat64(raw interface{}) float64 {
  164. switch i := raw.(type) {
  165. case []byte:
  166. f, _ := strconv.ParseFloat(string(i), 64)
  167. return f
  168. case int:
  169. return float64(i)
  170. case int64:
  171. return float64(i)
  172. case float32:
  173. return float64(i)
  174. case float64:
  175. return i
  176. case uint:
  177. return float64(i)
  178. case uint8:
  179. return float64(i)
  180. case uint16:
  181. return float64(i)
  182. case uint32:
  183. return float64(i)
  184. case uint64:
  185. return float64(i)
  186. case int8:
  187. return float64(i)
  188. case int16:
  189. return float64(i)
  190. case int32:
  191. return float64(i)
  192. case string:
  193. f, _ := strconv.ParseFloat(i, 64)
  194. return f
  195. case bool:
  196. if i {
  197. return 1
  198. }
  199. }
  200. return 0
  201. }
  202. func ToByte(raw interface{}, e error) []byte {
  203. if e != nil {
  204. return []byte{}
  205. }
  206. switch i := raw.(type) {
  207. case string:
  208. return []byte(i)
  209. case int:
  210. return Int64ToBytes(int64(i))
  211. case int64:
  212. return Int64ToBytes(i)
  213. case float32:
  214. return Float32ToByte(i)
  215. case float64:
  216. return Float64ToByte(i)
  217. case uint:
  218. return Int64ToBytes(int64(i))
  219. case uint8:
  220. return Int64ToBytes(int64(i))
  221. case uint16:
  222. return Int64ToBytes(int64(i))
  223. case uint32:
  224. return Int64ToBytes(int64(i))
  225. case uint64:
  226. return Int64ToBytes(int64(i))
  227. case int8:
  228. return Int64ToBytes(int64(i))
  229. case int16:
  230. return Int64ToBytes(int64(i))
  231. case int32:
  232. return Int64ToBytes(int64(i))
  233. case []byte:
  234. return i
  235. case error:
  236. return []byte(i.Error())
  237. case bool:
  238. if i {
  239. return []byte("true")
  240. }
  241. return []byte("false")
  242. }
  243. return []byte(fmt.Sprintf("%#v", raw))
  244. }
  245. func Int64ToBytes(i int64) []byte {
  246. var buf = make([]byte, 8)
  247. binary.BigEndian.PutUint64(buf, uint64(i))
  248. return buf
  249. }
  250. func BytesToInt64(buf []byte) int64 {
  251. return int64(binary.BigEndian.Uint64(buf))
  252. }
  253. func StrToInt(s string) int {
  254. res, _ := strconv.Atoi(s)
  255. return res
  256. }
  257. func StrToInt64(s string) int64 {
  258. res, _ := strconv.ParseInt(s, 10, 64)
  259. return res
  260. }
  261. func Float32ToByte(float float32) []byte {
  262. bits := math.Float32bits(float)
  263. bytes := make([]byte, 4)
  264. binary.LittleEndian.PutUint32(bytes, bits)
  265. return bytes
  266. }
  267. func ByteToFloat32(bytes []byte) float32 {
  268. bits := binary.LittleEndian.Uint32(bytes)
  269. return math.Float32frombits(bits)
  270. }
  271. func Float64ToByte(float float64) []byte {
  272. bits := math.Float64bits(float)
  273. bytes := make([]byte, 8)
  274. binary.LittleEndian.PutUint64(bytes, bits)
  275. return bytes
  276. }
  277. func ByteToFloat64(bytes []byte) float64 {
  278. bits := binary.LittleEndian.Uint64(bytes)
  279. return math.Float64frombits(bits)
  280. }
  281. func Float64ToStr(f float64) string {
  282. if f == float64(int64(f)) {
  283. return strconv.FormatInt(int64(f), 10)
  284. }
  285. return fmt.Sprintf("%.2f", f)
  286. }
  287. func Float64ToStrPrec1(f float64) string {
  288. return strconv.FormatFloat(f, 'f', 1, 64)
  289. }
  290. func Float64ToStrPrec10(f float64) string {
  291. return strconv.FormatFloat(f, 'f', 10, 64)
  292. }
  293. func Float64ToStrPrec8(f float64) string {
  294. return strconv.FormatFloat(f, 'f', 8, 64)
  295. }
  296. func Float32ToStr(f float32) string {
  297. return Float64ToStr(float64(f))
  298. }
  299. func StrToFloat64(s string) float64 {
  300. res, err := strconv.ParseFloat(s, 64)
  301. if err != nil {
  302. return 0
  303. }
  304. return res
  305. }
  306. func StrToFloat32(s string) float32 {
  307. res, err := strconv.ParseFloat(s, 32)
  308. if err != nil {
  309. return 0
  310. }
  311. return float32(res)
  312. }
  313. func StrToBool(s string) bool {
  314. b, _ := strconv.ParseBool(s)
  315. return b
  316. }
  317. func BoolToStr(b bool) string {
  318. if b {
  319. return "true"
  320. }
  321. return "false"
  322. }
  323. func FloatToInt64(f float64) int64 {
  324. return int64(f)
  325. }
  326. func IntToStr(i int) string {
  327. return strconv.Itoa(i)
  328. }
  329. func Int64ToStr(i int64) string {
  330. return strconv.FormatInt(i, 10)
  331. }