/
uuid.go
161 lines (136 loc) · 3.38 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
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
package uuid
import (
"database/sql/driver"
"github.com/pkg/errors"
"github.com/jackc/pgx/pgtype"
uuid "github.com/satori/go.uuid"
)
var errUndefined = errors.New("cannot encode status undefined")
type UUID struct {
UUID uuid.UUID
Status pgtype.Status
}
func (dst *UUID) Set(src interface{}) error {
switch value := src.(type) {
case uuid.UUID:
*dst = UUID{UUID: value, Status: pgtype.Present}
case [16]byte:
*dst = UUID{UUID: uuid.UUID(value), Status: pgtype.Present}
case []byte:
if len(value) != 16 {
return errors.Errorf("[]byte must be 16 bytes to convert to UUID: %d", len(value))
}
*dst = UUID{Status: pgtype.Present}
copy(dst.UUID[:], value)
case string:
uuid, err := uuid.FromString(value)
if err != nil {
return err
}
*dst = UUID{UUID: uuid, Status: pgtype.Present}
default:
// If all else fails see if pgtype.UUID can handle it. If so, translate through that.
pgUUID := &pgtype.UUID{}
if err := pgUUID.Set(value); err != nil {
return errors.Errorf("cannot convert %v to UUID", value)
}
*dst = UUID{UUID: uuid.UUID(pgUUID.Bytes), Status: pgUUID.Status}
}
return nil
}
func (dst *UUID) Get() interface{} {
switch dst.Status {
case pgtype.Present:
return dst.UUID
case pgtype.Null:
return nil
default:
return dst.Status
}
}
func (src *UUID) AssignTo(dst interface{}) error {
switch src.Status {
case pgtype.Present:
switch v := dst.(type) {
case *uuid.UUID:
*v = src.UUID
case *[16]byte:
*v = [16]byte(src.UUID)
return nil
case *[]byte:
*v = make([]byte, 16)
copy(*v, src.UUID[:])
return nil
case *string:
*v = src.UUID.String()
return nil
default:
if nextDst, retry := pgtype.GetAssignToDstType(v); retry {
return src.AssignTo(nextDst)
}
}
case pgtype.Null:
return pgtype.NullAssignTo(dst)
}
return errors.Errorf("cannot assign %v into %T", src, dst)
}
func (dst *UUID) DecodeText(ci *pgtype.ConnInfo, src []byte) error {
if src == nil {
*dst = UUID{Status: pgtype.Null}
return nil
}
u, err := uuid.FromString(string(src))
if err != nil {
return err
}
*dst = UUID{UUID: u, Status: pgtype.Present}
return nil
}
func (dst *UUID) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error {
if src == nil {
*dst = UUID{Status: pgtype.Null}
return nil
}
if len(src) != 16 {
return errors.Errorf("invalid length for UUID: %v", len(src))
}
*dst = UUID{Status: pgtype.Present}
copy(dst.UUID[:], src)
return nil
}
func (src *UUID) EncodeText(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
switch src.Status {
case pgtype.Null:
return nil, nil
case pgtype.Undefined:
return nil, errUndefined
}
return append(buf, src.UUID.String()...), nil
}
func (src *UUID) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
switch src.Status {
case pgtype.Null:
return nil, nil
case pgtype.Undefined:
return nil, errUndefined
}
return append(buf, src.UUID[:]...), nil
}
// Scan implements the database/sql Scanner interface.
func (dst *UUID) Scan(src interface{}) error {
if src == nil {
*dst = UUID{Status: pgtype.Null}
return nil
}
switch src := src.(type) {
case string:
return dst.DecodeText(nil, []byte(src))
case []byte:
return dst.DecodeText(nil, src)
}
return errors.Errorf("cannot scan %T", src)
}
// Value implements the database/sql/driver Valuer interface.
func (src *UUID) Value() (driver.Value, error) {
return pgtype.EncodeValueText(src)
}