-
Notifications
You must be signed in to change notification settings - Fork 0
/
customDuration.go
64 lines (53 loc) · 1.4 KB
/
customDuration.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
// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package webhook
import (
"bytes"
"strconv"
"strings"
"time"
)
type InvalidDurationError struct {
Value string
}
func (ide *InvalidDurationError) Error() string {
var o strings.Builder
o.WriteString("duration must be of type int or string (example:'5m'); Invalid value: ")
o.WriteString(ide.Value)
return o.String()
}
// CustomDuration is a custom type for time.Duration that allows for
// unmarshaling from a string or int. If unmarshaling from a string,
// the string must be parsable by time.ParseDuration. If unmarshaling
// from an int, the int is assumed to be in seconds. Wes
type CustomDuration time.Duration
func (cd CustomDuration) String() string {
return time.Duration(cd).String()
}
func (cd CustomDuration) MarshalJSON() ([]byte, error) {
d := bytes.NewBuffer(nil)
d.WriteByte('"')
d.WriteString(cd.String())
d.WriteByte('"')
return d.Bytes(), nil
}
func (cd *CustomDuration) UnmarshalJSON(b []byte) (err error) {
if b[0] == '"' {
var d time.Duration
d, err = time.ParseDuration(string(b[1 : len(b)-1]))
if err == nil {
*cd = CustomDuration(d)
return
}
}
var d int64
d, err = strconv.ParseInt(string(b), 10, 64)
if err == nil {
*cd = CustomDuration(time.Duration(d) * time.Second)
return
}
err = &InvalidDurationError{
Value: string(b),
}
return
}