utils.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 GetGMTLocation() (*time.Location, error) {
  43. if LoadLocationFromTZData != nil && TZData != nil {
  44. return LoadLocationFromTZData("GMT", TZData)
  45. } else {
  46. return time.LoadLocation("GMT")
  47. }
  48. }
  49. func GetTimeInFormatISO8601() (timeStr string) {
  50. gmt, err := GetGMTLocation()
  51. if err != nil {
  52. panic(err)
  53. }
  54. return time.Now().In(gmt).Format("2006-01-02T15:04:05Z")
  55. }
  56. func GetTimeInFormatRFC2616() (timeStr string) {
  57. gmt, err := GetGMTLocation()
  58. if err != nil {
  59. panic(err)
  60. }
  61. return time.Now().In(gmt).Format("Mon, 02 Jan 2006 15:04:05 GMT")
  62. }
  63. func GetUrlFormedMap(source map[string]string) (urlEncoded string) {
  64. urlEncoder := url.Values{}
  65. for key, value := range source {
  66. urlEncoder.Add(key, value)
  67. }
  68. urlEncoded = urlEncoder.Encode()
  69. return
  70. }
  71. func GetFromJsonString(jsonString, key string) (result string, err error) {
  72. var responseMap map[string]*json.RawMessage
  73. err = json.Unmarshal([]byte(jsonString), &responseMap)
  74. if err != nil {
  75. return
  76. }
  77. fmt.Println(string(*responseMap[key]))
  78. err = json.Unmarshal(*responseMap[key], &result)
  79. return
  80. }
  81. func InitStructWithDefaultTag(bean interface{}) {
  82. configType := reflect.TypeOf(bean)
  83. for i := 0; i < configType.Elem().NumField(); i++ {
  84. field := configType.Elem().Field(i)
  85. defaultValue := field.Tag.Get("default")
  86. if defaultValue == "" {
  87. continue
  88. }
  89. setter := reflect.ValueOf(bean).Elem().Field(i)
  90. switch field.Type.String() {
  91. case "int":
  92. intValue, _ := strconv.ParseInt(defaultValue, 10, 64)
  93. setter.SetInt(intValue)
  94. case "time.Duration":
  95. intValue, _ := strconv.ParseInt(defaultValue, 10, 64)
  96. setter.SetInt(intValue)
  97. case "string":
  98. setter.SetString(defaultValue)
  99. case "bool":
  100. boolValue, _ := strconv.ParseBool(defaultValue)
  101. setter.SetBool(boolValue)
  102. }
  103. }
  104. }