forked from owenthereal/ccat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
170 lines (152 loc) · 3.88 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package db_common
import (
"bytes"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"time"
)
// NullInt represents an int that may be null. NullInt implements the Scanner
// interface so it can be used as a scan destination, similar to NullString.
type NullInt struct {
Int int
Valid bool // Valid is true if Int is not NULL
}
// Scan implements the Scanner interface.
func (n *NullInt) Scan(value interface{}) error {
if value == nil {
n.Int, n.Valid = 0, false
return nil
}
switch value := value.(type) {
case int64:
n.Int = int(value)
default:
return fmt.Errorf("scanning %T, got %T", n, value)
}
return nil
}
// Value implements the driver Valuer interface.
func (n NullInt) Value() (driver.Value, error) {
if !n.Valid {
return nil, nil
}
return n.Int, nil
}
// MarshalJSON implements the encoding/json.Marshaler interface.
func (i NullInt) MarshalJSON() ([]byte, error) {
if i.Valid {
return json.Marshal(i.Int)
}
return json.Marshal(nil)
}
// UnmarshalJSON implements the encoding/json.Unmarshaler interface.
func (i *NullInt) UnmarshalJSON(data []byte) error {
var v interface{}
err := json.Unmarshal(data, &v)
if err != nil {
return err
}
if v == nil {
*i = NullInt{}
} else if v, ok := v.(float64); ok {
*i = NullInt{Valid: true, Int: int(v)}
} else {
return fmt.Errorf("unmarshaling %T, got %T", i, v)
}
return nil
}
type NullTime struct {
Time time.Time
Valid bool // Valid is true if Time is not NULL
}
// Scan implements the Scanner interface.
func (nt *NullTime) Scan(value interface{}) error {
nt.Time, nt.Valid = value.(time.Time)
return nil
}
// Value implements the driver Valuer interface.
func (nt NullTime) Value() (driver.Value, error) {
if !nt.Valid {
return nil, nil
}
return nt.Time, nil
}
// MarshalJSON implements the json.Marshaler interface.
func (nt NullTime) MarshalJSON() ([]byte, error) {
if nt.Valid {
return json.Marshal(nt.Time)
}
return []byte("null"), nil
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (nt *NullTime) UnmarshalJSON(data []byte) (err error) {
if nt == nil {
return errors.New("UnmarshalJSON on nil *NullTime pointer")
}
if bytes.Compare(data, []byte("null")) == 0 {
nt.Valid = false
} else {
nt.Valid = true
err = json.Unmarshal(data, &nt.Time)
}
return
}
func (nt NullTime) String() string {
if nt.Valid {
return nt.Time.String()
}
return "<nil>"
}
// Now returns a valid NullTime with the time set to now, in UTC and
// rounded to the nearest millisecond. It does this so that JSON- and
// SQL-serialization and deserialization yields the same time as passed
// in. If the time is not UTC or has sub-millisecond accuracy, the time
// retrieved from an SQL DB or JSON object might not be equal to the
// original object due to rounding and automatic timezone conversion.
func Now() NullTime {
return NullTime{Time: time.Now().In(time.UTC).Round(time.Millisecond), Valid: true}
}
type StringSlice struct {
Slice []string
}
func (s *StringSlice) Value() (driver.Value, error) {
if s == nil {
return nil, nil
}
inner := make([]string, len(s.Slice))
for i, elem := range s.Slice {
if strings.TrimSpace(elem) == "" || strings.Contains(elem, `"`) {
inner[i] = strconv.Quote(elem)
} else {
inner[i] = elem
}
}
return []byte("{" + strings.Join(inner, ",") + "}"), nil
}
func (s *StringSlice) Scan(v interface{}) error {
if data, ok := v.([]byte); ok {
interior := strings.Trim(string(data), "{}")
if interior != "" {
rawElems := strings.Split(interior, ",")
s.Slice = make([]string, len(rawElems))
for r, raw := range rawElems {
if elem, err := strconv.Unquote(raw); err == nil {
s.Slice[r] = elem
} else {
s.Slice[r] = raw
}
}
} else {
s.Slice = []string{}
}
return nil
}
return fmt.Errorf("%T.Scan failed: %v", s, v)
}
func NewSlice(goslice []string) *StringSlice {
return &StringSlice{Slice: goslice}
}