-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduration.go
35 lines (31 loc) · 829 Bytes
/
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
package marshallable
import (
"encoding/json"
"fmt"
"math"
"time"
)
// Duration is a time.Duration wraper that marshals to and from a JSON string.
type Duration struct{ time.Duration }
// MarshalJSON implements the json.Marshaler interface.
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.Duration.String())
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (d *Duration) UnmarshalJSON(b []byte) (err error) {
var v interface{}
if err = json.Unmarshal(b, &v); err != nil {
return
}
switch value := v.(type) {
case float64:
d.Duration = time.Duration(math.Abs(value))
return
case string:
d.Duration, err = time.ParseDuration(value) // todo: extend to support other formats: day and week
return
default:
err = fmt.Errorf("invalid duration: %v", b)
return
}
}