-
Notifications
You must be signed in to change notification settings - Fork 2
/
version_semantic.go
60 lines (55 loc) · 1.5 KB
/
version_semantic.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
package gobinsec
import (
"fmt"
"strconv"
"strings"
)
// SemanticVersion is type to represent a semantic version
type SemanticVersion struct {
Text string
Fields [3]int
}
// NewSemanticVersion builds a version from string
func NewSemanticVersion(text string) (*SemanticVersion, error) {
version := SemanticVersion{Text: text}
parts := strings.TrimPrefix(text, "v")
parts = strings.TrimSuffix(parts, "+incompatible")
numbers := strings.Split(parts, ".")
if len(numbers) != 3 { // nolint:gomnd // 3 is magic
return nil, fmt.Errorf("wrong version parts count: %s", text)
}
var err error
for i := 0; i < 3; i++ {
version.Fields[i], err = strconv.Atoi(numbers[i])
if err != nil {
return nil, fmt.Errorf("wrong version number: %s", text)
}
if version.Fields[i] < 0 {
return nil, fmt.Errorf("negative version number: %s", text)
}
}
return &version, nil
}
// String returns string representation of version
func (version *SemanticVersion) String() string {
return version.Text
}
// Compare two semantic versions s and o:
// - if s < o : returns -1
// - if s > o : returns +1
// - if s = o : returns 0
func (version *SemanticVersion) Compare(other interface{}) (int, error) {
otherVersion, ok := other.(*SemanticVersion)
if !ok {
return 0, fmt.Errorf("can't compare semantic version to other type: %T", other)
}
for i := 0; i < 3; i++ {
if version.Fields[i] < otherVersion.Fields[i] {
return -1, nil
}
if version.Fields[i] > otherVersion.Fields[i] {
return 1, nil
}
}
return 0, nil
}