utils.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. "encoding/base64"
  18. "encoding/hex"
  19. "encoding/json"
  20. "fmt"
  21. "github.com/satori/go.uuid"
  22. "net/url"
  23. "reflect"
  24. "strconv"
  25. "time"
  26. )
  27. // if you use go 1.10 or higher, you can hack this util by these to avoid "TimeZone.zip not found" on Windows
  28. var LoadLocationFromTZData func(name string, data []byte) (*time.Location, error) = nil
  29. var TZData []byte = nil
  30. func GetUUIDV4() (uuidHex string) {
  31. uuidV4 := uuid.NewV4()
  32. uuidHex = hex.EncodeToString(uuidV4.Bytes())
  33. return
  34. }
  35. func GetMD5Base64(bytes []byte) (base64Value string) {
  36. md5Ctx := md5.New()
  37. md5Ctx.Write(bytes)
  38. md5Value := md5Ctx.Sum(nil)
  39. base64Value = base64.StdEncoding.EncodeToString(md5Value)
  40. return
  41. }
  42. func GetTimeInFormatISO8601() (timeStr string) {
  43. var gmt *time.Location
  44. var err error
  45. if LoadLocationFromTZData != nil && TZData != nil {
  46. gmt, err = LoadLocationFromTZData("GMT", TZData)
  47. } else {
  48. gmt, err = time.LoadLocation("GMT")
  49. }
  50. if err != nil {
  51. panic(err)
  52. }
  53. return time.Now().In(gmt).Format("2006-01-02T15:04:05Z")
  54. }
  55. func GetTimeInFormatRFC2616() (timeStr string) {
  56. var gmt *time.Location
  57. var err error
  58. if LoadLocationFromTZData != nil && TZData != nil {
  59. gmt, err = LoadLocationFromTZData("GMT", TZData)
  60. } else {
  61. gmt, err = time.LoadLocation("GMT")
  62. }
  63. if err != nil {
  64. panic(err)
  65. }
  66. return time.Now().In(gmt).Format("Mon, 02 Jan 2006 15:04:05 GMT")
  67. }
  68. func GetUrlFormedMap(source map[string]string) (urlEncoded string) {
  69. urlEncoder := url.Values{}
  70. for key, value := range source {
  71. urlEncoder.Add(key, value)
  72. }
  73. urlEncoded = urlEncoder.Encode()
  74. return
  75. }
  76. func GetFromJsonString(jsonString, key string) (result string, err error) {
  77. var responseMap map[string]*json.RawMessage
  78. err = json.Unmarshal([]byte(jsonString), &responseMap)
  79. if err != nil {
  80. return
  81. }
  82. fmt.Println(string(*responseMap[key]))
  83. err = json.Unmarshal(*responseMap[key], &result)
  84. return
  85. }
  86. func InitStructWithDefaultTag(bean interface{}) {
  87. configType := reflect.TypeOf(bean)
  88. for i := 0; i < configType.Elem().NumField(); i++ {
  89. field := configType.Elem().Field(i)
  90. defaultValue := field.Tag.Get("default")
  91. if defaultValue == "" {
  92. continue
  93. }
  94. setter := reflect.ValueOf(bean).Elem().Field(i)
  95. switch field.Type.String() {
  96. case "int":
  97. intValue, _ := strconv.ParseInt(defaultValue, 10, 64)
  98. setter.SetInt(intValue)
  99. case "time.Duration":
  100. intValue, _ := strconv.ParseInt(defaultValue, 10, 64)
  101. setter.SetInt(intValue)
  102. case "string":
  103. setter.SetString(defaultValue)
  104. case "bool":
  105. boolValue, _ := strconv.ParseBool(defaultValue)
  106. setter.SetBool(boolValue)
  107. }
  108. }
  109. }