-
Notifications
You must be signed in to change notification settings - Fork 46
/
version.go
83 lines (71 loc) · 1.77 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
package core
import (
"encoding/binary"
"io"
"strconv"
"strings"
)
// DefaultVersion is default protocol version.
var DefaultVersion Version = [2]uint16{1, 0}
// Version define the version of protocol.
// It includes major and minor version.
type Version [2]uint16
// Bytes returns raw bytes of current version.
func (p Version) Bytes() []byte {
bs := make([]byte, 4)
binary.BigEndian.PutUint16(bs, p[0])
binary.BigEndian.PutUint16(bs[2:], p[1])
return bs
}
// Major returns major version.
func (p Version) Major() uint16 {
return p[0]
}
// Minor returns minor version.
func (p Version) Minor() uint16 {
return p[1]
}
// Equals returns true if versions are same.
func (p Version) Equals(version Version) bool {
return p.Major() == version.Major() && p.Minor() == version.Minor()
}
// GreaterThan returns true if current version is greater than target.
func (p Version) GreaterThan(version Version) bool {
if p[0] == version[0] {
return p[1] > version[1]
}
return p[0] > version[0]
}
// LessThan returns true if current version is less than target.
func (p Version) LessThan(version Version) bool {
if p[0] == version[0] {
return p[1] < version[1]
}
return p[0] < version[0]
}
// WriteTo write raw version bytes to a writer.
func (p Version) WriteTo(w io.Writer) (n int64, err error) {
err = binary.Write(w, binary.BigEndian, p[0])
if err != nil {
return
}
err = binary.Write(w, binary.BigEndian, p[1])
if err != nil {
return
}
n = 4
return
}
func (p Version) String() string {
b := strings.Builder{}
b.WriteString(strconv.Itoa(int(p[0])))
b.WriteByte('.')
b.WriteString(strconv.Itoa(int(p[1])))
return b.String()
}
// NewVersion creates a new Version from major and minor.
func NewVersion(major, minor uint16) Version {
return Version{
major, minor,
}
}