Skip to content

Commit af3461a

Browse files
committed
introduce version 4 as separate go module
1 parent 19a0680 commit af3461a

13 files changed

+2344
-5
lines changed

README.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1-
semver for golang [![Build Status](https://travis-ci.org/blang/semver.svg?branch=master)](https://travis-ci.org/blang/semver) [![GoDoc](https://godoc.org/github.com/blang/semver?status.svg)](https://godoc.org/github.com/blang/semver) [![Coverage Status](https://img.shields.io/coveralls/blang/semver.svg)](https://coveralls.io/r/blang/semver?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/blang/semver)](https://goreportcard.com/report/github.com/blang/semver)
1+
semver for golang [![Build Status](https://travis-ci.org/blang/semver.svg?branch=master)](https://travis-ci.org/blang/semver) [![GoDoc](https://godoc.org/github.com/blang/semver/v4?status.svg)](https://godoc.org/github.com/blang/semver/v4) [![Coverage Status](https://img.shields.io/coveralls/blang/semver.svg)](https://coveralls.io/r/blang/semver?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/blang/semver)](https://goreportcard.com/report/github.com/blang/semver)
22
======
33

44
semver is a [Semantic Versioning](http://semver.org/) library written in golang. It fully covers spec version `2.0.0`.
55

6+
Versioning
7+
----------
8+
Old v1-v3 versions exist in the root of the repository for compatiblity reasons and will only receive bug fixes.
9+
10+
The current stable version is [*v4*](v4/).
11+
612
Usage
713
-----
814
```bash
9-
$ go get github.com/blang/semver
15+
$ go get github.com/blang/semver/v4
1016
```
1117
Note: Always vendor your dependencies or fix on a specific version tag.
1218

1319
```go
14-
import github.com/blang/semver
20+
import github.com/blang/semver/v4
1521
v1, err := semver.Make("1.0.0-beta")
1622
v2, err := semver.Make("2.0.0-beta")
1723
v1.Compare(v2)
1824
```
1925

20-
Also check the [GoDocs](http://godoc.org/github.com/blang/semver).
26+
Also check the [GoDocs](http://godoc.org/github.com/blang/semver/v4).
2127

2228
Why should I use this lib?
2329
-----
@@ -96,7 +102,7 @@ Example
96102
Have a look at full examples in [examples/main.go](examples/main.go)
97103

98104
```go
99-
import github.com/blang/semver
105+
import github.com/blang/semver/v4
100106

101107
v, err := semver.Make("0.0.1-alpha.preview+123.github")
102108
fmt.Printf("Major: %d\n", v.Major)

v4/examples/main.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/blang/semver/v4"
7+
)
8+
9+
func main() {
10+
v, err := semver.Parse("0.0.1-alpha.preview.222+123.github")
11+
if err != nil {
12+
fmt.Printf("Error while parsing (not valid): %q", err)
13+
}
14+
fmt.Printf("Version to string: %q\n", v)
15+
16+
fmt.Printf("Major: %d\n", v.Major)
17+
fmt.Printf("Minor: %d\n", v.Minor)
18+
fmt.Printf("Patch: %d\n", v.Patch)
19+
20+
// Prerelease versions
21+
if len(v.Pre) > 0 {
22+
fmt.Println("Prerelease versions:")
23+
for i, pre := range v.Pre {
24+
fmt.Printf("%d: %q\n", i, pre)
25+
}
26+
}
27+
28+
// Build meta data
29+
if len(v.Build) > 0 {
30+
fmt.Println("Build meta data:")
31+
for i, build := range v.Build {
32+
fmt.Printf("%d: %q\n", i, build)
33+
}
34+
}
35+
36+
// Make == Parse (Value), New for Pointer
37+
v001, _ := semver.Make("0.0.1")
38+
39+
fmt.Println("\nUse Version.Compare for comparisons (-1, 0, 1):")
40+
fmt.Printf("%q is greater than %q: Compare == %d\n", v001, v, v001.Compare(v))
41+
fmt.Printf("%q is less than %q: Compare == %d\n", v, v001, v.Compare(v001))
42+
fmt.Printf("%q is equal to %q: Compare == %d\n", v, v, v.Compare(v))
43+
44+
fmt.Println("\nUse comparison helpers returning booleans:")
45+
fmt.Printf("%q is greater than %q: %t\n", v001, v, v001.GT(v))
46+
fmt.Printf("%q is greater than equal %q: %t\n", v001, v, v001.GTE(v))
47+
fmt.Printf("%q is greater than equal %q: %t\n", v, v, v.GTE(v))
48+
fmt.Printf("%q is less than %q: %t\n", v, v001, v.LT(v001))
49+
fmt.Printf("%q is less than equal %q: %t\n", v, v001, v.LTE(v001))
50+
fmt.Printf("%q is less than equal %q: %t\n", v, v, v.LTE(v))
51+
52+
fmt.Println("\nManipulate Version in place:")
53+
v.Pre[0], err = semver.NewPRVersion("beta")
54+
if err != nil {
55+
fmt.Printf("Error parsing pre release version: %q", err)
56+
}
57+
fmt.Printf("Version to string: %q\n", v)
58+
59+
fmt.Println("\nCompare Prerelease versions:")
60+
pre1, _ := semver.NewPRVersion("123")
61+
pre2, _ := semver.NewPRVersion("alpha")
62+
pre3, _ := semver.NewPRVersion("124")
63+
fmt.Printf("%q is less than %q: Compare == %d\n", pre1, pre2, pre1.Compare(pre2))
64+
fmt.Printf("%q is greater than %q: Compare == %d\n", pre3, pre1, pre3.Compare(pre1))
65+
fmt.Printf("%q is equal to %q: Compare == %d\n", pre1, pre1, pre1.Compare(pre1))
66+
67+
fmt.Println("\nValidate versions:")
68+
v.Build[0] = "?"
69+
70+
err = v.Validate()
71+
if err != nil {
72+
fmt.Printf("Validation failed: %s\n", err)
73+
}
74+
75+
fmt.Println("Create valid build meta data:")
76+
b1, _ := semver.NewBuildVersion("build123")
77+
v.Build[0] = b1
78+
fmt.Printf("Version with new build version %q\n", v)
79+
80+
_, err = semver.NewBuildVersion("build?123")
81+
if err != nil {
82+
fmt.Printf("Create build version failed: %s\n", err)
83+
}
84+
}

v4/go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/blang/semver/v4
2+
3+
go 1.14

v4/json.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package semver
2+
3+
import (
4+
"encoding/json"
5+
)
6+
7+
// MarshalJSON implements the encoding/json.Marshaler interface.
8+
func (v Version) MarshalJSON() ([]byte, error) {
9+
return json.Marshal(v.String())
10+
}
11+
12+
// UnmarshalJSON implements the encoding/json.Unmarshaler interface.
13+
func (v *Version) UnmarshalJSON(data []byte) (err error) {
14+
var versionString string
15+
16+
if err = json.Unmarshal(data, &versionString); err != nil {
17+
return
18+
}
19+
20+
*v, err = Parse(versionString)
21+
22+
return
23+
}

v4/json_test.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package semver
2+
3+
import (
4+
"encoding/json"
5+
"strconv"
6+
"testing"
7+
)
8+
9+
func TestJSONMarshal(t *testing.T) {
10+
versionString := "3.1.4-alpha.1.5.9+build.2.6.5"
11+
v, err := Parse(versionString)
12+
if err != nil {
13+
t.Fatal(err)
14+
}
15+
16+
versionJSON, err := json.Marshal(v)
17+
if err != nil {
18+
t.Fatal(err)
19+
}
20+
21+
quotedVersionString := strconv.Quote(versionString)
22+
23+
if string(versionJSON) != quotedVersionString {
24+
t.Fatalf("JSON marshaled semantic version not equal: expected %q, got %q", quotedVersionString, string(versionJSON))
25+
}
26+
}
27+
28+
func TestJSONUnmarshal(t *testing.T) {
29+
versionString := "3.1.4-alpha.1.5.9+build.2.6.5"
30+
quotedVersionString := strconv.Quote(versionString)
31+
32+
var v Version
33+
if err := json.Unmarshal([]byte(quotedVersionString), &v); err != nil {
34+
t.Fatal(err)
35+
}
36+
37+
if v.String() != versionString {
38+
t.Fatalf("JSON unmarshaled semantic version not equal: expected %q, got %q", versionString, v.String())
39+
}
40+
41+
badVersionString := strconv.Quote("3.1.4.1.5.9.2.6.5-other-digits-of-pi")
42+
if err := json.Unmarshal([]byte(badVersionString), &v); err == nil {
43+
t.Fatal("expected JSON unmarshal error, got nil")
44+
}
45+
46+
if err := json.Unmarshal([]byte("3.1"), &v); err == nil {
47+
t.Fatal("expected JSON unmarshal error, got nil")
48+
}
49+
}

0 commit comments

Comments
 (0)