utils.go
1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package common
import (
"crypto/md5"
"encoding/hex"
"google.golang.org/protobuf/reflect/protoreflect"
"math/rand"
"strings"
"time"
)
func Md5V(str string) string {
h := md5.New()
h.Write([]byte(str))
return hex.EncodeToString(h.Sum(nil))
}
func Timex() int64 {
return time.Now().Unix()
}
var defaultLetters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
func RandomString(n int, allowedChars ...[]rune) string {
var letters []rune
if len(allowedChars) == 0 {
letters = defaultLetters
} else {
letters = allowedChars[0]
}
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func RandomName(name [][]string) string {
idx1 := rand.Intn(len(name[0]))
idx2 := rand.Intn(len(name[1]))
var builder strings.Builder
builder.WriteString(name[0][idx1])
builder.WriteString(name[1][idx2])
return builder.String()
}
//英文字符串,第一个字符大写
func FirstCharToUpper(key string) string {
first := strings.ToUpper(key[0:1])
str := strings.Builder{}
str.WriteString(first)
str.WriteString(key[1:])
return str.String()
}
func ParseFields(message protoreflect.Message ,properties map[string]interface{}) []int32 {
ids := make([]int32,0)
for k, v := range properties {
name := protoreflect.Name(strings.ToLower(k))
field := message.Descriptor().Fields().ByName(name)
if field == nil {
continue
}
ids = append(ids, int32(field.Index()))
message.Set(field, protoreflect.ValueOf(v))
}
return ids
}