/
types.go
47 lines (39 loc) · 854 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 instagram
import (
"encoding/json"
"labix.org/v2/mgo/bson"
"log"
"strconv"
"time"
)
type Timestamp struct{ time.Time }
func (t *Timestamp) UnmarshalJSON(b []byte) error {
i, err := strconv.ParseInt(string(b[1:len(b)-1]), 10, 0)
if err != nil {
log.Println("Unable to parse timestamp from json")
}
*t = Timestamp{time.Unix(i, 0)}
return nil
}
func (t *Timestamp) MarshalJSON() ([]byte, error) {
return json.Marshal(t.Unix())
}
func (t *Timestamp) GetBSON() (interface{}, error) {
return t.Time, nil
}
func (t *Timestamp) SetBSON(raw bson.Raw) error {
tim := new(time.Time)
err := raw.Unmarshal(&tim)
if err != nil {
return err
}
*t = Timestamp{*tim}
return nil
}
type NString string
func (s *NString) UnmarshalJSON(b []byte) error {
if string(b) == "null" {
return nil
}
return json.Unmarshal(b, (*string)(s))
}