forked from go-zookeeper/zk
-
Notifications
You must be signed in to change notification settings - Fork 6
/
version.go
87 lines (76 loc) · 1.67 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
package zk
import (
"fmt"
"strings"
)
// ParseVersion parses a version string into a Version struct.
func ParseVersion(vs string) Version {
v, err := ParseVersionErr(vs)
if err != nil {
return Version{-1, -1, -1}
}
return v
}
// ParseVersionErr parses a version string into a Version struct; returns an error if the string is invalid.
func ParseVersionErr(vs string) (Version, error) {
var major, minor, patch int
var err error
switch strings.Count(vs, ".") {
case 2:
_, err = fmt.Sscanf(vs, "%d.%d.%d", &major, &minor, &patch)
case 1:
_, err = fmt.Sscanf(vs, "%d.%d", &major, &minor)
case 0:
_, err = fmt.Sscanf(vs, "%d", &major)
default:
err = fmt.Errorf("too many dots")
}
if err != nil {
return Version{}, fmt.Errorf("invalid version string: %v", err)
}
return Version{major, minor, patch}, nil
}
type Version struct {
Major int
Minor int
Patch int
}
func (v Version) String() string {
return fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Patch)
}
func (v Version) IsValid() bool {
return v.Major != -1
}
func (v Version) LessThan(other Version) bool {
if v.Major < other.Major {
return true
}
if v.Major > other.Major {
return false
}
if v.Minor < other.Minor {
return true
}
if v.Minor > other.Minor {
return false
}
return v.Patch < other.Patch
}
func (v Version) GreaterThan(other Version) bool {
if v.Major > other.Major {
return true
}
if v.Major < other.Major {
return false
}
if v.Minor > other.Minor {
return true
}
if v.Minor < other.Minor {
return false
}
return v.Patch > other.Patch
}
func (v Version) Equal(other Version) bool {
return v.Major == other.Major && v.Minor == other.Minor && v.Patch == other.Patch
}