-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathversion.go
More file actions
220 lines (193 loc) · 5.19 KB
/
version.go
File metadata and controls
220 lines (193 loc) · 5.19 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package semver
import (
"fmt"
"slices"
"strconv"
"strings"
)
// VersionList keeps a list of versions and provides helper functions on them.
type VersionList []Version
// StringList converts all Versions into strings and returns them.
func (l VersionList) StringList() []string {
vs := make([]string, len(l))
for i := range l {
vs[i] = l[i].String()
}
return vs
}
// Prints the VersionList as comma and space ", " separated list.
func (l VersionList) String() string {
return strings.Join(l.StringList(), ", ")
}
// Version represents a Semantic Versioning 2.0.0 Version.
type Version struct {
Major, Minor, Patch uint64
PreRelease PreReleaseIdentifierList
BuildMetadata []string
}
// Same returns true if both Versions are the same.
func (v *Version) Same(o Version) bool {
return v.Major == o.Major &&
v.Minor == o.Minor &&
v.Patch == o.Patch &&
slices.Equal(v.PreRelease, o.PreRelease) &&
slices.Equal(v.BuildMetadata, o.BuildMetadata)
}
// String returns a string representation of the Version.
func (v *Version) String() string {
s := fmt.Sprintf("%s.%s.%s",
printXonMaxInt(v.Major),
printXonMaxInt(v.Minor),
printXonMaxInt(v.Patch))
if len(v.PreRelease) > 0 {
s += "-" + v.PreRelease.String()
}
if len(v.BuildMetadata) > 0 {
s += "+" + strings.Join(v.BuildMetadata, ".")
}
return s
}
func printXonMaxInt(d uint64) string {
if d == maxUint64 {
return "x"
}
return strconv.FormatUint(d, 10)
}
// Equal tests if both version are equal.
func (v *Version) Equal(o Version) bool {
return v.Compare(o) == 0
}
// LessThan tests if one version is less than another one.
func (v *Version) LessThan(o Version) bool {
return v.Compare(o) < 0
}
// GreaterThan tests if one version is greater than another one.
func (v *Version) GreaterThan(o Version) bool {
return v.Compare(o) > 0
}
// Compare compares this version to another one. It returns -1, 0, or 1 if
// the version smaller, equal, or larger than the other version.
func (v *Version) Compare(o Version) int {
if d := compareSegment(v.Major, o.Major); d != 0 {
return d
}
if d := compareSegment(v.Minor, o.Minor); d != 0 {
return d
}
if d := compareSegment(v.Patch, o.Patch); d != 0 {
return d
}
return o.PreRelease.Compare(v.PreRelease)
}
// PreReleaseIdentifierList is a list of PreReleaseIdentifiers.
// A pre-release version MAY be denoted by appending a hyphen and
// a series of dot separated identifiers immediately following the patch version.
// Examples: 1.0.0-alpha, 1.0.0-alpha.1, 1.0.0-0.3.7, 1.0.0-x.7.z.92, 1.0.0-x-y-z.--.
type PreReleaseIdentifierList []PreReleaseIdentifier
// String returns the pre-release identifier list as a single string joined by '.'.
func (l PreReleaseIdentifierList) String() string {
pre := make([]string, len(l))
for i := range l {
pre[i] = l[i].String()
}
return strings.Join(pre, ".")
}
// Compare compares this pre release identifier list to another one.
// It returns -1, 0, or 1 if the version smaller, equal, or larger than the other list.
func (l PreReleaseIdentifierList) Compare(o []PreReleaseIdentifier) int {
preLen := len(l)
otherLen := len(o)
if preLen == 0 && otherLen == 0 {
return 0
}
if preLen == 0 {
return -1
}
if otherLen == 0 {
return 1
}
prel := preLen
if otherLen > preLen {
prel = otherLen
}
for i := range prel {
var (
pre PreReleaseIdentifier
other PreReleaseIdentifier
)
if i < preLen {
pre = l[i]
}
if i < otherLen {
other = o[i]
}
if d := pre.Compare(other); d != 0 {
return d
}
}
return 0
}
// PreReleaseIdentifier can be alphanumeric or a number.
type PreReleaseIdentifier struct {
str string
num uint64
}
// Compare compares this pre release identifier to another one.
// It returns -1, 0, or 1 if the version smaller, equal, or larger than the other identifier.
func (s *PreReleaseIdentifier) Compare(o PreReleaseIdentifier) int {
aNum, isANum := s.GetNumber()
bNum, isBNum := o.GetNumber()
switch {
case !isANum && !isBNum:
if s.str == o.str {
return 0
}
if s.str > o.str {
return -1
}
return 1
case !isANum:
// Numeric identifiers always have lower precedence
// than non-numeric identifiers.
return -1
case !isBNum:
return 1
}
if aNum == bNum {
return 0
}
if aNum > bNum {
return -1
}
return 1
}
// Interface returns either a string or uint64 depending on the underlying type.
func (s *PreReleaseIdentifier) Interface() any {
if len(s.str) > 0 {
return s.str
}
return s.num
}
// GetString returns a string and whether the underlying type is a string.
func (s *PreReleaseIdentifier) GetString() (string, bool) {
return s.str, len(s.str) > 0
}
// GetNumber returns a uint64 and whether the underlying type is a uint64.
func (s *PreReleaseIdentifier) GetNumber() (uint64, bool) {
return s.num, len(s.str) == 0
}
// String always returns a string representation.
func (s *PreReleaseIdentifier) String() string {
if len(s.str) > 0 {
return s.str
}
return strconv.FormatUint(s.num, 10)
}
// ToPreReleaseIdentifier converts the given string into a PreReleaseIdentifier.
func ToPreReleaseIdentifier(s string) PreReleaseIdentifier {
num, err := strconv.ParseUint(s, 10, 0)
if err != nil {
return PreReleaseIdentifier{str: s}
}
return PreReleaseIdentifier{num: num}
}