utils.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package orm
  15. import (
  16. "fmt"
  17. "math/big"
  18. "reflect"
  19. "strconv"
  20. "strings"
  21. "time"
  22. )
  23. // StrTo is the target string
  24. type StrTo string
  25. // Set string
  26. func (f *StrTo) Set(v string) {
  27. if v != "" {
  28. *f = StrTo(v)
  29. } else {
  30. f.Clear()
  31. }
  32. }
  33. // Clear string
  34. func (f *StrTo) Clear() {
  35. *f = StrTo(0x1E)
  36. }
  37. // Exist check string exist
  38. func (f StrTo) Exist() bool {
  39. return string(f) != string(0x1E)
  40. }
  41. // Bool string to bool
  42. func (f StrTo) Bool() (bool, error) {
  43. return strconv.ParseBool(f.String())
  44. }
  45. // Float32 string to float32
  46. func (f StrTo) Float32() (float32, error) {
  47. v, err := strconv.ParseFloat(f.String(), 32)
  48. return float32(v), err
  49. }
  50. // Float64 string to float64
  51. func (f StrTo) Float64() (float64, error) {
  52. return strconv.ParseFloat(f.String(), 64)
  53. }
  54. // Int string to int
  55. func (f StrTo) Int() (int, error) {
  56. v, err := strconv.ParseInt(f.String(), 10, 32)
  57. return int(v), err
  58. }
  59. // Int8 string to int8
  60. func (f StrTo) Int8() (int8, error) {
  61. v, err := strconv.ParseInt(f.String(), 10, 8)
  62. return int8(v), err
  63. }
  64. // Int16 string to int16
  65. func (f StrTo) Int16() (int16, error) {
  66. v, err := strconv.ParseInt(f.String(), 10, 16)
  67. return int16(v), err
  68. }
  69. // Int32 string to int32
  70. func (f StrTo) Int32() (int32, error) {
  71. v, err := strconv.ParseInt(f.String(), 10, 32)
  72. return int32(v), err
  73. }
  74. // Int64 string to int64
  75. func (f StrTo) Int64() (int64, error) {
  76. v, err := strconv.ParseInt(f.String(), 10, 64)
  77. if err != nil {
  78. i := new(big.Int)
  79. ni, ok := i.SetString(f.String(), 10) // octal
  80. if !ok {
  81. return int64(v), err
  82. }
  83. return ni.Int64(), nil
  84. }
  85. return int64(v), err
  86. }
  87. // Uint string to uint
  88. func (f StrTo) Uint() (uint, error) {
  89. v, err := strconv.ParseUint(f.String(), 10, 32)
  90. return uint(v), err
  91. }
  92. // Uint8 string to uint8
  93. func (f StrTo) Uint8() (uint8, error) {
  94. v, err := strconv.ParseUint(f.String(), 10, 8)
  95. return uint8(v), err
  96. }
  97. // Uint16 string to uint16
  98. func (f StrTo) Uint16() (uint16, error) {
  99. v, err := strconv.ParseUint(f.String(), 10, 16)
  100. return uint16(v), err
  101. }
  102. // Uint32 string to uint31
  103. func (f StrTo) Uint32() (uint32, error) {
  104. v, err := strconv.ParseUint(f.String(), 10, 32)
  105. return uint32(v), err
  106. }
  107. // Uint64 string to uint64
  108. func (f StrTo) Uint64() (uint64, error) {
  109. v, err := strconv.ParseUint(f.String(), 10, 64)
  110. if err != nil {
  111. i := new(big.Int)
  112. ni, ok := i.SetString(f.String(), 10)
  113. if !ok {
  114. return uint64(v), err
  115. }
  116. return ni.Uint64(), nil
  117. }
  118. return uint64(v), err
  119. }
  120. // String string to string
  121. func (f StrTo) String() string {
  122. if f.Exist() {
  123. return string(f)
  124. }
  125. return ""
  126. }
  127. // ToStr interface to string
  128. func ToStr(value interface{}, args ...int) (s string) {
  129. switch v := value.(type) {
  130. case bool:
  131. s = strconv.FormatBool(v)
  132. case float32:
  133. s = strconv.FormatFloat(float64(v), 'f', argInt(args).Get(0, -1), argInt(args).Get(1, 32))
  134. case float64:
  135. s = strconv.FormatFloat(v, 'f', argInt(args).Get(0, -1), argInt(args).Get(1, 64))
  136. case int:
  137. s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
  138. case int8:
  139. s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
  140. case int16:
  141. s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
  142. case int32:
  143. s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
  144. case int64:
  145. s = strconv.FormatInt(v, argInt(args).Get(0, 10))
  146. case uint:
  147. s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
  148. case uint8:
  149. s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
  150. case uint16:
  151. s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
  152. case uint32:
  153. s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
  154. case uint64:
  155. s = strconv.FormatUint(v, argInt(args).Get(0, 10))
  156. case string:
  157. s = v
  158. case []byte:
  159. s = string(v)
  160. default:
  161. s = fmt.Sprintf("%v", v)
  162. }
  163. return s
  164. }
  165. // ToInt64 interface to int64
  166. func ToInt64(value interface{}) (d int64) {
  167. val := reflect.ValueOf(value)
  168. switch value.(type) {
  169. case int, int8, int16, int32, int64:
  170. d = val.Int()
  171. case uint, uint8, uint16, uint32, uint64:
  172. d = int64(val.Uint())
  173. default:
  174. panic(fmt.Errorf("ToInt64 need numeric not `%T`", value))
  175. }
  176. return
  177. }
  178. // snake string, XxYy to xx_yy , XxYY to xx_yy
  179. func snakeString(s string) string {
  180. data := make([]byte, 0, len(s)*2)
  181. j := false
  182. num := len(s)
  183. for i := 0; i < num; i++ {
  184. d := s[i]
  185. if i > 0 && d >= 'A' && d <= 'Z' && j {
  186. data = append(data, '_')
  187. }
  188. if d != '_' {
  189. j = true
  190. }
  191. data = append(data, d)
  192. }
  193. return strings.ToLower(string(data[:]))
  194. }
  195. // camel string, xx_yy to XxYy
  196. func camelString(s string) string {
  197. data := make([]byte, 0, len(s))
  198. flag, num := true, len(s)-1
  199. for i := 0; i <= num; i++ {
  200. d := s[i]
  201. if d == '_' {
  202. flag = true
  203. continue
  204. } else if flag == true {
  205. if d >= 'a' && d <= 'z' {
  206. d = d - 32
  207. }
  208. flag = false
  209. }
  210. data = append(data, d)
  211. }
  212. return string(data[:])
  213. }
  214. type argString []string
  215. // get string by index from string slice
  216. func (a argString) Get(i int, args ...string) (r string) {
  217. if i >= 0 && i < len(a) {
  218. r = a[i]
  219. } else if len(args) > 0 {
  220. r = args[0]
  221. }
  222. return
  223. }
  224. type argInt []int
  225. // get int by index from int slice
  226. func (a argInt) Get(i int, args ...int) (r int) {
  227. if i >= 0 && i < len(a) {
  228. r = a[i]
  229. }
  230. if len(args) > 0 {
  231. r = args[0]
  232. }
  233. return
  234. }
  235. // parse time to string with location
  236. func timeParse(dateString, format string) (time.Time, error) {
  237. tp, err := time.ParseInLocation(format, dateString, DefaultTimeLoc)
  238. return tp, err
  239. }
  240. // get pointer indirect type
  241. func indirectType(v reflect.Type) reflect.Type {
  242. switch v.Kind() {
  243. case reflect.Ptr:
  244. return indirectType(v.Elem())
  245. default:
  246. return v
  247. }
  248. }