-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
compat.go
45 lines (41 loc) · 787 Bytes
/
compat.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
package compat
type v struct {
major uint16
minor uint8
patch uint8
}
// Returns <0 if "a < b"
// Returns 0 if "a == b"
// Returns >0 if "a > b"
func compareVersions(a v, b []int) int {
diff := int(a.major)
if len(b) > 0 {
diff -= b[0]
}
if diff == 0 {
diff = int(a.minor)
if len(b) > 1 {
diff -= b[1]
}
}
if diff == 0 {
diff = int(a.patch)
if len(b) > 2 {
diff -= b[2]
}
}
return diff
}
// The start is inclusive and the end is exclusive
type versionRange struct {
start v
end v // Use 0.0.0 for "no end"
}
func isVersionSupported(ranges []versionRange, version []int) bool {
for _, r := range ranges {
if compareVersions(r.start, version) <= 0 && (r.end == (v{}) || compareVersions(r.end, version) > 0) {
return true
}
}
return false
}