-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathhelpers.go
More file actions
45 lines (36 loc) · 717 Bytes
/
helpers.go
File metadata and controls
45 lines (36 loc) · 717 Bytes
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
package pushover
import (
"encoding/json"
"fmt"
"time"
)
// Helper to unmarshal a timestamp as string to a time.Time.
type timestamp struct{ *time.Time }
func (t *timestamp) UnmarshalJSON(data []byte) error {
var i int64
if err := json.Unmarshal(data, &i); err != nil {
return err
}
if i > 0 {
unixTime := time.Unix(i, 0)
*t = timestamp{&unixTime}
}
return nil
}
// Helper to unmarshal a int as a boolean.
type intBool bool
func (i *intBool) UnmarshalJSON(data []byte) error {
var v int64
if err := json.Unmarshal(data, &v); err != nil {
return err
}
switch v {
case 0:
*i = false
case 1:
*i = true
default:
return fmt.Errorf("failed to unmarshal int to bool")
}
return nil
}