智慧食堂
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 Float32ToStr(f float32) string {
  262. return Float64ToStr(float64(f))
  263. }
  264. func StrToFloat64(s string) float64 {
  265. res, err := strconv.ParseFloat(s, 64)
  266. if err != nil {
  267. return 0
  268. }
  269. return res
  270. }
  271. func StrToFloat32(s string) float32 {
  272. res, err := strconv.ParseFloat(s, 32)
  273. if err != nil {
  274. return 0
  275. }
  276. return float32(res)
  277. }
  278. func StrToBool(s string) bool {
  279. b, _ := strconv.ParseBool(s)
  280. return b
  281. }
  282. func BoolToStr(b bool) string {
  283. if b {
  284. return "true"
  285. }
  286. return "false"
  287. }
  288. func FloatToInt64(f float64) int64 {
  289. return int64(f)
  290. }
  291. func IntToStr(i int) string {
  292. return strconv.Itoa(i)
  293. }
  294. func Int64ToStr(i int64) string {
  295. return strconv.FormatInt(i, 10)
  296. }