forked from hallgren/eventsourcing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
idgenerator.go
41 lines (35 loc) · 877 Bytes
/
idgenerator.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
package eventsourcing
import (
"crypto/rand"
)
// idFunc is a global function that generates aggregate id's.
// It could be changed from the outside via the SetIDFunc function.
var idFunc = randSeq
// SetIDFunc is used to change how aggregate ID's are generated
// default is a random string
func SetIDFunc(f func() string) {
idFunc = f
}
func randSeq() string {
id, err := generateRandomString(20)
if err != nil {
return ""
}
return id
}
func generateRandomString(n int) (string, error) {
const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-"
bytes, err := generateRandomBytes(n)
if err != nil {
return "", err
}
for i, b := range bytes {
bytes[i] = letters[b%byte(len(letters))]
}
return string(bytes), nil
}
func generateRandomBytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
return b, err
}