-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathversion.go
109 lines (98 loc) · 2.54 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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//
// DISCLAIMER
//
// Copyright 2017 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Ewout Prangsma
//
package driver
import (
"strconv"
"strings"
)
// Version holds a server version string. The string has the format "major.minor.sub".
// Major and minor will be numeric, and sub may contain a number or a textual version.
type Version string
// Major returns the major part of the version
// E.g. "3.1.7" -> 3
func (v Version) Major() int {
parts := strings.Split(string(v), ".")
result, _ := strconv.Atoi(parts[0])
return result
}
// Minor returns the minor part of the version.
// E.g. "3.1.7" -> 1
func (v Version) Minor() int {
parts := strings.Split(string(v), ".")
if len(parts) >= 2 {
result, _ := strconv.Atoi(parts[1])
return result
}
return 0
}
// Sub returns the sub part of the version.
// E.g. "3.1.7" -> "7"
func (v Version) Sub() string {
parts := strings.SplitN(string(v), ".", 3)
if len(parts) == 3 {
return parts[2]
}
return ""
}
// SubInt returns the sub part of the version as integer.
// The bool return value indicates if the sub part is indeed a number.
// E.g. "3.1.7" -> 7, true
// E.g. "3.1.foo" -> 0, false
func (v Version) SubInt() (int, bool) {
result, err := strconv.Atoi(v.Sub())
return result, err == nil
}
// CompareTo returns an integer comparing two version.
// The result will be 0 if v==other, -1 if v < other, and +1 if v > other.
// If major & minor parts are equal and sub part is not a number,
// the sub part will be compared using lexicographical string comparison.
func (v Version) CompareTo(other Version) int {
a := v.Major()
b := other.Major()
if a < b {
return -1
}
if a > b {
return 1
}
a = v.Minor()
b = other.Minor()
if a < b {
return -1
}
if a > b {
return 1
}
a, aIsInt := v.SubInt()
b, bIsInt := other.SubInt()
if !aIsInt || !bIsInt {
// Do a string comparison
return strings.Compare(v.Sub(), other.Sub())
}
if a < b {
return -1
}
if a > b {
return 1
}
return 0
}