forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
142 lines (125 loc) · 2.22 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package version
import (
"errors"
"fmt"
)
const (
defaultAppSeparator = "/"
defaultVersionSeparator = "."
)
var (
errDifferentApps = errors.New("different applications")
errDifferentMajor = errors.New("different major version")
errDifferentMinor = errors.New("different minor version")
)
// Version defines what is needed to describe a version
type Version interface {
fmt.Stringer
App() string
Major() int
Minor() int
Patch() int
Compatible(Version) error
Before(Version) bool
}
type version struct {
app string
major int
minor int
patch int
str string
}
// NewDefaultVersion returns a new version with default separators
func NewDefaultVersion(
app string,
major int,
minor int,
patch int,
) Version {
return NewVersion(
app,
defaultAppSeparator,
defaultVersionSeparator,
major,
minor,
patch,
)
}
// NewVersion returns a new version
func NewVersion(
app string,
appSeparator string,
versionSeparator string,
major int,
minor int,
patch int,
) Version {
return &version{
app: app,
major: major,
minor: minor,
patch: patch,
str: fmt.Sprintf("%s%s%d%s%d%s%d",
app,
appSeparator,
major,
versionSeparator,
minor,
versionSeparator,
patch,
),
}
}
func (v *version) App() string { return v.app }
func (v *version) Major() int { return v.major }
func (v *version) Minor() int { return v.minor }
func (v *version) Patch() int { return v.patch }
func (v *version) String() string { return v.str }
func (v *version) Compatible(o Version) error {
switch {
case v.App() != o.App():
return errDifferentApps
case v.Major() != o.Major():
return errDifferentMajor
case v.Minor() != o.Minor():
return errDifferentMinor
default:
return nil
}
}
func (v *version) Before(o Version) bool {
if v.App() != o.App() {
return false
}
{
v := v.Major()
o := o.Major()
switch {
case v < o:
return true
case v > o:
return false
}
}
{
v := v.Minor()
o := o.Minor()
switch {
case v < o:
return true
case v > o:
return false
}
}
{
v := v.Patch()
o := o.Patch()
switch {
case v < o:
return true
}
}
return false
}