-
Notifications
You must be signed in to change notification settings - Fork 0
/
float64.go
130 lines (116 loc) · 3.31 KB
/
float64.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
package check
import "fmt"
// Float64 is the type of a check function for a float64 value. It takes a
// float64 value and returns an error or nil if the check passes
type Float64 func(f float64) error
// Float64GT returns a function that will check that the value is greater
// than the limit
func Float64GT(limit float64) Float64 {
return func(f float64) error {
if f > limit {
return nil
}
return fmt.Errorf("the value (%g) must be greater than %g", f, limit)
}
}
// Float64GE returns a function that will check that the value is greater
// than or equal to the limit
func Float64GE(limit float64) Float64 {
return func(f float64) error {
if f >= limit {
return nil
}
return fmt.Errorf(
"the value (%g) must be greater than or equal to %g", f, limit)
}
}
// Float64LT returns a function that will check that the value is less than
// the limit
func Float64LT(limit float64) Float64 {
return func(f float64) error {
if f < limit {
return nil
}
return fmt.Errorf("the value (%g) must be less than %g", f, limit)
}
}
// Float64LE returns a function that will check that the value is less than
// or equal to the limit
func Float64LE(limit float64) Float64 {
return func(f float64) error {
if f <= limit {
return nil
}
return fmt.Errorf(
"the value (%g) must be less than or equal to %g", f, limit)
}
}
// Float64Between returns a function that will check that the value lies
// between the upper and lower limits (inclusive)
func Float64Between(low, high float64) Float64 {
if low >= high {
panic(fmt.Sprintf("Impossible checks passed to Float64Between:"+
" the lower limit (%g) should be less than the upper limit (%g)",
low, high))
}
return func(f float64) error {
if f < low {
return fmt.Errorf(
"the value (%g) must be between %g and %g - too small",
f, low, high)
}
if f > high {
return fmt.Errorf(
"the value (%g) must be between %g and %g - too big",
f, low, high)
}
return nil
}
}
// Float64Or 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 Float64Or(chkFuncs ...Float64) Float64 {
return func(f float64) error {
compositeErr := ""
sep := "("
for _, cf := range chkFuncs {
err := cf(f)
if err == nil {
return nil
}
compositeErr += sep + err.Error()
sep = _Or
}
return fmt.Errorf("%s)", compositeErr)
}
}
// Float64And returns a function that will check that the value, when
// passed to each of the check funcs in turn, passes all of them
func Float64And(chkFuncs ...Float64) Float64 {
return func(f float64) error {
for _, cf := range chkFuncs {
err := cf(f)
if err != nil {
return err
}
}
return nil
}
}
// Float64Not 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 value that fails. This error text should be a string
// that describes the quality that the value should not have. So, for
// instance, if the function being Not'ed was
// check.Float64GT(5.0)
// then the errMsg parameter should be
// "greater than 5.0".
func Float64Not(c Float64, errMsg string) Float64 {
return func(v float64) error {
err := c(v)
if err != nil {
return nil
}
return fmt.Errorf("%g should not be %s", v, errMsg)
}
}