/
comparabletypes.go
61 lines (49 loc) · 1.38 KB
/
comparabletypes.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
package dataselector
import (
"strings"
"time"
)
type StdComparableInt int
func (self StdComparableInt) Compare(otherV ComparableValue) int {
other := otherV.(StdComparableInt)
return intsCompare(int(self), int(other))
}
func (self StdComparableInt) Contains(otherV ComparableValue) bool {
return self.Compare(otherV) == 0
}
type StdComparableString string
func (self StdComparableString) Compare(otherV ComparableValue) int {
other := otherV.(StdComparableString)
return strings.Compare(string(self), string(other))
}
func (self StdComparableString) Contains(otherV ComparableValue) bool {
other := otherV.(StdComparableString)
return strings.Contains(string(self), string(other))
}
type StdComparableTime time.Time
func (self StdComparableTime) Compare(otherV ComparableValue) int {
other := otherV.(StdComparableTime)
return ints64Compare(time.Time(self).Unix(), time.Time(other).Unix())
}
func (self StdComparableTime) Contains(otherV ComparableValue) bool {
return self.Compare(otherV) == 0
}
// Compares self with other value. Returns 1 if other value is smaller,
// 0 if they are the same, -1 if other is larger.
func ints64Compare(a, b int64) int {
if a > b {
return 1
} else if a == b {
return 0
}
return -1
}
// Int comparison functions. Similar to strings.Compare.
func intsCompare(a, b int) int {
if a > b {
return 1
} else if a == b {
return 0
}
return -1
}