-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
51 lines (43 loc) · 988 Bytes
/
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
package common
import (
"errors"
"fmt"
"strconv"
"strings"
)
type Version uint64
func (v Version) Major() uint64 {
return uint64(v) / 1000000
}
func (v Version) Minor() uint64 {
uv := uint64(v)
return (uv - (uv/1000000)*1000000) / 1000
}
func (v Version) Patch() uint64 {
uv := uint64(v)
return uv - (uv/1000)*1000
}
func NewNodeVersion(major, minor, patch uint64) (Version, error) {
if minor > 999 || patch > 999 || major > 999 {
return 0, errors.New("out of range")
}
return Version(patch + minor*1000 + major*1000000), nil
}
func NewVersion(s string) (Version, error) {
ss := strings.Split(s, ".")
if len(ss) != 3 {
return 0, errors.New("invalid version string")
}
var ns []uint64
for _, s := range ss {
u, err := strconv.ParseUint(s, 10, 64)
if err != nil {
return 0, err
}
ns = append(ns, u)
}
return NewNodeVersion(ns[0], ns[1], ns[2])
}
func (v Version) String() string {
return fmt.Sprintf("%d.%d.%d", v.Major(), v.Minor(), v.Patch())
}