-
Notifications
You must be signed in to change notification settings - Fork 0
/
duration.go
134 lines (120 loc) · 3.46 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package check
import (
"fmt"
"time"
)
// Duration is the type of a check function which takes a time.Duration
// parameter and returns an error or nil if the check passes
type Duration func(d time.Duration) error
// DurationGT returns a function that will check that the value is
// greater than the limit
func DurationGT(limit time.Duration) Duration {
return func(d time.Duration) error {
if d > limit {
return nil
}
return fmt.Errorf("the value (%s) must be greater than %s", d, limit)
}
}
// DurationGE returns a function that will check that the value is
// greater than or equal to the limit
func DurationGE(limit time.Duration) Duration {
return func(d time.Duration) error {
if d >= limit {
return nil
}
return fmt.Errorf("the value (%s) must be greater than or equal to %s",
d, limit)
}
}
// DurationLT returns a function that will check that the value is
// less than the limit
func DurationLT(limit time.Duration) Duration {
return func(d time.Duration) error {
if d < limit {
return nil
}
return fmt.Errorf("the value (%s) must be less than %s", d, limit)
}
}
// DurationLE returns a function that will check that the value is
// less than or equal to the limit
func DurationLE(limit time.Duration) Duration {
return func(d time.Duration) error {
if d <= limit {
return nil
}
return fmt.Errorf("the value (%s) must be less than or equal to %s",
d, limit)
}
}
// DurationBetween returns a function that will check that the
// value lies between the upper and lower limits (inclusive)
func DurationBetween(low, high time.Duration) Duration {
if low >= high {
panic(fmt.Sprintf(
"Impossible checks passed to DurationBetween: "+
"the lower limit (%s) should be less than the upper limit (%s)",
low, high))
}
return func(d time.Duration) error {
if d < low {
return fmt.Errorf(
"the value (%s) must be between %s and %s - too short",
d, low, high)
}
if d > high {
return fmt.Errorf(
"the value (%s) must be between %s and %s - too long",
d, low, high)
}
return nil
}
}
// DurationOr returns a function that will check that the value, when
// passed to each of the check funcs in turn, passes at least one of them
func DurationOr(chkFuncs ...Duration) Duration {
return func(d time.Duration) error {
compositeErr := ""
sep := "("
for _, cf := range chkFuncs {
err := cf(d)
if err == nil {
return nil
}
compositeErr += sep + err.Error()
sep = _Or
}
return fmt.Errorf("%s)", compositeErr)
}
}
// DurationAnd returns a function that will check that the value, when
// passed to each of the check funcs in turn, passes all of them
func DurationAnd(chkFuncs ...Duration) Duration {
return func(d time.Duration) error {
for _, cf := range chkFuncs {
err := cf(d)
if err != nil {
return err
}
}
return nil
}
}
// DurationNot returns a function that will check that the value, when passed
// to the check func, does not pass it. You must also supply the error text
// to appear after the duration that fails. This error text should be a string
// that describes the quality that the duration should not have. So, for
// instance, if the function being Not'ed was
// check.DurationGE(5)
// then the errMsg parameter should be
// "a duration greater than or equal to 5".
func DurationNot(c Duration, errMsg string) Duration {
return func(d time.Duration) error {
err := c(d)
if err != nil {
return nil
}
return fmt.Errorf("'%s' should not be %s", d, errMsg)
}
}