-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_duration_marshaler.go
53 lines (42 loc) · 1.22 KB
/
custom_duration_marshaler.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
package config
import (
"encoding/json"
"errors"
"fmt"
"time"
)
// customDuration's goals is to implement the unmarshalling
// of time.Duration through the string or float representation.
type customDuration time.Duration
// ToDuration converts the custom duration back to the real time.Duration.
func (cd *customDuration) ToDuration() time.Duration { return time.Duration(*cd) }
// ToInt64 converts the custom duration to the original value behind a time.Duration.
func (cd *customDuration) ToInt64() int64 { return int64(*cd) }
// MarshalJSON implements json Marshaler interface.
func (cd *customDuration) MarshalJSON() ([]byte, error) {
return json.Marshal((time.Duration(*cd)).String())
}
// UnmarshalJSON implements json Unmarshaler interface.
func (cd *customDuration) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return fmt.Errorf("json failed to unmarshal to v: %w", err)
}
var (
d time.Duration
err error
)
switch value := v.(type) {
case float64:
d = time.Duration(value)
case string:
d, err = time.ParseDuration(value)
default:
err = errors.New("invalid duration type")
}
if err != nil {
return err
}
*cd = customDuration(d)
return nil
}