7
0
gaore-common-sdk-go/sdk/utils/utils.go

112 lines
2.4 KiB
Go
Raw Normal View History

2020-08-04 00:51:19 +08:00
package utils
2020-08-04 21:14:06 +08:00
import (
"crypto/md5"
"crypto/rand"
"encoding/hex"
2020-08-04 21:14:06 +08:00
"fmt"
"hash"
rand2 "math/rand"
2020-08-04 21:14:06 +08:00
"net/url"
"reflect"
"sort"
2020-08-04 21:14:06 +08:00
"strconv"
"strings"
"time"
)
2020-08-04 00:51:19 +08:00
type UUID [16]byte
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func GetUUID() (uuidHex string) {
uuid := NewUUID()
uuidHex = hex.EncodeToString(uuid[:])
return
}
func NewUUID() UUID {
ns := UUID{}
safeRandom(ns[:])
u := newFromHash(md5.New(), ns, RandStringBytes(16))
u[6] = (u[6] & 0x0f) | (byte(2) << 4)
u[8] = (u[8]&(0xff>>2) | (0x02 << 6))
return u
}
func RandStringBytes(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand2.Intn(len(letterBytes))]
}
return string(b)
}
func newFromHash(h hash.Hash, ns UUID, name string) UUID {
u := UUID{}
h.Write(ns[:])
h.Write([]byte(name))
copy(u[:], h.Sum(nil))
return u
}
func safeRandom(dest []byte) {
if _, err := rand.Read(dest); err != nil {
panic(err)
}
}
2020-08-04 00:51:19 +08:00
func GetUrlFormedMap(source map[string]string) (urlEncoded string) {
urlEncoder := url.Values{}
for key, value := range source {
urlEncoder.Add(key, value)
}
urlEncoded = urlEncoder.Encode()
return
}
2020-08-04 21:14:06 +08:00
func GetUrlByKeySort(source map[string]string) (url string) {
keys := make([]string, 0, len(source))
for k := range source {
keys = append(keys, k)
}
sort.Strings(keys)
for _, key := range keys {
url += fmt.Sprintf("%s=%s&", key, source[key])
}
return strings.TrimRight(url, "&")
}
2020-08-04 21:14:06 +08:00
func InitStructWithDefaultTag(bean interface{}) {
beantype := reflect.TypeOf(bean)
for i := 0; i < beantype.Elem().NumField(); i++ {
field := beantype.Elem().Field(i)
defaultValue := strings.TrimSpace(field.Tag.Get("default"))
if defaultValue == "" || defaultValue == "-" {
continue
}
setter := reflect.ValueOf(bean).Elem().Field(i)
fieldTypeName := field.Type.String()
switch fieldTypeName {
case "int", "int64", "int32", "int8", "int16":
intval, _ := strconv.ParseInt(defaultValue, 10, 64)
setter.SetInt(intval)
case "uint", "uint8", "uint16", "uint32", "uint64", "uintptr":
uintval, _ := strconv.ParseUint(defaultValue, 10, 64)
setter.SetUint(uintval)
case "string":
setter.SetString(defaultValue)
case "time.Duration":
intval, _ := strconv.ParseInt(defaultValue, 10, 64)
setter.SetInt(intval * int64(time.Second))
case "bool":
boolval, _ := strconv.ParseBool(defaultValue)
setter.SetBool(boolval)
default:
fmt.Println(field.Type.String(), field.Name)
}
}
}