File: comparitors.go

package info (click to toggle)
golang-github-karlseguin-expect 1.0.1+git20160716.12.5c2eadb-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 172 kB
  • sloc: makefile: 6
file content (74 lines) | stat: -rw-r--r-- 1,905 bytes parent folder | download | duplicates (2)
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
package expect

import (
	"reflect"
	"time"
)

const (
	Equals comparitorType = iota
	NotEqual
	LessThan
	LessThanOrEqual
	GreaterThan
	GreaterThanOrEqual
)

type comparitorType int
type comparitor func(k reflect.Kind, a, b interface{}) bool
type resolver func(a, b interface{}) bool

var (
	Comparitors = map[reflect.Kind]map[comparitorType]resolver{
		reflect.Int64: map[comparitorType]resolver{
			LessThan: func(a, b interface{}) bool { return a.(int64) < b.(int64) },
		},
		reflect.Uint64: map[comparitorType]resolver{
			LessThan: func(a, b interface{}) bool { return a.(uint64) < b.(uint64) },
		},
		reflect.Float32: map[comparitorType]resolver{
			LessThan: func(a, b interface{}) bool { return a.(float32) < b.(float32) },
		},
		reflect.Float64: map[comparitorType]resolver{
			LessThan: func(a, b interface{}) bool { return a.(float64) < b.(float64) },
		},
	}
)

func EqualsComparitor(k reflect.Kind, a, b interface{}) bool {
	return reflect.DeepEqual(a, b)
}

func NotEqualsComparitor(k reflect.Kind, a, b interface{}) bool {
	return !EqualsComparitor(k, a, b)
}

func LessThanComparitor(k reflect.Kind, a, b interface{}) bool {
	if t, ok := a.(time.Time); ok {
		return t.Before(b.(time.Time))
	}
	return Comparitors[k][LessThan](a, b)
}

func LessThanOrEqualToComparitor(k reflect.Kind, a, b interface{}) bool {
	if t, ok := a.(time.Time); ok {
		bt := b.(time.Time)
		return t.Before(bt) || t == bt
	}
	return EqualsComparitor(k, a, b) || Comparitors[k][LessThan](a, b)
}

func GreaterThanComparitor(k reflect.Kind, a, b interface{}) bool {
	if t, ok := a.(time.Time); ok {
		return t.After(b.(time.Time))
	}
	return !EqualsComparitor(k, a, b) && !Comparitors[k][LessThan](a, b)
}

func GreaterOrEqualToComparitor(k reflect.Kind, a, b interface{}) bool {
	if t, ok := a.(time.Time); ok {
		bt := b.(time.Time)
		return t.After(bt) || t == bt
	}
	return !Comparitors[k][LessThan](a, b)
}