forked from openp2p-cn/service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
57 lines (49 loc) · 1.21 KB
/
version.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
package service
import (
"errors"
"strconv"
"strings"
)
// versionAtMost will return true if the provided version is less than or equal to max
func versionAtMost(version, max []int) (bool, error) {
if comp, err := versionCompare(version, max); err != nil {
return false, err
} else if comp == 1 {
return false, nil
}
return true, nil
}
// versionCompare take to versions split into integer arrays and attempts to compare them
// An error will be returned if there is an array length mismatch.
// Return values are as follows
// -1 - v1 is less than v2
// 0 - v1 is equal to v2
// 1 - v1 is greater than v2
func versionCompare(v1, v2 []int) (int, error) {
if len(v1) != len(v2) {
return 0, errors.New("version length mismatch")
}
for idx, v2S := range v2 {
v1S := v1[idx]
if v1S > v2S {
return 1, nil
}
if v1S < v2S {
return -1, nil
}
}
return 0, nil
}
// parseVersion will parse any integer type version separated by periods.
// This does not fully support semver style versions.
func parseVersion(v string) []int {
version := make([]int, 3)
for idx, vStr := range strings.Split(v, ".") {
vS, err := strconv.Atoi(vStr)
if err != nil {
return nil
}
version[idx] = vS
}
return version
}