-
Notifications
You must be signed in to change notification settings - Fork 277
/
uuid.go
73 lines (58 loc) · 1.4 KB
/
uuid.go
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
package uuid
import (
"crypto/md5"
"fmt"
googleuuid "github.com/google/uuid"
"sort"
"strings"
)
var mock bool
//InitMock initializes mock flag => New() func will return mock value everytime
func InitMock() {
mock = true
}
//New returns uuid v4 string or the mocked value
func New() string {
if mock {
return "mockeduuid"
}
return googleuuid.New().String()
}
//NewLettersNumbers returns uuid without "-"
func NewLettersNumbers() string {
if mock {
return "mockeduuid"
}
uuidValue := googleuuid.New().String()
return strings.ReplaceAll(uuidValue, "-", "")
}
//GetHash returns GetKeysHash result with keys from m
func GetHash(m map[string]interface{}) string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return GetKeysHash(m, keys)
}
//GetKeysHash returns md5 hashsum of concatenated map values (sort keys before)
func GetKeysHash(m map[string]interface{}, keys []string) string {
sort.Strings(keys)
var str strings.Builder
for _, k := range keys {
str.WriteString(fmt.Sprint(m[k]))
str.WriteRune('|')
}
return fmt.Sprintf("%x", md5.Sum([]byte(str.String())))
}
//GetKeysUnhashed returns keys values joined by '_'
func GetKeysUnhashed(m map[string]interface{}, keys []string) string {
sort.Strings(keys)
var str strings.Builder
for i, k := range keys {
if i > 0 {
str.WriteRune('_')
}
str.WriteString(fmt.Sprint(m[k]))
}
return str.String()
}