-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprimarykey.go
106 lines (91 loc) · 2.66 KB
/
primarykey.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
package primarykey
import (
"database/sql/driver"
"encoding/json"
"github.com/google/uuid"
"github.com/lithammer/shortuuid/v4"
)
// ID is a type that stores the bytes of a UUID. It delegates a lot of the
// handling and parsing to `github.com/google/uuid` and stringifies the UUIDs
// using `github.com/lithammer/shortuuid`. It is compatible with the various
// database interfaces required to use it as a UUID column type.
type ID [16]byte
// String returns the shortuuid string representation of the UUID.
func (i ID) String() string {
return shortuuid.DefaultEncoder.Encode(i.UUID())
}
// UUID returns the underlying "long" UUID, which can be useful for
// compatibility with things that need the not-short UUID.
func (i ID) UUID() uuid.UUID {
return uuid.UUID(i)
}
// Scan implements the sql.Scanner interface to support reading UUID values out
// of a database into an ID type.
func (i *ID) Scan(src interface{}) error {
var u uuid.UUID
err := u.Scan(src)
if err != nil {
return err
}
*i = ID(u)
return nil
}
// Value implements the sql.Valuer interface to support converting the UUID to
// a value that can be stored by a database driver.
func (i ID) Value() (driver.Value, error) {
return i.UUID().String(), nil
}
// MarshalJSON satisfies the [encoding/json.Marshaler] interface so IDs get
// serialized to JSON in their short string form automatically.
func (i ID) MarshalJSON() ([]byte, error) {
return json.Marshal(i.String())
}
// UnmarshalJSON satisfies the [encoding/json.Unmarshaler] interface so the
// stringified IDs get decoded into [ID] structs automatically.
func (i *ID) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
v, err := Decode(s)
if err != nil {
return err
}
*i = v
return nil
}
// Decode parses the string as a shortuuid into an `ID`.
func Decode(src string) (ID, error) {
u, err := shortuuid.DefaultEncoder.Decode(src)
return ID(u), err
}
// MustDecode parses the string as an ID, similar to `Decode`, but panics if
// the string cannot be parsed.
func MustDecode(src string) ID {
id, err := Decode(src)
if err != nil {
panic(err)
}
return id
}
// Encode takes an ID and encodes it to a shortuuid string. This is equivalent
// to calling `String()` on the `ID`, but is provided for symmetry.
func Encode(id ID) string {
return id.String()
}
// New generates a new ID.
func New() ID {
return ID(uuid.New())
}
// Empty returns a "valid", but zeroed-out UUID.
func Empty() ID {
return ID{}
}
// FromBytes initializes an [ID] from a UUID byte slice.
func FromBytes(b []byte) (ID, error) {
uid, err := uuid.FromBytes(b)
if err != nil {
return Empty(), err
}
return ID(uid), nil
}