This repository has been archived by the owner on May 9, 2020. It is now read-only.
forked from bakanis/uuid
-
Notifications
You must be signed in to change notification settings - Fork 13
/
types.go
175 lines (143 loc) · 4.18 KB
/
types.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package uuid
import (
"database/sql/driver"
"fmt"
"errors"
)
const (
length = 16
variantIndex = 8
versionIndex = 6
)
// **************************************************** Create UUIDs
func (o *UUID) unmarshal(data []byte) {
copy(o[:], data)
}
// Set the three most significant bits (bits 0, 1 and 2) of the
// sequenceHiAndVariant equivalent in the array to ReservedRFC4122.
func (o *UUID) setRFC4122Version(version Version) {
o[versionIndex] &= 0x0f
o[versionIndex] |= uint8(version << 4)
o[variantIndex] &= variantSet
o[variantIndex] |= VariantRFC4122
}
// **************************************************** Default implementation
var _ Implementation = &UUID{}
// UUID is the default RFC implementation. All uuid functions will return this
// type.
type UUID [length]byte
// Size returns the octet length of the Uuid
func (o UUID) Size() int {
return length
}
// Version returns the uuid.Version of the Uuid
func (o UUID) Version() Version {
return resolveVersion(o[versionIndex] >> 4)
}
// Variant returns the implementation variant of the Uuid
func (o UUID) Variant() uint8 {
return variant(o[variantIndex])
}
// Bytes return the underlying data representation of the Uuid.
func (o UUID) Bytes() []byte {
return o[:]
}
// String returns the canonical string representation of the UUID or the
// uuid.Format the package is set to via uuid.SwitchFormat
func (o UUID) String() string {
return formatUuid(o[:], printFormat)
}
// **************************************************** Implementations
// MarshalBinary implements the encoding.BinaryMarshaler interface
func (o UUID) MarshalBinary() ([]byte, error) {
return o.Bytes(), nil
}
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface
func (o *UUID) UnmarshalBinary(bytes []byte) error {
if len(bytes) != o.Size() {
return errors.New("uuid: invalid length")
}
o.unmarshal(bytes)
return nil
}
// MarshalText implements the encoding.TextMarshaler interface. It will marshal
// text into one of the known formats, if you have changed to a custom Format
// the text be output in canonical format.
func (o UUID) MarshalText() ([]byte, error) {
f := FormatCanonical
if defaultFormats[printFormat] {
f = printFormat
}
return []byte(formatUuid(o.Bytes(), f)), nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface. It will
// support any text that MarshalText can produce.
func (o *UUID) UnmarshalText(uuid []byte) error {
id, err := parse(string(uuid))
if err == nil {
o.UnmarshalBinary(id)
}
return err
}
// Value implements the driver.Valuer interface
func (o UUID) Value() (value driver.Value, err error) {
if IsNil(o) {
value, err = nil, nil
return
}
value, err = o.MarshalText()
return
}
// Scan implements the sql.Scanner interface
func (o *UUID) Scan(src interface{}) error {
if src == nil {
return nil
}
if src == "" {
return nil
}
switch src := src.(type) {
case string:
return o.UnmarshalText([]byte(src))
case []byte:
if len(src) == length {
return o.UnmarshalBinary(src)
} else {
return o.UnmarshalText(src)
}
default:
return fmt.Errorf("uuid: cannot scan type [%T] into UUID", src)
}
}
// **************************************************** Immutable UUID
var _ Implementation = new(Immutable)
// Immutable is an easy to use UUID which can be used as a key or for constants
type Immutable string
// Size returns the octet length of the Uuid
func (o Immutable) Size() int {
return length
}
// Version returns the uuid.Version of the Uuid
func (o Immutable) Version() Version {
return resolveVersion(o[versionIndex] >> 4)
}
// Variant returns the implementation variant of the Uuid
func (o Immutable) Variant() uint8 {
return variant(o[variantIndex])
}
// Bytes return the underlying data representation of the Uuid in network byte
// order
func (o Immutable) Bytes() []byte {
return []byte(o)
}
// String returns the canonical string representation of the UUID or the
// uuid.Format the package is set to via uuid.SwitchFormat
func (o Immutable) String() string {
return formatUuid([]byte(o), printFormat)
}
// UUID converts this implementation to the default type uuid.UUID
func (o Immutable) UUID() UUID {
id := UUID{}
id.unmarshal(o.Bytes())
return id
}