-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding.go
97 lines (76 loc) · 1.91 KB
/
encoding.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
package typeid
import (
"errors"
"fmt"
"github.com/gofrs/uuid/v5"
"github.com/jackc/pgx/v5/pgtype"
)
// idImplementation is a helper constraint asserting the existence of the typeID methods on the type.
type idImplementation[P Prefix] interface {
instance[P]
String() string
UUID() uuid.UUID
}
func marshalText[T idImplementation[P], P Prefix](id T) ([]byte, error) {
return []byte(id.String()), nil
}
func unmarshalText[T idImplementation[P], P Prefix](dst *T, text []byte) error {
var err error
*dst, err = FromString[T](string(text))
if err != nil {
return fmt.Errorf("unmarshal text to typeid.TypeID: %w", err)
}
return nil
}
func value[T idImplementation[P], P Prefix](id T) (string, error) {
return id.String(), nil
}
func scan[T idImplementation[P], P Prefix](dst *T, src any) error {
var err error
s, ok := src.(string)
if !ok {
return fmt.Errorf("scan typeid.Typeid: espected string, got %T", src)
}
*dst, err = FromString[T](s)
if err != nil {
return fmt.Errorf("scan typeid.TypeID: %w", err)
}
return nil
}
var (
errNilScan = errors.New("cannot scan NULL into *typeid.TypeID")
)
func textValue[T idImplementation[P], P Prefix](id T) (pgtype.Text, error) {
return pgtype.Text{
String: id.String(),
Valid: true,
}, nil
}
func scanText[T idImplementation[P], P Prefix](dst *T, v pgtype.Text) error {
var err error
if !v.Valid {
return errNilScan
}
*dst, err = FromString[T](v.String)
if err != nil {
return fmt.Errorf("scan text to typeid.TypeID: %w", err)
}
return nil
}
func uuidValue[T idImplementation[P], P Prefix](id T) (pgtype.UUID, error) {
return pgtype.UUID{
Bytes: id.UUID(),
Valid: true,
}, nil
}
func scanUUID[T idImplementation[P], P Prefix](dst *T, v pgtype.UUID) error {
var err error
if !v.Valid {
return errNilScan
}
*dst, err = FromUUIDBytes[T](v.Bytes[:])
if err != nil {
return fmt.Errorf("scan UUID to typeid.TypeID: %w", err)
}
return nil
}