-
Notifications
You must be signed in to change notification settings - Fork 3
/
duration.go
77 lines (65 loc) · 1.49 KB
/
duration.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
package api
import (
"encoding/json"
"time"
)
func NewDuration(val string) Duration {
d := Duration{}
if err := d.Set(val); err != nil {
panic(err)
}
return d
}
// Duration is a wrapper around time.Duration which supports correct
// marshaling to YAML and JSON. In particular, it marshals into strings, which
// can be used as map keys in json.
type Duration struct {
time.Duration `protobuf:"varint,1,opt,name=duration,casttype=time.Duration"`
}
// UnmarshalJSON implements the json.Unmarshaller interface.
func (d *Duration) UnmarshalJSON(b []byte) error {
var str string
err := json.Unmarshal(b, &str)
if err != nil {
// try int
var pd int64
if err := json.Unmarshal(b, &pd); err != nil {
return err
}
d.Duration = time.Duration(pd)
return nil
}
pd, err := time.ParseDuration(str)
if err != nil {
return err
}
d.Duration = pd
return nil
}
// MarshalJSON implements the json.Marshaler interface.
func (d Duration) MarshalJSON() ([]byte, error) {
s := d.Duration.String()
// configer isZero checker, "0s" is not supported
if s == "0s" {
return json.Marshal(0)
}
return json.Marshal(s)
}
func (d Duration) String() string {
return d.Duration.String()
}
func (d *Duration) Set(val string) error {
v, err := time.ParseDuration(val)
*d = Duration{v}
return err
}
func (d *Duration) Type() string {
return "duration"
}
// IsZero returns true if the value is nil or time is zero.
func (d *Duration) IsZero() bool {
if d == nil {
return true
}
return d.Duration == 0
}