|
@@ -7,7 +7,8 @@ import (
|
|
|
"github.com/pkg/errors"
|
|
"github.com/pkg/errors"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
-type duration struct {
|
|
|
|
|
|
|
+// Duration is a wrapper around Time.Duration to aid with marshal/unmarshal.
|
|
|
|
|
+type Duration struct {
|
|
|
time.Duration
|
|
time.Duration
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -16,8 +17,8 @@ type duration struct {
|
|
|
// A duration string is a possibly signed sequence of decimal numbers, each with
|
|
// A duration string is a possibly signed sequence of decimal numbers, each with
|
|
|
// optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m".
|
|
// optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m".
|
|
|
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
|
|
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
|
|
|
-func (d *duration) MarshalJSON() ([]byte, error) {
|
|
|
|
|
- return json.Marshal(d.String())
|
|
|
|
|
|
|
+func (d *Duration) MarshalJSON() ([]byte, error) {
|
|
|
|
|
+ return json.Marshal(d.Duration.String())
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON parses a duration string and sets it to the duration.
|
|
// UnmarshalJSON parses a duration string and sets it to the duration.
|
|
@@ -25,14 +26,21 @@ func (d *duration) MarshalJSON() ([]byte, error) {
|
|
|
// A duration string is a possibly signed sequence of decimal numbers, each with
|
|
// A duration string is a possibly signed sequence of decimal numbers, each with
|
|
|
// optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m".
|
|
// optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m".
|
|
|
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
|
|
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
|
|
|
-func (d *duration) UnmarshalJSON(data []byte) (err error) {
|
|
|
|
|
- var s string
|
|
|
|
|
|
|
+func (d *Duration) UnmarshalJSON(data []byte) (err error) {
|
|
|
|
|
+ var (
|
|
|
|
|
+ s string
|
|
|
|
|
+ _d time.Duration
|
|
|
|
|
+ )
|
|
|
|
|
+ if d == nil {
|
|
|
|
|
+ return errors.New("duration cannot be nil")
|
|
|
|
|
+ }
|
|
|
if err = json.Unmarshal(data, &s); err != nil {
|
|
if err = json.Unmarshal(data, &s); err != nil {
|
|
|
return errors.Wrapf(err, "error unmarshalling %s", data)
|
|
return errors.Wrapf(err, "error unmarshalling %s", data)
|
|
|
}
|
|
}
|
|
|
- if d.Duration, err = time.ParseDuration(s); err != nil {
|
|
|
|
|
|
|
+ if _d, err = time.ParseDuration(s); err != nil {
|
|
|
return errors.Wrapf(err, "error parsing %s as duration", s)
|
|
return errors.Wrapf(err, "error parsing %s as duration", s)
|
|
|
}
|
|
}
|
|
|
|
|
+ d.Duration = _d
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|