-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathid.go
75 lines (59 loc) · 1.44 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
package goat
import (
"database/sql/driver"
"github.com/google/uuid"
)
// ID is a UUID type that implements a custom Value function for storing UUIDs as binary(16) columns in a database using Gorm.
type ID uuid.UUID
func (id *ID) Scan(src any) error {
uid := (*uuid.UUID)(id)
return uid.Scan(src)
}
func (id ID) Value() (driver.Value, error) {
return id[:], nil
}
func (id ID) String() string {
return uuid.UUID(id).String()
}
// MarshalText implements encoding.TextMarshaler.
func (id ID) MarshalText() ([]byte, error) {
return uuid.UUID(id).MarshalText()
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (id *ID) UnmarshalText(data []byte) error {
uid := (*uuid.UUID)(id)
return uid.UnmarshalText(data)
}
// MarshalBinary implements encoding.BinaryMarshaler.
func (id ID) MarshalBinary() ([]byte, error) {
return id[:], nil
}
// UnmarshalBinary implements encoding.BinaryUnmarshaler.
func (id *ID) UnmarshalBinary(data []byte) error {
uid := (*uuid.UUID)(id)
return uid.UnmarshalBinary(data)
}
func (id ID) Valid() bool {
return uuid.UUID(id) != uuid.Nil
}
func NewID() ID {
return ID(uuid.New())
}
func NilID() ID {
return ID(uuid.Nil)
}
func ParseID(s string) (ID, error) {
id, err := uuid.Parse(s)
return ID(id), err
}
func ParseAllIDs(s []string) ([]ID, error) {
var ids []ID
for _, i := range s {
parsed, err := ParseID(i)
if err != nil {
return nil, err
}
ids = append(ids, parsed)
}
return ids, nil
}