-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.go
102 lines (93 loc) · 2.77 KB
/
helpers.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
package apiary
import (
"database/sql"
"encoding/json"
"os"
"time"
)
// getEnv either returns the value of an environment variable or, if that
// environment variables does not exist, returns the fallback value provided.
func getEnv(key, fallback string) string {
value, exists := os.LookupEnv(key)
if !exists {
value = fallback
}
return value
}
// dateInRange takes in a string which should be parsed to a date. That date is
// then kept within the range of the min and max dates passed as arguments.
func dateInRange(d string, min, max time.Time) (time.Time, error) {
format := "2006-01-02"
date, err := time.Parse(format, d)
if err != nil {
return time.Time{}, err
}
if date.Before(min) {
date = min
} else if date.After(max) {
date = max
}
return date, nil
}
// NullInt64 embeds the sql.NullInt64 type, so that it can be extended
// to change the JSON marshaling.
type NullInt64 struct {
sql.NullInt64
}
// MarshalJSON marshals a null integer as `{"int": null}` rather than
// using an object inside the field.
// See https://stackoverflow.com/questions/33072172/how-can-i-work-with-sql-null-values-and-json-in-a-good-way
func (v NullInt64) MarshalJSON() ([]byte, error) {
if v.Valid {
return json.Marshal(v.Int64)
}
return json.Marshal(nil)
}
// UnmarshalJSON unmarshals a null integer represented in JSON as `{"int": null}`
// into our embedded type that allows nulls.
// See https://stackoverflow.com/questions/33072172/how-can-i-work-with-sql-null-values-and-json-in-a-good-way
func (v *NullInt64) UnmarshalJSON(data []byte) error {
// Unmarshalling into a pointer will let us detect null
var x *int64
if err := json.Unmarshal(data, &x); err != nil {
return err
}
if x != nil {
v.Valid = true
v.Int64 = *x
} else {
v.Valid = false
}
return nil
}
// NullString embeds the sql.NullString type, so that it can be extended
// to change the JSON marshaling.
type NullString struct {
sql.NullString
}
// MarshalJSON marshals a null string as `{"string": null}` rather than
// using an object inside the field.
// See https://stackoverflow.com/questions/33072172/how-can-i-work-with-sql-null-values-and-json-in-a-good-way
func (v NullString) MarshalJSON() ([]byte, error) {
if v.Valid {
return json.Marshal(v.String)
}
return json.Marshal(nil)
}
// UnmarshalJSON unmarshals a null string represented in JSON as `{"string": null}`
// into our embedded type that allows nulls.
// See https://stackoverflow.com/questions/33072172/how-can-i-work-with-sql-null-values-and-json-in-a-good-way
func (v *NullString) UnmarshalJSON(data []byte) error {
// Unmarshalling into a pointer will let us detect null
var x *string
if err := json.Unmarshal(data, &x); err != nil {
return err
}
if x != nil {
v.Valid = true
v.String = *x
} else {
v.Valid = false
}
return nil
}