-
Notifications
You must be signed in to change notification settings - Fork 3
/
comparison.go
225 lines (187 loc) · 7.43 KB
/
comparison.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package constraint
import (
"reflect"
"strings"
"time"
"github.com/jt0/gomer/flect"
"github.com/jt0/gomer/gomerr"
)
type ComparisonType = string
const (
EQ ComparisonType = "EQ"
NEQ = "NEQ"
GT = "GT"
GTE = "GTE"
LT = "LT"
LTE = "LTE"
)
func Gte(ft reflect.Type, compareTo *interface{}) {
}
// IntCompare compares the tested value to compareTo. While compareTo is an int64, the tested value can be any of the
// integer types (e.g. int, int16, etc). If the tested value is not an integer type, the constraint will fail.
func IntCompare(comparisonType ComparisonType, compareTo *int64) Constraint {
comparisonType = strings.ToUpper(comparisonType)
comparator, ok := intComparators[comparisonType]
if !ok {
panic("Unrecognized comparison type: " + comparisonType)
}
return New("Int"+comparisonType, compareTo, func(toTest interface{}) (ge gomerr.Gomerr) {
if compareTo == nil {
return nil
}
ttv, ok := flect.ReadableIndirectValue(toTest)
if !ok {
return nil // should be NotSatisfied?
}
defer func() {
if r := recover(); r != nil {
ge = gomerr.Unprocessable("toTest is not an int (or *int)", toTest)
}
}()
if !comparator(ttv.Int(), *compareTo) {
return NotSatisfied(toTest)
}
return nil
})
}
// IntBetween determines whether the provided value is (inclusively) between the lower and upper values provided.
// Stated explicitly, this tests for lower <= value <= upper.
func IntBetween(lower, upper *int64) Constraint {
c := And(IntCompare(GTE, lower), IntCompare(LTE, upper))
c.(*constraint).type_ = "IntBetween"
return c
}
var intComparators = map[ComparisonType]func(int64, int64) bool{
EQ: func(value, compareTo int64) bool { return value == compareTo },
NEQ: func(value, compareTo int64) bool { return value != compareTo },
GT: func(value, compareTo int64) bool { return value > compareTo },
GTE: func(value, compareTo int64) bool { return value >= compareTo },
LT: func(value, compareTo int64) bool { return value < compareTo },
LTE: func(value, compareTo int64) bool { return value <= compareTo },
}
// UintCompare compares a tested value to compareTo. While compareTo is an uint64, the tested value can be any of the
// unsigned integer types (e.g. uint, uint16, etc). If the tested value is not an unsigned integer type, the constraint
// will fail.
func UintCompare(comparisonType ComparisonType, compareTo *uint64) Constraint {
comparisonType = strings.ToUpper(comparisonType)
comparator, ok := uintComparators[comparisonType]
if !ok {
panic("Unrecognized comparison type: " + comparisonType)
}
return New("Uint"+comparisonType, compareTo, func(toTest interface{}) (ge gomerr.Gomerr) {
if compareTo == nil {
return nil
}
ttv, ok := flect.ReadableIndirectValue(toTest)
if !ok {
return nil // should be NotSatisfied?
}
defer func() {
if r := recover(); r != nil {
ge = gomerr.Unprocessable("toTest is not a uint (or *uint)", toTest)
}
}()
if !comparator(ttv.Uint(), *compareTo) {
return NotSatisfied(toTest)
}
return nil
})
}
// UintBetween determines whether the provided value is (inclusively) between the lower and upper values provided.
// Stated explicitly, this tests for lower <= value <= upper.
func UintBetween(lower, upper *uint64) Constraint {
c := And(UintCompare(GTE, lower), UintCompare(LTE, upper))
c.(*constraint).type_ = "UintBetween"
return c
}
var uintComparators = map[ComparisonType]func(uint64, uint64) bool{
EQ: func(value, compareTo uint64) bool { return value == compareTo },
NEQ: func(value, compareTo uint64) bool { return value != compareTo },
GT: func(value, compareTo uint64) bool { return value > compareTo },
GTE: func(value, compareTo uint64) bool { return value >= compareTo },
LT: func(value, compareTo uint64) bool { return value < compareTo },
LTE: func(value, compareTo uint64) bool { return value <= compareTo },
}
// FloatCompare compares a tested value to compareTo. While compareTo is an float64, the tested value can be either
// float32/float64. If the value is not a float type, the constraint will fail.
func FloatCompare(comparisonType ComparisonType, compareTo *float64) Constraint {
comparisonType = strings.ToUpper(comparisonType)
comparator, exists := floatComparators[comparisonType]
if !exists {
panic("Unrecognized comparison type: " + comparisonType)
}
return New("Float"+comparisonType, compareTo, func(toTest interface{}) (ge gomerr.Gomerr) {
if compareTo == nil {
return nil
}
ttv, ok := flect.ReadableIndirectValue(toTest)
if !ok {
return nil // should be NotSatisfied?
}
defer func() {
if r := recover(); r != nil {
ge = gomerr.Unprocessable("toTest is not a float (or *float)", toTest)
}
}()
if !comparator(ttv.Float(), *compareTo) {
return NotSatisfied(toTest)
}
return nil
})
}
// FloatBetween determines whether the provided value is (inclusively) between the lower and upper values provided.
// Stated explicitly, this tests for lower <= value <= upper.
func FloatBetween(lower, upper *float64) Constraint {
c := And(FloatCompare(GTE, lower), FloatCompare(LTE, upper))
c.(*constraint).type_ = "FloatBetween"
return c
}
var floatComparators = map[ComparisonType]func(float64, float64) bool{
EQ: func(value, compareTo float64) bool { return value == compareTo },
NEQ: func(value, compareTo float64) bool { return value != compareTo },
GT: func(value, compareTo float64) bool { return value > compareTo },
GTE: func(value, compareTo float64) bool { return value >= compareTo },
LT: func(value, compareTo float64) bool { return value < compareTo },
LTE: func(value, compareTo float64) bool { return value <= compareTo },
}
// TimeCompare compares a tested value to compareTo. If the tested value is not a time.Time, the constraint will fail.
func TimeCompare(comparisonType ComparisonType, compareTo *time.Time) Constraint {
comparisonType = strings.ToUpper(comparisonType)
comparator, ok := timeComparators[comparisonType]
if !ok {
panic("Unrecognized comparison type: " + comparisonType)
}
return New("Time"+comparisonType, compareTo, func(toTest interface{}) (ge gomerr.Gomerr) {
if compareTo == nil {
return nil
}
ttv, ok := flect.ReadableIndirectValue(toTest)
if !ok {
return nil // should be NotSatisfied?
}
defer func() {
if r := recover(); r != nil {
ge = gomerr.Unprocessable("toTest is not a time.Time (or *time.Time)", toTest)
}
}()
if !comparator(ttv.Interface().(time.Time), *compareTo) {
return NotSatisfied(toTest)
}
return nil
})
}
// TimeBetween determines whether the provided value is (inclusively) between the lower and upper values provided.
// Stated explicitly, this tests for lower <= value <= upper.
func TimeBetween(lower, upper *time.Time) Constraint {
c := And(TimeCompare(GTE, lower), TimeCompare(LTE, upper))
c.(*constraint).type_ = "TimeBetween"
return c
}
var timeComparators = map[ComparisonType]func(time.Time, time.Time) bool{
EQ: func(value, compareTo time.Time) bool { return value.Equal(compareTo) },
NEQ: func(value, compareTo time.Time) bool { return !value.Equal(compareTo) },
GT: func(value, compareTo time.Time) bool { return value.After(compareTo) },
GTE: func(value, compareTo time.Time) bool { return value.After(compareTo) || value.Equal(compareTo) },
LT: func(value, compareTo time.Time) bool { return value.Before(compareTo) },
LTE: func(value, compareTo time.Time) bool { return value.Before(compareTo) || value.Equal(compareTo) },
}