utils.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. */
  14. package utils
  15. import (
  16. "crypto/md5"
  17. "crypto/rand"
  18. "encoding/base64"
  19. "encoding/hex"
  20. "hash"
  21. rand2 "math/rand"
  22. "net/url"
  23. "reflect"
  24. "strconv"
  25. "time"
  26. )
  27. type UUID [16]byte
  28. const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  29. func GetUUID() (uuidHex string) {
  30. uuid := NewUUID()
  31. uuidHex = hex.EncodeToString(uuid[:])
  32. return
  33. }
  34. func RandStringBytes(n int) string {
  35. b := make([]byte, n)
  36. for i := range b {
  37. b[i] = letterBytes[rand2.Intn(len(letterBytes))]
  38. }
  39. return string(b)
  40. }
  41. func GetMD5Base64(bytes []byte) (base64Value string) {
  42. md5Ctx := md5.New()
  43. md5Ctx.Write(bytes)
  44. md5Value := md5Ctx.Sum(nil)
  45. base64Value = base64.StdEncoding.EncodeToString(md5Value)
  46. return
  47. }
  48. func GetTimeInFormatISO8601() (timeStr string) {
  49. gmt := time.FixedZone("GMT", 0)
  50. return time.Now().In(gmt).Format("2006-01-02T15:04:05Z")
  51. }
  52. func GetTimeInFormatRFC2616() (timeStr string) {
  53. gmt := time.FixedZone("GMT", 0)
  54. return time.Now().In(gmt).Format("Mon, 02 Jan 2006 15:04:05 GMT")
  55. }
  56. func GetUrlFormedMap(source map[string]string) (urlEncoded string) {
  57. urlEncoder := url.Values{}
  58. for key, value := range source {
  59. urlEncoder.Add(key, value)
  60. }
  61. urlEncoded = urlEncoder.Encode()
  62. return
  63. }
  64. func InitStructWithDefaultTag(bean interface{}) {
  65. configType := reflect.TypeOf(bean)
  66. for i := 0; i < configType.Elem().NumField(); i++ {
  67. field := configType.Elem().Field(i)
  68. defaultValue := field.Tag.Get("default")
  69. if defaultValue == "" {
  70. continue
  71. }
  72. setter := reflect.ValueOf(bean).Elem().Field(i)
  73. switch field.Type.String() {
  74. case "int":
  75. intValue, _ := strconv.ParseInt(defaultValue, 10, 64)
  76. setter.SetInt(intValue)
  77. case "time.Duration":
  78. intValue, _ := strconv.ParseInt(defaultValue, 10, 64)
  79. setter.SetInt(intValue)
  80. case "string":
  81. setter.SetString(defaultValue)
  82. case "bool":
  83. boolValue, _ := strconv.ParseBool(defaultValue)
  84. setter.SetBool(boolValue)
  85. }
  86. }
  87. }
  88. func NewUUID() UUID {
  89. ns := UUID{}
  90. safeRandom(ns[:])
  91. u := newFromHash(md5.New(), ns, RandStringBytes(16))
  92. u[6] = (u[6] & 0x0f) | (byte(2) << 4)
  93. u[8] = (u[8]&(0xff>>2) | (0x02 << 6))
  94. return u
  95. }
  96. func newFromHash(h hash.Hash, ns UUID, name string) UUID {
  97. u := UUID{}
  98. h.Write(ns[:])
  99. h.Write([]byte(name))
  100. copy(u[:], h.Sum(nil))
  101. return u
  102. }
  103. func safeRandom(dest []byte) {
  104. if _, err := rand.Read(dest); err != nil {
  105. panic(err)
  106. }
  107. }
  108. func (u UUID) String() string {
  109. buf := make([]byte, 36)
  110. hex.Encode(buf[0:8], u[0:4])
  111. buf[8] = '-'
  112. hex.Encode(buf[9:13], u[4:6])
  113. buf[13] = '-'
  114. hex.Encode(buf[14:18], u[6:8])
  115. buf[18] = '-'
  116. hex.Encode(buf[19:23], u[8:10])
  117. buf[23] = '-'
  118. hex.Encode(buf[24:], u[10:])
  119. return string(buf)
  120. }