Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 783651e

Browse files
committed
Merge commit '27bd351d1aa92fc65fb3a8c31b06ea4d5cefb3a8' into go-autoupdate-packages
2 parents 1fa6055 + 27bd351 commit 783651e

File tree

8 files changed

+97
-74
lines changed

8 files changed

+97
-74
lines changed

src/github.com/blang/semver/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ Note: Always vendor your dependencies or fix on a specific version tag.
1212

1313
```go
1414
import github.com/blang/semver
15-
v1, err := semver.New("1.0.0-beta")
16-
v2, err := semver.New("2.0.0-beta")
15+
v1, err := semver.Make("1.0.0-beta")
16+
v2, err := semver.Make("2.0.0-beta")
1717
v1.Compare(v2)
1818
```
1919

@@ -53,7 +53,7 @@ Have a look at full examples in [examples/main.go](examples/main.go)
5353
```go
5454
import github.com/blang/semver
5555

56-
v, err := semver.New("0.0.1-alpha.preview+123.github")
56+
v, err := semver.Make("0.0.1-alpha.preview+123.github")
5757
fmt.Printf("Major: %d\n", v.Major)
5858
fmt.Printf("Minor: %d\n", v.Minor)
5959
fmt.Printf("Patch: %d\n", v.Patch)
@@ -76,7 +76,7 @@ if len(v.Build) > 0 {
7676
}
7777
}
7878

79-
v001, err := semver.New("0.0.1")
79+
v001, err := semver.Make("0.0.1")
8080
// Compare using helpers: v.GT(v2), v.LT, v.GTE, v.LTE
8181
v001.GT(v) == true
8282
v.LT(v001) == true

src/github.com/blang/semver/examples/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ func main() {
3232
}
3333
}
3434

35-
// New == Parse
36-
v001, err := semver.New("0.0.1")
35+
// Make == Parse (Value), New for Pointer
36+
v001, err := semver.Make("0.0.1")
3737

3838
fmt.Println("\nUse Version.Compare for comparisons (-1, 0, 1):")
3939
fmt.Printf("%q is greater than %q: Compare == %d\n", v001, v, v001.Compare(v))

src/github.com/blang/semver/semver.go

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ const (
1313
alphanum = alphas + numbers
1414
)
1515

16-
// Latest fully supported spec version
17-
var SPEC_VERSION = Version{
16+
// SpecVersion is the latest fully supported spec version of semver
17+
var SpecVersion = Version{
1818
Major: 2,
1919
Minor: 0,
2020
Patch: 0,
2121
}
2222

23+
// Version represents a semver compatible version
2324
type Version struct {
2425
Major uint64
2526
Minor uint64
@@ -60,76 +61,73 @@ func (v Version) String() string {
6061
return string(b)
6162
}
6263

63-
// Checks if v is equal to o.
64+
// Equals checks if v is equal to o.
6465
func (v Version) Equals(o Version) bool {
6566
return (v.Compare(o) == 0)
6667
}
6768

68-
// Checks if v is equal to o.
69+
// EQ checks if v is equal to o.
6970
func (v Version) EQ(o Version) bool {
7071
return (v.Compare(o) == 0)
7172
}
7273

73-
// Checks if v is not equal to o.
74+
// NE checks if v is not equal to o.
7475
func (v Version) NE(o Version) bool {
7576
return (v.Compare(o) != 0)
7677
}
7778

78-
// Checks if v is greater than o.
79+
// GT checks if v is greater than o.
7980
func (v Version) GT(o Version) bool {
8081
return (v.Compare(o) == 1)
8182
}
8283

83-
// Checks if v is greater than or equal to o.
84+
// GTE checks if v is greater than or equal to o.
8485
func (v Version) GTE(o Version) bool {
8586
return (v.Compare(o) >= 0)
8687
}
8788

88-
// Checks if v is greater than or equal to o.
89+
// GE checks if v is greater than or equal to o.
8990
func (v Version) GE(o Version) bool {
9091
return (v.Compare(o) >= 0)
9192
}
9293

93-
// Checks if v is less than o.
94+
// LT checks if v is less than o.
9495
func (v Version) LT(o Version) bool {
9596
return (v.Compare(o) == -1)
9697
}
9798

98-
// Checks if v is less than or equal to o.
99+
// LTE checks if v is less than or equal to o.
99100
func (v Version) LTE(o Version) bool {
100101
return (v.Compare(o) <= 0)
101102
}
102103

103-
// Checks if v is less than or equal to o.
104+
// LE checks if v is less than or equal to o.
104105
func (v Version) LE(o Version) bool {
105106
return (v.Compare(o) <= 0)
106107
}
107108

108-
// Compares Versions v to o:
109+
// Compare compares Versions v to o:
109110
// -1 == v is less than o
110111
// 0 == v is equal to o
111112
// 1 == v is greater than o
112113
func (v Version) Compare(o Version) int {
113114
if v.Major != o.Major {
114115
if v.Major > o.Major {
115116
return 1
116-
} else {
117-
return -1
118117
}
118+
return -1
119119
}
120120
if v.Minor != o.Minor {
121121
if v.Minor > o.Minor {
122122
return 1
123-
} else {
124-
return -1
125123
}
124+
return -1
126125
}
127126
if v.Patch != o.Patch {
128127
if v.Patch > o.Patch {
129128
return 1
130-
} else {
131-
return -1
132129
}
130+
return -1
133131
}
134132

135133
// Quick comparison if a version has no prerelease versions
@@ -139,32 +137,31 @@ func (v Version) Compare(o Version) int {
139137
return 1
140138
} else if len(v.Pre) > 0 && len(o.Pre) == 0 {
141139
return -1
142-
} else {
143-
144-
i := 0
145-
for ; i < len(v.Pre) && i < len(o.Pre); i++ {
146-
if comp := v.Pre[i].Compare(o.Pre[i]); comp == 0 {
147-
continue
148-
} else if comp == 1 {
149-
return 1
150-
} else {
151-
return -1
152-
}
153-
}
140+
}
154141

155-
// If all pr versions are the equal but one has further prversion, this one greater
156-
if i == len(v.Pre) && i == len(o.Pre) {
157-
return 0
158-
} else if i == len(v.Pre) && i < len(o.Pre) {
159-
return -1
160-
} else {
142+
i := 0
143+
for ; i < len(v.Pre) && i < len(o.Pre); i++ {
144+
if comp := v.Pre[i].Compare(o.Pre[i]); comp == 0 {
145+
continue
146+
} else if comp == 1 {
161147
return 1
148+
} else {
149+
return -1
162150
}
151+
}
163152

153+
// If all pr versions are the equal but one has further prversion, this one greater
154+
if i == len(v.Pre) && i == len(o.Pre) {
155+
return 0
156+
} else if i == len(v.Pre) && i < len(o.Pre) {
157+
return -1
158+
} else {
159+
return 1
164160
}
161+
165162
}
166163

167-
// Validates v and returns error in case
164+
// Validate validates v and returns error in case
168165
func (v Version) Validate() error {
169166
// Major, Minor, Patch already validated using uint64
170167

@@ -191,12 +188,19 @@ func (v Version) Validate() error {
191188
return nil
192189
}
193190

194-
// Alias for Parse, parses version string and returns a validated Version or error
195-
func New(s string) (Version, error) {
191+
// New is an alias for Parse and returns a pointer, parses version string and returns a validated Version or error
192+
func New(s string) (vp *Version, err error) {
193+
v, err := Parse(s)
194+
vp = &v
195+
return
196+
}
197+
198+
// Make is an alias for Parse, parses version string and returns a validated Version or error
199+
func Make(s string) (Version, error) {
196200
return Parse(s)
197201
}
198202

199-
// Parses version string and returns a validated Version or error
203+
// Parse parses version string and returns a validated Version or error
200204
func Parse(s string) (Version, error) {
201205
if len(s) == 0 {
202206
return Version{}, errors.New("Version string empty")
@@ -294,14 +298,14 @@ func MustParse(s string) Version {
294298
return v
295299
}
296300

297-
// PreRelease Version
301+
// PRVersion represents a PreRelease Version
298302
type PRVersion struct {
299303
VersionStr string
300304
VersionNum uint64
301305
IsNum bool
302306
}
303307

304-
// Creates a new valid prerelease version
308+
// NewPRVersion creates a new valid prerelease version
305309
func NewPRVersion(s string) (PRVersion, error) {
306310
if len(s) == 0 {
307311
return PRVersion{}, errors.New("Prerelease is empty")
@@ -328,12 +332,12 @@ func NewPRVersion(s string) (PRVersion, error) {
328332
return v, nil
329333
}
330334

331-
// Is pre release version numeric?
335+
// IsNumeric checks if prerelease-version is numeric
332336
func (v PRVersion) IsNumeric() bool {
333337
return v.IsNum
334338
}
335339

336-
// Compares PreRelease Versions v to o:
340+
// Compare compares two PreRelease Versions v and o:
337341
// -1 == v is less than o
338342
// 0 == v is equal to o
339343
// 1 == v is greater than o
@@ -379,7 +383,7 @@ func hasLeadingZeroes(s string) bool {
379383
return len(s) > 1 && s[0] == '0'
380384
}
381385

382-
// Creates a new valid build version
386+
// NewBuildVersion creates a new valid build version
383387
func NewBuildVersion(s string) (string, error) {
384388
if len(s) == 0 {
385389
return "", errors.New("Buildversion is empty")

src/github.com/blang/semver/semver_test.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,21 @@ func TestNewHelper(t *testing.T) {
272272
if err != nil {
273273
t.Fatalf("Unexpected error %q", err)
274274
}
275+
276+
// New returns pointer
277+
if v == nil {
278+
t.Fatal("Version is nil")
279+
}
280+
if v.Compare(Version{1, 2, 3, nil, nil}) != 0 {
281+
t.Fatal("Unexpected comparison problem")
282+
}
283+
}
284+
285+
func TestMakeHelper(t *testing.T) {
286+
v, err := Make("1.2.3")
287+
if err != nil {
288+
t.Fatalf("Unexpected error %q", err)
289+
}
275290
if v.Compare(Version{1, 2, 3, nil, nil}) != 0 {
276291
t.Fatal("Unexpected comparison problem")
277292
}
@@ -282,7 +297,7 @@ func BenchmarkParseSimple(b *testing.B) {
282297
b.ReportAllocs()
283298
b.ResetTimer()
284299
for n := 0; n < b.N; n++ {
285-
New(VERSION)
300+
Parse(VERSION)
286301
}
287302
}
288303

@@ -291,7 +306,7 @@ func BenchmarkParseComplex(b *testing.B) {
291306
b.ReportAllocs()
292307
b.ResetTimer()
293308
for n := 0; n < b.N; n++ {
294-
New(VERSION)
309+
Parse(VERSION)
295310
}
296311
}
297312

@@ -300,13 +315,13 @@ func BenchmarkParseAverage(b *testing.B) {
300315
b.ReportAllocs()
301316
b.ResetTimer()
302317
for n := 0; n < b.N; n++ {
303-
New(formatTests[n%l].result)
318+
Parse(formatTests[n%l].result)
304319
}
305320
}
306321

307322
func BenchmarkStringSimple(b *testing.B) {
308323
const VERSION = "0.0.1"
309-
v, _ := New(VERSION)
324+
v, _ := Parse(VERSION)
310325
b.ReportAllocs()
311326
b.ResetTimer()
312327
for n := 0; n < b.N; n++ {
@@ -316,7 +331,7 @@ func BenchmarkStringSimple(b *testing.B) {
316331

317332
func BenchmarkStringLarger(b *testing.B) {
318333
const VERSION = "11.15.2012"
319-
v, _ := New(VERSION)
334+
v, _ := Parse(VERSION)
320335
b.ReportAllocs()
321336
b.ResetTimer()
322337
for n := 0; n < b.N; n++ {
@@ -326,7 +341,7 @@ func BenchmarkStringLarger(b *testing.B) {
326341

327342
func BenchmarkStringComplex(b *testing.B) {
328343
const VERSION = "0.0.1-alpha.preview+123.456"
329-
v, _ := New(VERSION)
344+
v, _ := Parse(VERSION)
330345
b.ReportAllocs()
331346
b.ResetTimer()
332347
for n := 0; n < b.N; n++ {
@@ -345,7 +360,7 @@ func BenchmarkStringAverage(b *testing.B) {
345360

346361
func BenchmarkValidateSimple(b *testing.B) {
347362
const VERSION = "0.0.1"
348-
v, _ := New(VERSION)
363+
v, _ := Parse(VERSION)
349364
b.ReportAllocs()
350365
b.ResetTimer()
351366
for n := 0; n < b.N; n++ {
@@ -355,7 +370,7 @@ func BenchmarkValidateSimple(b *testing.B) {
355370

356371
func BenchmarkValidateComplex(b *testing.B) {
357372
const VERSION = "0.0.1-alpha.preview+123.456"
358-
v, _ := New(VERSION)
373+
v, _ := Parse(VERSION)
359374
b.ReportAllocs()
360375
b.ResetTimer()
361376
for n := 0; n < b.N; n++ {
@@ -374,7 +389,7 @@ func BenchmarkValidateAverage(b *testing.B) {
374389

375390
func BenchmarkCompareSimple(b *testing.B) {
376391
const VERSION = "0.0.1"
377-
v, _ := New(VERSION)
392+
v, _ := Parse(VERSION)
378393
b.ReportAllocs()
379394
b.ResetTimer()
380395
for n := 0; n < b.N; n++ {
@@ -384,7 +399,7 @@ func BenchmarkCompareSimple(b *testing.B) {
384399

385400
func BenchmarkCompareComplex(b *testing.B) {
386401
const VERSION = "0.0.1-alpha.preview+123.456"
387-
v, _ := New(VERSION)
402+
v, _ := Parse(VERSION)
388403
b.ReportAllocs()
389404
b.ResetTimer()
390405
for n := 0; n < b.N; n++ {

src/github.com/blang/semver/sort.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@ import (
44
"sort"
55
)
66

7+
// Versions represents multiple versions.
78
type Versions []Version
89

10+
// Len returns length of version collection
911
func (s Versions) Len() int {
1012
return len(s)
1113
}
1214

15+
// Swap swaps two versions inside the collection by its indices
1316
func (s Versions) Swap(i, j int) {
1417
s[i], s[j] = s[j], s[i]
1518
}
1619

20+
// Less checks if version at index i is less than version at index j
1721
func (s Versions) Less(i, j int) bool {
1822
return s[i].LT(s[j])
1923
}

0 commit comments

Comments
 (0)