forked from jackc/pgtype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonb.go
86 lines (65 loc) · 1.7 KB
/
jsonb.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
package pgtype
import (
"database/sql/driver"
errors "golang.org/x/xerrors"
)
type JSONB JSON
func (dst *JSONB) Set(src interface{}) error {
return (*JSON)(dst).Set(src)
}
func (dst JSONB) Get() interface{} {
return (JSON)(dst).Get()
}
func (src *JSONB) AssignTo(dst interface{}) error {
return (*JSON)(src).AssignTo(dst)
}
func (JSONB) PreferredResultFormat() int16 {
return TextFormatCode
}
func (dst *JSONB) DecodeText(ci *ConnInfo, src []byte) error {
return (*JSON)(dst).DecodeText(ci, src)
}
func (dst *JSONB) DecodeBinary(ci *ConnInfo, src []byte) error {
if src == nil {
*dst = JSONB{Status: Null}
return nil
}
if len(src) == 0 {
return errors.Errorf("jsonb too short")
}
if src[0] != 1 {
return errors.Errorf("unknown jsonb version number %d", src[0])
}
*dst = JSONB{Bytes: src[1:], Status: Present}
return nil
}
func (JSONB) PreferredParamFormat() int16 {
return TextFormatCode
}
func (src JSONB) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
return (JSON)(src).EncodeText(ci, buf)
}
func (src JSONB) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
switch src.Status {
case Null:
return nil, nil
case Undefined:
return nil, errUndefined
}
buf = append(buf, 1)
return append(buf, src.Bytes...), nil
}
// Scan implements the database/sql Scanner interface.
func (dst *JSONB) Scan(src interface{}) error {
return (*JSON)(dst).Scan(src)
}
// Value implements the database/sql/driver Valuer interface.
func (src JSONB) Value() (driver.Value, error) {
return (JSON)(src).Value()
}
func (src JSONB) MarshalJSON() ([]byte, error) {
return (JSON)(src).MarshalJSON()
}
func (dst *JSONB) UnmarshalJSON(b []byte) error {
return (*JSON)(dst).UnmarshalJSON(b)
}