-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
38 lines (33 loc) · 981 Bytes
/
types.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
package matcher
import (
"errors"
"reflect"
"time"
)
type Comparator interface {
GreaterThan(interface{}, interface{}) bool
LessThan(interface{}, interface{}) bool
EqualTo(interface{}, interface{}) bool
NotEqualTo(interface{}, interface{}) bool
Valid(interface{}) error
}
func RegisterDefaults() {
Register(reflect.ValueOf("foo").Type(), &StringComparator{})
Register(reflect.ValueOf(3.14159).Type(), &FloatComparator{})
Register(reflect.ValueOf(1).Type(), &IntComparator{})
Register(reflect.ValueOf(time.Now()).Type(), &TimeComparator{})
}
func Compare(comparator Comparator, operator string, lh, rh interface{}) (bool, error) {
switch op := operator; op {
case "gt":
return comparator.GreaterThan(lh, rh), nil
case "lt":
return comparator.LessThan(lh, rh), nil
case "eq":
return comparator.EqualTo(lh, rh), nil
case "ne":
return comparator.NotEqualTo(lh, rh), nil
default:
return false, errors.New("Unknown comparison operator " + operator)
}
}