-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.go
93 lines (68 loc) · 2 KB
/
filter.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
package find
import (
"sort"
"strings"
)
type FilterByfunc func(version PlatformVersion) bool
type Filter interface {
Apply(l VersionList) VersionList
}
type defaultFilter struct {
version string
bitness BitnessType
}
// VersionSorter sorts planets by axis.
type byVersion VersionList
func (a byVersion) Len() int { return len(a) }
func (a byVersion) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byVersion) Less(i, j int) bool { return a[i].Version() < a[j].Version() }
// byVersionAndBitness sorts planets by name.
type byVersionAndBitness VersionList
func (a byVersionAndBitness) Len() int { return len(a) }
func (a byVersionAndBitness) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byVersionAndBitness) Less(i, j int) bool {
if a[i].Version() > a[j].Version() {
return true
}
if a[i].Version() < a[j].Version() {
return false
}
return a[i].Bitness() < a[j].Bitness()
}
type byVersionAndBitnessReverse VersionList
func (a byVersionAndBitnessReverse) Len() int { return len(a) }
func (a byVersionAndBitnessReverse) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byVersionAndBitnessReverse) Less(i, j int) bool {
if a[i].Version() > a[j].Version() {
return true
}
if a[i].Version() < a[j].Version() {
return false
}
return a[i].Bitness() > a[j].Bitness()
}
func (f defaultFilter) Apply(l VersionList) (fl VersionList) {
fl = l.FilterBy(filterByVersion(f.version))
if f.bitness == V8_x32 || f.bitness == V8_x64 {
fl = fl.FilterBy(filterByBitness(f.bitness))
}
switch f.bitness {
case V8_x64, V8_x32:
sort.Sort(byVersion(fl))
case V8_x64x32:
sort.Sort(byVersionAndBitness(fl))
case V8_x32x64:
sort.Sort(byVersionAndBitnessReverse(fl))
}
return
}
func filterByVersion(versionMask string) FilterByfunc {
return func(version PlatformVersion) bool {
return strings.HasPrefix(version.Version(), versionMask)
}
}
func filterByBitness(b BitnessType) FilterByfunc {
return func(version PlatformVersion) bool {
return version.Bitness() == b
}
}