forked from bxcodec/faker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uuid.go
90 lines (77 loc) · 1.74 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package faker
import (
"crypto/rand"
"fmt"
"io"
"reflect"
)
var identifier Identifier
// GetIdentifier returns a new Identifier
func GetIdentifier() Identifier {
mu.Lock()
defer mu.Unlock()
if identifier == nil {
identifier = &UUID{}
}
return identifier
}
// Identifier ...
type Identifier interface {
Digit(v reflect.Value) (interface{}, error)
Hyphenated(v reflect.Value) (interface{}, error)
}
// UUID struct
type UUID struct{}
// createUUID returns a 16 byte slice with random values
func createUUID() ([]byte, error) {
b := make([]byte, 16)
_, err := io.ReadFull(rand.Reader, b)
if err != nil {
return b, err
}
// variant bits; see section 4.1.1
b[8] = b[8]&^0xc0 | 0x80
// version 4 (pseudo-random); see section 4.1.3
b[6] = b[6]&^0xf0 | 0x40
return b, nil
}
func (u UUID) hyphenated() (string, error) {
b, err := createUUID()
if err != nil {
return "", err
}
uuid := fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
return uuid, err
}
// Hyphenated returns a 36 byte hyphenated UUID
func (u UUID) Hyphenated(v reflect.Value) (interface{}, error) {
return u.hyphenated()
}
// UUIDHyphenated get fake Hyphenated UUID
func UUIDHyphenated() string {
return singleFakeData(HyphenatedID, func() interface{} {
u := UUID{}
res, _ := u.hyphenated()
return res
}).(string)
}
func (u UUID) digit() (string, error) {
b, err := createUUID()
if err != nil {
return "", err
}
uuid := fmt.Sprintf("%x", b)
return uuid, err
}
// Digit returns a 32 bytes UUID
func (u UUID) Digit(v reflect.Value) (interface{}, error) {
return u.digit()
}
// UUIDDigit get fake Digit UUID
func UUIDDigit() string {
return singleFakeData(ID, func() interface{} {
u := UUID{}
res, _ := u.digit()
return res
}).(string)
}