-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsonb.go
77 lines (67 loc) · 1.66 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
package storage
import (
"database/sql/driver"
"encoding/json"
"errors"
)
// JSONB represents a PostgreSQL's JSONB value:
// https://www.postgresql.org/docs/9.6/static/datatype-json.html. JSONB
// satisfies sqlbuilder.ScannerValuer.
type JSONB struct {
V interface{}
}
// MarshalJSON encodes the wrapper value as JSON.
func (j JSONB) MarshalJSON() ([]byte, error) {
return json.Marshal(j.V)
}
// UnmarshalJSON decodes the given JSON into the wrapped value.
func (j *JSONB) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
j.V = v
return nil
}
// Scan satisfies the sql.Scanner interface.
func (j *JSONB) Scan(src interface{}) error {
if src == nil {
j.V = nil
return nil
}
b, ok := src.([]byte)
if !ok {
return errors.New("Scan source was not []bytes")
}
if err := json.Unmarshal(b, &j.V); err != nil {
return err
}
return nil
}
// Value satisfies the driver.Valuer interface.
func (j JSONB) Value() (driver.Value, error) {
// See https://github.com/lib/pq/issues/528#issuecomment-257197239 on why are
// we returning string instead of []byte.
if j.V == nil {
return nil, nil
}
if v, ok := j.V.(json.RawMessage); ok {
return string(v), nil
}
b, err := json.Marshal(j.V)
if err != nil {
return nil, err
}
return string(b), nil
}
// JSONBValue takes an interface and provides a driver.Value that can be
// stored as a JSONB column.
func JSONBValue(i interface{}) (driver.Value, error) {
v := JSONB{i}
return v.Value()
}
// ScanJSONB decodes a JSON byte stream into the passed dst value.
func ScanJSONB(dst interface{}, src interface{}) error {
v := JSONB{dst}
return v.Scan(src)
}