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
174 lines (142 loc) · 4.25 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
package uuid
import (
"database/sql/driver"
"fmt"
"strings"
)
const (
length = 16
variantIndex = 8
versionIndex = 6
)
// **************************************************** Create UUIDs
type array [length]byte
func (o *array) unmarshal(pData []byte) {
copy(o[:], pData)
}
// Set the three most significant bits (bits 0, 1 and 2) of the
// sequenceHiAndVariant equivalent in the array to ReservedRFC4122.
func (o *array) setRFC4122Version(pVersion uint8) {
o[versionIndex] &= 0x0f
o[versionIndex] |= uint8(pVersion << 4)
o[variantIndex] &= variantSet
o[variantIndex] |= VariantRFC4122
}
// **************************************************** Default implementation
var _ UUID = &Uuid{}
// Uuid is the default UUID implementation. All uuid functions will return this
// type. All uuid functions should use UUID as their in parameter.
type Uuid []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 in network byte
// order
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(pBytes []byte) error {
if len(pBytes) != o.Size() {
return fmt.Errorf("uuid.Uuid.UnmarshalBinary: invalid length")
}
if len(*o) != 0 {
panic("uuid.Uuid.UnmarshalBinary: you must use an empty or new Uuid to unmarhal bytes")
}
*o = append(*o, pBytes...)
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
func (o Uuid) MarshalText() ([]byte, error) {
f := FormatCanonical
if defaultFormats[printFormat] {
f = printFormat
}
return []byte(strings.ToLower(string(format(o.Bytes(), string(f))))), nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface. It will
// support any text that MarshalText can produce.
func (o *Uuid) UnmarshalText(pUuid []byte) error {
id, err := parse(string(pUuid))
if err == nil {
o.UnmarshalBinary(id)
}
return err
}
// Value implements the driver.Valuer interface
func (o Uuid) Value() (value driver.Value, err error) {
if len(o) == 0 {
value, err = nil, nil
return
}
value, err = o.MarshalText()
return
}
// Scan implements the sql.Scanner interface
func (o *Uuid) Scan(pSrc interface{}) error {
if pSrc == nil {
return nil
}
if pSrc == "" {
return nil
}
switch src := pSrc.(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.Uuid.Scan: cannot scan type %T into Uuid", pSrc)
}
}
// **************************************************** Immutable UUID
var _ UUID = 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 Uuid(o).Bytes()
}
// 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 Uuid(o).String()
}