-
Notifications
You must be signed in to change notification settings - Fork 0
/
uuid.go
88 lines (68 loc) · 1.37 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
package uuid
import (
"database/sql/driver"
googleUUID "github.com/google/uuid"
)
func New() UUID {
u := googleUUID.New()
return UUID{
UUID: u,
Str: u.String(),
}
}
var Nil = googleUUID.Nil
func Parse(s string) (UUID, error) {
u := UUID{}
var err error
u.UUID, err = googleUUID.Parse(s)
if err == nil {
u.Str = u.UUID.String()
} else {
u.UUID = googleUUID.Nil
u.Str = googleUUID.Nil.String()
}
return u, err
}
func MustParse(s string) UUID {
u := UUID{}
u.UUID = googleUUID.MustParse(s)
u.Str = u.UUID.String()
return u
}
type UUID struct {
UUID googleUUID.UUID `json:"uuid"`
Str string `json:",omitempty"`
}
func (u UUID) String() string {
return u.Str
}
func (u UUID) IsNil() bool {
return u.UUID == Nil
}
func (u UUID) URN() string {
return u.UUID.URN()
}
func (u *UUID) Scan(src any) error {
err := u.UUID.Scan(src)
u.Str = u.UUID.String()
return err
}
func (u UUID) Value() (driver.Value, error) {
return u.UUID.Value()
}
func (u UUID) MarshalText() ([]byte, error) {
return u.UUID.MarshalText()
}
func (u *UUID) UnmarshalText(data []byte) error {
err := u.UUID.UnmarshalText(data)
u.Str = u.UUID.String()
return err
}
func (u UUID) MarshalBinary() ([]byte, error) {
return u.UUID.MarshalBinary()
}
func (u *UUID) UnmarshalBinary(data []byte) error {
err := u.UUID.UnmarshalBinary(data)
u.Str = u.UUID.String()
return err
}