forked from blang/semver
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce version 4 as separate go module
- Loading branch information
Showing
13 changed files
with
2,344 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/blang/semver/v4" | ||
) | ||
|
||
func main() { | ||
v, err := semver.Parse("0.0.1-alpha.preview.222+123.github") | ||
if err != nil { | ||
fmt.Printf("Error while parsing (not valid): %q", err) | ||
} | ||
fmt.Printf("Version to string: %q\n", v) | ||
|
||
fmt.Printf("Major: %d\n", v.Major) | ||
fmt.Printf("Minor: %d\n", v.Minor) | ||
fmt.Printf("Patch: %d\n", v.Patch) | ||
|
||
// Prerelease versions | ||
if len(v.Pre) > 0 { | ||
fmt.Println("Prerelease versions:") | ||
for i, pre := range v.Pre { | ||
fmt.Printf("%d: %q\n", i, pre) | ||
} | ||
} | ||
|
||
// Build meta data | ||
if len(v.Build) > 0 { | ||
fmt.Println("Build meta data:") | ||
for i, build := range v.Build { | ||
fmt.Printf("%d: %q\n", i, build) | ||
} | ||
} | ||
|
||
// Make == Parse (Value), New for Pointer | ||
v001, _ := semver.Make("0.0.1") | ||
|
||
fmt.Println("\nUse Version.Compare for comparisons (-1, 0, 1):") | ||
fmt.Printf("%q is greater than %q: Compare == %d\n", v001, v, v001.Compare(v)) | ||
fmt.Printf("%q is less than %q: Compare == %d\n", v, v001, v.Compare(v001)) | ||
fmt.Printf("%q is equal to %q: Compare == %d\n", v, v, v.Compare(v)) | ||
|
||
fmt.Println("\nUse comparison helpers returning booleans:") | ||
fmt.Printf("%q is greater than %q: %t\n", v001, v, v001.GT(v)) | ||
fmt.Printf("%q is greater than equal %q: %t\n", v001, v, v001.GTE(v)) | ||
fmt.Printf("%q is greater than equal %q: %t\n", v, v, v.GTE(v)) | ||
fmt.Printf("%q is less than %q: %t\n", v, v001, v.LT(v001)) | ||
fmt.Printf("%q is less than equal %q: %t\n", v, v001, v.LTE(v001)) | ||
fmt.Printf("%q is less than equal %q: %t\n", v, v, v.LTE(v)) | ||
|
||
fmt.Println("\nManipulate Version in place:") | ||
v.Pre[0], err = semver.NewPRVersion("beta") | ||
if err != nil { | ||
fmt.Printf("Error parsing pre release version: %q", err) | ||
} | ||
fmt.Printf("Version to string: %q\n", v) | ||
|
||
fmt.Println("\nCompare Prerelease versions:") | ||
pre1, _ := semver.NewPRVersion("123") | ||
pre2, _ := semver.NewPRVersion("alpha") | ||
pre3, _ := semver.NewPRVersion("124") | ||
fmt.Printf("%q is less than %q: Compare == %d\n", pre1, pre2, pre1.Compare(pre2)) | ||
fmt.Printf("%q is greater than %q: Compare == %d\n", pre3, pre1, pre3.Compare(pre1)) | ||
fmt.Printf("%q is equal to %q: Compare == %d\n", pre1, pre1, pre1.Compare(pre1)) | ||
|
||
fmt.Println("\nValidate versions:") | ||
v.Build[0] = "?" | ||
|
||
err = v.Validate() | ||
if err != nil { | ||
fmt.Printf("Validation failed: %s\n", err) | ||
} | ||
|
||
fmt.Println("Create valid build meta data:") | ||
b1, _ := semver.NewBuildVersion("build123") | ||
v.Build[0] = b1 | ||
fmt.Printf("Version with new build version %q\n", v) | ||
|
||
_, err = semver.NewBuildVersion("build?123") | ||
if err != nil { | ||
fmt.Printf("Create build version failed: %s\n", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/blang/semver/v4 | ||
|
||
go 1.14 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package semver | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
// MarshalJSON implements the encoding/json.Marshaler interface. | ||
func (v Version) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(v.String()) | ||
} | ||
|
||
// UnmarshalJSON implements the encoding/json.Unmarshaler interface. | ||
func (v *Version) UnmarshalJSON(data []byte) (err error) { | ||
var versionString string | ||
|
||
if err = json.Unmarshal(data, &versionString); err != nil { | ||
return | ||
} | ||
|
||
*v, err = Parse(versionString) | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package semver | ||
|
||
import ( | ||
"encoding/json" | ||
"strconv" | ||
"testing" | ||
) | ||
|
||
func TestJSONMarshal(t *testing.T) { | ||
versionString := "3.1.4-alpha.1.5.9+build.2.6.5" | ||
v, err := Parse(versionString) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
versionJSON, err := json.Marshal(v) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
quotedVersionString := strconv.Quote(versionString) | ||
|
||
if string(versionJSON) != quotedVersionString { | ||
t.Fatalf("JSON marshaled semantic version not equal: expected %q, got %q", quotedVersionString, string(versionJSON)) | ||
} | ||
} | ||
|
||
func TestJSONUnmarshal(t *testing.T) { | ||
versionString := "3.1.4-alpha.1.5.9+build.2.6.5" | ||
quotedVersionString := strconv.Quote(versionString) | ||
|
||
var v Version | ||
if err := json.Unmarshal([]byte(quotedVersionString), &v); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if v.String() != versionString { | ||
t.Fatalf("JSON unmarshaled semantic version not equal: expected %q, got %q", versionString, v.String()) | ||
} | ||
|
||
badVersionString := strconv.Quote("3.1.4.1.5.9.2.6.5-other-digits-of-pi") | ||
if err := json.Unmarshal([]byte(badVersionString), &v); err == nil { | ||
t.Fatal("expected JSON unmarshal error, got nil") | ||
} | ||
|
||
if err := json.Unmarshal([]byte("3.1"), &v); err == nil { | ||
t.Fatal("expected JSON unmarshal error, got nil") | ||
} | ||
} |
Oops, something went wrong.