utils.go 2.6 KB

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