-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
151 lines (126 loc) · 3.52 KB
/
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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package dat
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"errors"
"github.com/lib/pq"
)
// UnsafeString is interpolated as an unescaped and unquoted value and should
// only be used to create constants.
type UnsafeString string
// Value implements a valuer for compatibility
func (u UnsafeString) Value() (driver.Value, error) {
panic("UnsafeStrings and its constants NOW, DEFAULT ... are disabled when EnableInterpolation==false")
}
// DEFAULT SQL value
const DEFAULT = UnsafeString("DEFAULT")
// NOW SQL value
const NOW = UnsafeString("NOW()")
var timeFormat = "2006-01-02 15:04:05"
// NullString is a type that can be null or a string
type NullString struct {
sql.NullString
}
// NullFloat64 is a type that can be null or a float64
type NullFloat64 struct {
sql.NullFloat64
}
// NullInt64 is a type that can be null or an int
type NullInt64 struct {
sql.NullInt64
}
// NullTime is a type that can be null or a time
type NullTime struct {
pq.NullTime
}
// NullBool is a type that can be null or a bool
type NullBool struct {
sql.NullBool
}
var nullString = []byte("null")
// MarshalJSON correctly serializes a NullString to JSON
func (n *NullString) MarshalJSON() ([]byte, error) {
if n.Valid {
j, e := json.Marshal(n.String)
return j, e
}
return nullString, nil
}
// MarshalJSON correctly serializes a NullFloat64 to JSON
func (n *NullFloat64) MarshalJSON() ([]byte, error) {
if n.Valid {
j, e := json.Marshal(n.Float64)
return j, e
}
return nullString, nil
}
// MarshalJSON correctly serializes a NullInt64 to JSON
func (n *NullInt64) MarshalJSON() ([]byte, error) {
if n.Valid {
j, e := json.Marshal(n.Int64)
return j, e
}
return nullString, nil
}
// MarshalJSON correctly serializes a NullTime to JSON
func (n *NullTime) MarshalJSON() ([]byte, error) {
if n.Valid {
j, e := json.Marshal(n.Time)
return j, e
}
return nullString, nil
}
// MarshalJSON correctly serializes a NullBool to JSON
func (n *NullBool) MarshalJSON() ([]byte, error) {
if n.Valid {
j, e := json.Marshal(n.Bool)
return j, e
}
return nullString, nil
}
// JSONText is a json.RawMessage, which is a []byte underneath.
// Value() validates the json format in the source, and returns an error if
// the json is not valid. Scan does no validation. JSONText additionally
// implements `Unmarshal`, which unmarshals the json within to an interface{}
type JSONText json.RawMessage
// MarshalJSON returns the *j as the JSON encoding of j.
func (j *JSONText) MarshalJSON() ([]byte, error) {
return *j, nil
}
// UnmarshalJSON sets *j to a copy of data
func (j *JSONText) UnmarshalJSON(data []byte) error {
if j == nil {
return errors.New("JSONText: UnmarshalJSON on nil pointer")
}
*j = append((*j)[0:0], data...)
return nil
}
// Value returns j as a value. This does a validating unmarshal into another
// RawMessage. If j is invalid json, it returns an error.
func (j JSONText) Value() (driver.Value, error) {
var m json.RawMessage
var err = j.Unmarshal(&m)
if err != nil {
return []byte{}, err
}
return []byte(j), nil
}
// Scan stores the src in *j. No validation is done.
func (j *JSONText) Scan(src interface{}) error {
var source []byte
switch src.(type) {
case string:
source = []byte(src.(string))
case []byte:
source = src.([]byte)
default:
return errors.New("Incompatible type for JSONText")
}
*j = JSONText(append((*j)[0:0], source...))
return nil
}
// Unmarshal unmarshal's the json in j to v, as in json.Unmarshal.
func (j *JSONText) Unmarshal(v interface{}) error {
return json.Unmarshal([]byte(*j), v)
}