-
Notifications
You must be signed in to change notification settings - Fork 0
/
duration.go
44 lines (35 loc) · 996 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
36
37
38
39
40
41
42
43
44
package apis
import (
"encoding/json"
"fmt"
"time"
)
// Duration extends time.Duration with additional methods for (de-)serialization to/from
// JSON, allowing it to be embedded in custom API objects.
type Duration time.Duration
var (
_ fmt.Stringer = (*Duration)(nil)
_ json.Marshaler = (*Duration)(nil)
_ json.Unmarshaler = (*Duration)(nil)
)
// String implements the fmt.Stringer interface.
func (d Duration) String() string {
return time.Duration(d).String()
}
// UnmarshalJSON implements json.Unmarshaler.
func (d *Duration) UnmarshalJSON(data []byte) error {
var dataStr string
if err := json.Unmarshal(data, &dataStr); err != nil {
return err
}
dur, err := time.ParseDuration(dataStr)
if err != nil {
return fmt.Errorf("failed to parse duration %q: %w", dataStr, err)
}
*d = Duration(dur)
return nil
}
// MarshalJSON implements json.Marshaler.
func (d Duration) MarshalJSON() ([]byte, error) {
return []byte(`"` + time.Duration(d).String() + `"`), nil
}