-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuuid.go
137 lines (119 loc) · 2.69 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
package uuid
import (
"bytes"
"database/sql/driver"
"encoding/hex"
"fmt"
"github.com/gofrs/uuid"
)
// swagger:strfmt uuid
type UUID [16]byte
var Zero = UUID{}
func (u UUID) IsZero() bool {
return u == Zero
}
func (u UUID) String() string {
buf := make([]byte, 32)
hex.Encode(buf[:], u[:])
return string(buf)
}
func (u UUID) Bytes() []byte {
return u[:]
}
// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (u UUID) MarshalBinary() (data []byte, err error) {
data = u.Bytes()
return
}
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
// It will return error if the slice isn't 16 bytes long.
func (u *UUID) UnmarshalBinary(data []byte) (err error) {
if len(data) != 16 {
err = fmt.Errorf("uuid: UUID must be exactly 16 bytes long, got %d bytes", len(data))
return
}
copy(u[:], data)
return
}
func (u *UUID) UnmarshalText(text []byte) (err error) {
if len(text) < 32 {
err = fmt.Errorf("uuid: invalid UUID string: %s", text)
return
}
if len(text) == 36 {
text = bytes.Replace(text, []byte("-"), []byte(""), -1)
}
_, err = hex.Decode(u[:], text)
if err != nil {
return
}
return
}
func (u UUID) MarshalText() (data []byte, err error) {
data = []byte(u.String())
return
}
// Value implements the driver.Valuer interface.
func (u UUID) Value() (driver.Value, error) {
if u.IsZero() {
return nil, nil
}
return u.String(), nil
}
// Scan implements the sql.Scanner interface.
// A 16-byte slice is handled by UnmarshalBinary, while
// a longer byte slice or a string is handled by UnmarshalText.
func (u *UUID) Scan(src interface{}) error {
switch src := src.(type) {
case []byte:
if len(src) == 16 {
return u.UnmarshalBinary(src)
} else {
//return fmt.Errorf("uuid: it is not a length of 16, its length is %v and its value is %s", len(src), src)
}
return u.UnmarshalText(src)
case string:
return u.UnmarshalText([]byte(src))
case nil:
u = &UUID{}
return nil
}
return fmt.Errorf("uuid: cannot convert %T to UUID", src)
}
// FromBytes returns UUID converted from raw byte slice input.
// It will return error if the slice isn't 16 bytes long.
func FromBytes(input []byte) (u UUID, err error) {
err = u.UnmarshalBinary(input)
return
}
func FromString(input string) (u UUID, err error) {
err = u.UnmarshalText([]byte(input))
return
}
func NewV1() (UUID, error) {
var u UUID
n, err := uuid.NewV1()
if err != nil {
return u, err
}
u = UUID(n)
return u, nil
}
func NewV7() (UUID, error) {
var u UUID
n, err := uuid.NewV7()
if err != nil {
return u, err
}
u = UUID(n)
return u, nil
}
func NewV4() (UUID, error) {
var u UUID
n, err := uuid.NewV4()
if err != nil {
return u, err
}
u = UUID(n)
return u, nil
}