utils.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. func GetUUIDV4() (uuidHex string) {
  28. uuidV4 := uuid.NewV4()
  29. uuidHex = hex.EncodeToString(uuidV4.Bytes())
  30. return
  31. }
  32. func GetMD5Base64(bytes []byte) (base64Value string) {
  33. md5Ctx := md5.New()
  34. md5Ctx.Write(bytes)
  35. md5Value := md5Ctx.Sum(nil)
  36. base64Value = base64.StdEncoding.EncodeToString(md5Value)
  37. return
  38. }
  39. func GetTimeInFormatISO8601() (timeStr string) {
  40. gmt, err := time.LoadLocation("GMT")
  41. if err != nil {
  42. panic(err)
  43. }
  44. return time.Now().In(gmt).Format("2006-01-02T15:04:05Z")
  45. }
  46. func GetTimeInFormatRFC2616() (timeStr string) {
  47. gmt, err := time.LoadLocation("GMT")
  48. if err != nil {
  49. panic(err)
  50. }
  51. return time.Now().In(gmt).Format("Mon, 02 Jan 2006 15:04:05 GMT")
  52. }
  53. func GetUrlFormedMap(source map[string]string) (urlEncoded string) {
  54. urlEncoder := url.Values{}
  55. for key, value := range source {
  56. urlEncoder.Add(key, value)
  57. }
  58. urlEncoded = urlEncoder.Encode()
  59. return
  60. }
  61. func GetFromJsonString(jsonString, key string) (result string, err error) {
  62. var responseMap map[string]*json.RawMessage
  63. err = json.Unmarshal([]byte(jsonString), &responseMap)
  64. if err != nil {
  65. return
  66. }
  67. fmt.Println(string(*responseMap[key]))
  68. err = json.Unmarshal(*responseMap[key], &result)
  69. return
  70. }
  71. func InitStructWithDefaultTag(bean interface{}) {
  72. configType := reflect.TypeOf(bean)
  73. for i := 0; i < configType.Elem().NumField(); i++ {
  74. field := configType.Elem().Field(i)
  75. defaultValue := field.Tag.Get("default")
  76. if defaultValue == "" {
  77. continue
  78. }
  79. setter := reflect.ValueOf(bean).Elem().Field(i)
  80. switch field.Type.String() {
  81. case "int":
  82. intValue, _ := strconv.ParseInt(defaultValue, 10, 64)
  83. setter.SetInt(intValue)
  84. case "time.Duration":
  85. intValue, _ := strconv.ParseInt(defaultValue, 10, 64)
  86. setter.SetInt(intValue)
  87. case "string":
  88. setter.SetString(defaultValue)
  89. case "bool":
  90. boolValue, _ := strconv.ParseBool(defaultValue)
  91. setter.SetBool(boolValue)
  92. }
  93. }
  94. }