Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

convert.go 5.9 KiB

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