/
types.go
47 lines (39 loc) · 826 Bytes
/
types.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
package dbx
import (
"database/sql"
"encoding/json"
"github.com/lib/pq"
)
// NullInt representa a nullable integer
type NullInt struct {
sql.NullInt64
}
// MarshalJSON interface redefinition
func (r NullInt) MarshalJSON() ([]byte, error) {
if r.Valid {
return json.Marshal(r.Int64)
}
return json.Marshal(nil)
}
// NullString representa a nullable string
type NullString struct {
sql.NullString
}
// MarshalJSON interface redefinition
func (r NullString) MarshalJSON() ([]byte, error) {
if r.Valid {
return json.Marshal(r.String)
}
return json.Marshal(nil)
}
// NullTime representa a nullable time.Time
type NullTime struct {
pq.NullTime
}
// MarshalJSON interface redefinition
func (r NullTime) MarshalJSON() ([]byte, error) {
if r.Valid {
return json.Marshal(r.Time)
}
return json.Marshal(nil)
}