-
Notifications
You must be signed in to change notification settings - Fork 0
/
id.go
260 lines (195 loc) · 4.65 KB
/
id.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package ulid
import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"time"
"github.com/gocql/gocql"
"github.com/jackc/pgx/v5/pgtype"
"github.com/oklog/ulid/v2"
)
const (
length = 16
)
// ID is an alias of ulid.ULID.
type ID []byte
type IDs []ID
func (ids IDs) String() []string {
result := make([]string, len(ids))
for i, id := range ids {
result[i] = id.String()
}
return result
}
func (ids IDs) Any() []any {
result := make([]any, len(ids))
for i, id := range ids {
result[i] = id
}
return result
}
var zero = make(ID, length)
// NewID returns a new random ID.
func NewID() ID {
return ID(ulid.MustNew(ulid.Timestamp(time.Now()), rand.Reader).Bytes())
}
// NewZeroID returns a new zero ID.
func NewZeroID() ID {
return make(ID, length)
}
// ScanUUID implements pgtype.UUIDScanner interface
func (id *ID) ScanUUID(v pgtype.UUID) error {
if !v.Valid {
return ErrInvalidULID{Value: v.Bytes}
}
return id.Unmarshal(v.Bytes[:])
}
// UUIDValue implements pgtype.UUIDValuer interface
func (id ID) UUIDValue() (pgtype.UUID, error) {
return pgtype.UUID{Bytes: [16]byte(id.Bytes()), Valid: true}, nil
}
// MarshalCQL override marshalling to fit CQL UUID.
func (id ID) MarshalCQL(info gocql.TypeInfo) ([]byte, error) {
if len(id) != length {
id = zero
}
raw, err := gocql.UUIDFromBytes(id)
return raw.Bytes(), err
}
// UnmarshalCQL override unmarshalling to fit CQL UUID.
func (id *ID) UnmarshalCQL(info gocql.TypeInfo, data []byte) error {
return id.Unmarshal(data)
}
// NewIDs returns a new array of random IDs.
func NewIDs(n int) []ID {
t := ulid.Timestamp(time.Now())
ids := make([]ID, n)
for i := range ids {
ids[i] = ID(ulid.MustNew(t, rand.Reader).Bytes())
}
return ids
}
// NewTimeID returns a new random ID based on time t.
func NewTimeID(t uint64) ID {
return ID(ulid.MustNew(t, rand.Reader).Bytes())
}
// MustParse alias ulid.MustParse. Panics if s is invalid.
func MustParse(s string) ID {
return ID(ulid.MustParse(s).Bytes())
}
// Parse alias ulid.Parse. Panics if s is invalid.
func Parse(s string) (ID, error) {
id, err := ulid.Parse(s)
return ID(id.Bytes()), err
}
// Time returns ms time of ID.
func (id ID) Time() uint64 {
var ul ulid.ULID
if len(id) != length {
return zero.Time()
}
copy(ul[:], id)
return ul.Time()
}
// IsZero returns if ID is zero value or not.
func (id ID) IsZero() bool {
return len(id) != length || id[0] == 0
}
// ULID returns original type.
func (id ID) ULID() ulid.ULID {
var ul ulid.ULID
if len(id) != length {
return zero.ULID()
}
copy(ul[:], id)
return ul
}
// Copy returns a hard copy of bytes.
func (id ID) Copy() ID {
cp := make(ID, length)
copy(cp[:], id)
return cp
}
// String returns a human readable string ID.
func (id ID) String() string {
var ul ulid.ULID
if len(id) != length {
return zero.String()
}
copy(ul[:], id)
return ul.String()
}
// Bytes returns id as byte slice for protobuf marshaling.
func (id ID) Bytes() []byte {
return id[:]
}
// Marshal never returns any error..
func (id ID) Marshal() ([]byte, error) {
return id, nil
}
// MarshalTo never returns any error.
func (id ID) MarshalTo(data []byte) (n int, err error) {
copy(data[0:length], id[:])
return len(id), nil
}
// Unmarshal never returns any error.
func (id *ID) Unmarshal(data []byte) error {
if len(data) != length {
return ulid.ErrBufferSize
}
if len(*id) != length {
*id = make(ID, length)
}
for i := 0; i < length; i++ {
(*id)[i] = data[i]
}
return nil
}
// Size always returns length.
func (id *ID) Size() int {
return length
}
// MarshalJSON returns id in human readable string format.
func (id ID) MarshalJSON() ([]byte, error) {
return json.Marshal(base64.StdEncoding.EncodeToString(id))
}
// UnmarshalJSON unmarshals and valid data.
func (id *ID) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
raw, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return err
}
*id = ID(raw) //nolint: wsl
return nil
}
// Compare only required if the compare option is set.
func (id ID) Compare(other ID) int {
var ul, ulOther ulid.ULID
if len(other) != length || len(id) != length {
return -1
}
copy(ul[:], id)
copy(ulOther[:], other[0:length])
return ul.Compare(ulOther)
}
// Equal only required if the equal option is set.
func (id ID) Equal(other ID) bool {
return id.Compare(other) == 0
}
// NewPopulatedID only required if populate option is set.
func NewPopulatedID(r randyID) *ID {
id := ID(ulid.MustNew(uint64(r.Uint32()), rand.Reader).Bytes())
return &id
}
type randyID interface {
Float32() float32
Float64() float64
Int63() int64
Int31() int32
Uint32() uint32
Intn(n int) int
}