-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjson.go
40 lines (36 loc) · 968 Bytes
/
json.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
package pqtype
import (
"database/sql/driver"
"encoding/json"
"fmt"
)
// NullRawMessage represents a json.RawMessage that may be null.
// NullRawMessage implements the Scanner interface so
// it can be used as a scan destination, similar to NullString.
type NullRawMessage struct {
RawMessage json.RawMessage
Valid bool // Valid is true if RawMessage is not NULL
}
// Scan implements the Scanner interface.
func (n *NullRawMessage) Scan(src interface{}) error {
if src == nil {
n.Valid = false
return nil
}
switch src := src.(type) {
case []byte:
srcCopy := make([]byte, len(src))
copy(srcCopy, src)
n.RawMessage, n.Valid = srcCopy, true
default:
return fmt.Errorf("unsupported Scan, storing driver.Value type %T into type %T", src, []byte{})
}
return nil
}
// Value implements the driver Valuer interface.
func (n NullRawMessage) Value() (driver.Value, error) {
if !n.Valid {
return nil, nil
}
return []byte(n.RawMessage), nil
}