-
Notifications
You must be signed in to change notification settings - Fork 2
/
uuid.go
147 lines (117 loc) · 3.15 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package uuid
import (
"bytes"
"fmt"
"github.com/wayn3h0/go-uuid/internal/dcesecurity"
"github.com/wayn3h0/go-uuid/internal/layout"
"github.com/wayn3h0/go-uuid/internal/namebased/md5"
"github.com/wayn3h0/go-uuid/internal/namebased/sha1"
"github.com/wayn3h0/go-uuid/internal/random"
"github.com/wayn3h0/go-uuid/internal/timebased"
"github.com/wayn3h0/go-uuid/internal/version"
)
var (
Nil = UUID{} // Nil UUID
)
// NewTimeBased returns a new time based UUID (version 1).
func NewTimeBased() (UUID, error) {
u, err := timebased.New()
if err != nil {
return Nil, err
}
uuid := UUID{}
copy(uuid[:], u)
return uuid, nil
}
// NewV1 same as NewTimeBased.
func NewV1() (UUID, error) {
return NewTimeBased()
}
// NewDCESecurity returns a new DCE security UUID (version 2).
func NewDCESecurity(domain Domain) (UUID, error) {
u, err := dcesecurity.New(dcesecurity.Domain(domain))
if err != nil {
return Nil, err
}
uuid := UUID{}
copy(uuid[:], u)
return uuid, nil
}
// NewV2 same as NewDCESecurity.
func NewV2(domain Domain) (UUID, error) {
return NewDCESecurity(domain)
}
// NewNameBasedMD5 returns a new name based UUID with MD5 hash (version 3).
func NewNameBasedMD5(namespace, name string) (UUID, error) {
u, err := md5.New(namespace, name)
if err != nil {
return Nil, err
}
uuid := UUID{}
copy(uuid[:], u)
return uuid, nil
}
// NewV3 same as NewNameBasedMD5.
func NewV3(namespace, name string) (UUID, error) {
return NewNameBasedMD5(namespace, name)
}
// NewRandom returns a new random UUID (version 4).
func NewRandom() (UUID, error) {
u, err := random.New()
if err != nil {
return Nil, err
}
uuid := UUID{}
copy(uuid[:], u)
return uuid, nil
}
// NewV4 same as NewRandom.
func NewV4() (UUID, error) {
return NewRandom()
}
// New same as NewRandom.
func New() (UUID, error) {
return NewRandom()
}
// NewNameBasedSHA1 returns a new name based UUID with SHA1 hash (version 5).
func NewNameBasedSHA1(namespace, name string) (UUID, error) {
u, err := sha1.New(namespace, name)
if err != nil {
return Nil, err
}
uuid := UUID{}
copy(uuid[:], u)
return uuid, nil
}
// NewV5 same as NewNameBasedSHA1.
func NewV5(namespace, name string) (UUID, error) {
return NewNameBasedSHA1(namespace, name)
}
// UUID respresents an UUID type compliant with specification in RFC 4122.
type UUID [16]byte
// Layout returns layout of UUID.
func (this UUID) Layout() Layout {
return Layout(layout.Get(this[:]))
}
// Version returns version of UUID.
func (this UUID) Version() Version {
return Version(version.Get(this[:]))
}
// Equal returns true if current uuid equal to passed uuid.
func (this UUID) Equal(another UUID) bool {
return bytes.EqualFold(this[:], another[:])
}
// Format returns the formatted string of UUID.
func (this UUID) Format(style Style) string {
switch style {
case StyleWithoutDash:
return fmt.Sprintf("%x", this[:])
//case StyleStandard:
default:
return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x", this[:4], this[4:6], this[6:8], this[8:10], this[10:])
}
}
// String returns the string of UUID with standard style(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | 8-4-4-4-12).
func (this UUID) String() string {
return this.Format(StyleStandard)
}