-
Notifications
You must be signed in to change notification settings - Fork 0
/
uuidbin16.go
56 lines (47 loc) · 1 KB
/
uuidbin16.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
package uuidbin16
import (
"database/sql/driver"
"github.com/google/uuid"
)
type UUIDBin16 uuid.UUID
func StringToBin16(s string) (UUIDBin16, error) {
if s == "" {
return UUIDBin16(uuid.Nil), nil
}
u, err := uuid.Parse(s)
if err != nil {
return UUIDBin16{}, err
}
return UUIDBin16(u), nil
}
func (u UUIDBin16) String() string {
return uuid.UUID(u).String()
}
func (u UUIDBin16) GormType() string {
return "binary(16)"
}
func (u UUIDBin16) MarshalJSON() ([]byte, error) {
str := "\"" + u.String() + "\""
return []byte(str), nil
}
func (u *UUIDBin16) UnmarshalJSON(b []byte) error {
s, err := uuid.ParseBytes(b)
*u = UUIDBin16(s)
return err
}
func (u *UUIDBin16) Scan(src interface{}) error {
if src == nil {
*u = UUIDBin16(uuid.Nil)
return nil
}
bytes := src.([]byte)
parseBytes, err := uuid.FromBytes(bytes)
*u = UUIDBin16(parseBytes)
return err
}
func (u UUIDBin16) Value() (driver.Value, error) {
if u == UUIDBin16(uuid.Nil) {
return nil, nil
}
return uuid.UUID(u).MarshalBinary()
}