-
Notifications
You must be signed in to change notification settings - Fork 4
/
uuid.go
131 lines (99 loc) · 3.31 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
package uuid
import (
"fmt"
"github.com/gofrs/uuid/v5"
"github.com/jackc/pgx/v5/pgtype"
)
type UUID uuid.UUID
func (u *UUID) ScanUUID(v pgtype.UUID) error {
if !v.Valid {
return fmt.Errorf("cannot scan NULL into *uuid.UUID")
}
*u = v.Bytes
return nil
}
func (u UUID) UUIDValue() (pgtype.UUID, error) {
return pgtype.UUID{Bytes: [16]byte(u), Valid: true}, nil
}
type NullUUID uuid.NullUUID
func (u *NullUUID) ScanUUID(v pgtype.UUID) error {
*u = NullUUID{UUID: uuid.UUID(v.Bytes), Valid: v.Valid}
return nil
}
func (u NullUUID) UUIDValue() (pgtype.UUID, error) {
return pgtype.UUID{Bytes: [16]byte(u.UUID), Valid: u.Valid}, nil
}
func TryWrapUUIDEncodePlan(value interface{}) (plan pgtype.WrappedEncodePlanNextSetter, nextValue interface{}, ok bool) {
switch value := value.(type) {
case uuid.UUID:
return &wrapUUIDEncodePlan{}, UUID(value), true
case uuid.NullUUID:
return &wrapNullUUIDEncodePlan{}, NullUUID(value), true
}
return nil, nil, false
}
type wrapUUIDEncodePlan struct {
next pgtype.EncodePlan
}
func (plan *wrapUUIDEncodePlan) SetNext(next pgtype.EncodePlan) { plan.next = next }
func (plan *wrapUUIDEncodePlan) Encode(value interface{}, buf []byte) (newBuf []byte, err error) {
return plan.next.Encode(UUID(value.(uuid.UUID)), buf)
}
type wrapNullUUIDEncodePlan struct {
next pgtype.EncodePlan
}
func (plan *wrapNullUUIDEncodePlan) SetNext(next pgtype.EncodePlan) { plan.next = next }
func (plan *wrapNullUUIDEncodePlan) Encode(value interface{}, buf []byte) (newBuf []byte, err error) {
return plan.next.Encode(NullUUID(value.(uuid.NullUUID)), buf)
}
func TryWrapUUIDScanPlan(target interface{}) (plan pgtype.WrappedScanPlanNextSetter, nextDst interface{}, ok bool) {
switch target := target.(type) {
case *uuid.UUID:
return &wrapUUIDScanPlan{}, (*UUID)(target), true
case *uuid.NullUUID:
return &wrapNullUUIDScanPlan{}, (*NullUUID)(target), true
}
return nil, nil, false
}
type wrapUUIDScanPlan struct {
next pgtype.ScanPlan
}
func (plan *wrapUUIDScanPlan) SetNext(next pgtype.ScanPlan) { plan.next = next }
func (plan *wrapUUIDScanPlan) Scan(src []byte, dst interface{}) error {
return plan.next.Scan(src, (*UUID)(dst.(*uuid.UUID)))
}
type wrapNullUUIDScanPlan struct {
next pgtype.ScanPlan
}
func (plan *wrapNullUUIDScanPlan) SetNext(next pgtype.ScanPlan) { plan.next = next }
func (plan *wrapNullUUIDScanPlan) Scan(src []byte, dst interface{}) error {
return plan.next.Scan(src, (*NullUUID)(dst.(*uuid.NullUUID)))
}
type UUIDCodec struct {
pgtype.UUIDCodec
}
func (UUIDCodec) DecodeValue(tm *pgtype.Map, oid uint32, format int16, src []byte) (interface{}, error) {
if src == nil {
return nil, nil
}
var target uuid.UUID
scanPlan := tm.PlanScan(oid, format, &target)
if scanPlan == nil {
return nil, fmt.Errorf("PlanScan did not find a plan")
}
err := scanPlan.Scan(src, &target)
if err != nil {
return nil, err
}
return target, nil
}
// Register registers the github.com/gofrs/uuid integration with a pgtype.Map.
func Register(tm *pgtype.Map) {
tm.TryWrapEncodePlanFuncs = append([]pgtype.TryWrapEncodePlanFunc{TryWrapUUIDEncodePlan}, tm.TryWrapEncodePlanFuncs...)
tm.TryWrapScanPlanFuncs = append([]pgtype.TryWrapScanPlanFunc{TryWrapUUIDScanPlan}, tm.TryWrapScanPlanFuncs...)
tm.RegisterType(&pgtype.Type{
Name: "uuid",
OID: pgtype.UUIDOID,
Codec: UUIDCodec{},
})
}