-
Notifications
You must be signed in to change notification settings - Fork 22
/
bytea.go
101 lines (87 loc) · 1.67 KB
/
bytea.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
//nolint:revive
package schema
import (
"bytes"
"encoding/hex"
"fmt"
)
type ByteaTransformer interface {
TransformBytea(*Bytea) any
}
type Bytea struct {
Bytes []byte
Status Status
}
func (*Bytea) Type() ValueType {
return TypeByteArray
}
func (dst *Bytea) Size() int {
return len(dst.Bytes)
}
func (dst *Bytea) GetStatus() Status {
return dst.Status
}
func (dst *Bytea) Equal(src CQType) bool {
if src == nil {
return false
}
s, ok := src.(*Bytea)
if !ok {
return false
}
return dst.Status == s.Status && bytes.Equal(dst.Bytes, s.Bytes)
}
func (dst *Bytea) String() string {
if dst.Status == Present {
return hex.EncodeToString(dst.Bytes)
} else {
return ""
}
}
func (dst *Bytea) Set(src any) error {
if src == nil {
*dst = Bytea{Status: Null}
return nil
}
if value, ok := src.(interface{ Get() any }); ok {
value2 := value.Get()
if value2 != value {
return dst.Set(value2)
}
}
switch value := src.(type) {
case []byte:
if value != nil {
*dst = Bytea{Bytes: value, Status: Present}
} else {
*dst = Bytea{Status: Null}
}
case string:
if value != "" {
b := make([]byte, hex.DecodedLen(len(value)))
_, err := hex.Decode(b, []byte(value))
if err != nil {
return fmt.Errorf("cannot decode hex string to bytea: %w", err)
}
*dst = Bytea{Status: Present, Bytes: b}
} else {
*dst = Bytea{Status: Null}
}
default:
if originalSrc, ok := underlyingBytesType(src); ok {
return dst.Set(originalSrc)
}
return fmt.Errorf("cannot convert %v to Bytea", value)
}
return nil
}
func (dst Bytea) Get() any {
switch dst.Status {
case Present:
return dst.Bytes
case Null:
return nil
default:
return dst.Status
}
}