forked from Shyp/go-circle
/
types.go
49 lines (42 loc) · 840 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
48
49
package circle
// Circle has some types that we want to decode to normal Go types, define/do
// that here.
import (
"bytes"
"encoding/json"
"net/url"
"time"
)
// Unmarshallable URL
type URL struct {
*url.URL
}
func (oururl *URL) UnmarshalJSON(b []byte) error {
// extra hop here to strip the leading/trailing quotes
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
u, err := url.Parse(s)
if err != nil {
return err
}
*oururl = URL{u}
return nil
}
type CircleDuration time.Duration
var null = []byte("null")
func (cd *CircleDuration) UnmarshalJSON(b []byte) error {
if bytes.Equal(b, null) {
*cd = CircleDuration(time.Duration(-1))
return nil
}
var d time.Duration
err := json.Unmarshal(b, &d)
if err != nil {
return err
}
*cd = CircleDuration(d * time.Millisecond)
return nil
}