-
Notifications
You must be signed in to change notification settings - Fork 5
/
uuid.go
151 lines (119 loc) · 4.21 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
package uuid
import (
"fmt"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
)
// UUID pgx type wrapper for google/uuid.UUID
type UUID uuid.UUID
// ScanUUID implements pgtype.UUIDScanner interface
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
}
// UUIDValue implements pgtype.UUIDValuer interface
func (u UUID) UUIDValue() (pgtype.UUID, error) {
return pgtype.UUID{Bytes: u, Valid: true}, nil
}
// NullUUID pgx type wrapper for google/uuid.NullUUID
type NullUUID uuid.NullUUID
// ScanUUID implements pgtype.UUIDScanner interface
func (u *NullUUID) ScanUUID(v pgtype.UUID) error {
*u = NullUUID{UUID: v.Bytes, Valid: v.Valid}
return nil
}
// UUIDValue implements pgtype.UUIDValuer interface
func (u NullUUID) UUIDValue() (pgtype.UUID, error) {
return pgtype.UUID{Bytes: u.UUID, Valid: u.Valid}, nil
}
// TryWrapUUIDEncodePlan implements pgtype.TryWrapEncodePlanFunc interface
func TryWrapUUIDEncodePlan(value any) (plan pgtype.WrappedEncodePlanNextSetter, nextValue any, 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
}
// SetNext implements pgtype.WrappedEncodePlanNextSetter interface
func (plan *wrapUUIDEncodePlan) SetNext(next pgtype.EncodePlan) { plan.next = next }
// Encode implements pgtype.EncodePlan interface
func (plan *wrapUUIDEncodePlan) Encode(value any, buf []byte) (newBuf []byte, err error) {
return plan.next.Encode(UUID(value.(uuid.UUID)), buf)
}
type wrapNullUUIDEncodePlan struct {
next pgtype.EncodePlan
}
// SetNext implements pgtype.WrappedEncodePlanNextSetter interface
func (plan *wrapNullUUIDEncodePlan) SetNext(next pgtype.EncodePlan) { plan.next = next }
// Encode implements pgtype.EncodePlan interface
func (plan *wrapNullUUIDEncodePlan) Encode(value any, buf []byte) (newBuf []byte, err error) {
return plan.next.Encode(NullUUID(value.(uuid.NullUUID)), buf)
}
// TryWrapUUIDScanPlan implements pgtype.TryWrapScanPlanFunc
func TryWrapUUIDScanPlan(target any) (plan pgtype.WrappedScanPlanNextSetter, nextDst any, 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
}
// SetNext implements pgtype.WrappedScanPlanNextSetter interface
func (plan *wrapUUIDScanPlan) SetNext(next pgtype.ScanPlan) { plan.next = next }
// Scan implements pgtype.ScanPlan interface
func (plan *wrapUUIDScanPlan) Scan(src []byte, dst any) error {
return plan.next.Scan(src, (*UUID)(dst.(*uuid.UUID)))
}
type wrapNullUUIDScanPlan struct {
next pgtype.ScanPlan
}
// SetNext implements pgtype.WrappedScanPlanNextSetter interface
func (plan *wrapNullUUIDScanPlan) SetNext(next pgtype.ScanPlan) { plan.next = next }
// Scan implements pgtype.ScanPlan interface
func (plan *wrapNullUUIDScanPlan) Scan(src []byte, dst any) error {
return plan.next.Scan(src, (*NullUUID)(dst.(*uuid.NullUUID)))
}
// UUIDCodec pgx type wrapper for pgtype.Codec
//
//revive:disable-next-line:exported
type UUIDCodec struct {
pgtype.UUIDCodec
}
// DecodeValue implements pgtype.Codec interface
func (UUIDCodec) DecodeValue(tm *pgtype.Map, oid uint32, format int16, src []byte) (any, 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/google/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{},
})
}