commit 1eb61f45ead781b1b27e33a94a67072707544a5a Author: Aneurin Barker Snook Date: Thu Jul 11 22:04:40 2024 +0100 add version package diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..977375c --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,11 @@ +# MIT License + +Copyright © 2024 Aneurin Barker Snook a@aneur.in + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +https://mit-license.org/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..a9bf9ea --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# Go Version + +A simple library for parsing and comparing versions according to [Semantic Versioning 2.0.0](https://semver.org/). + +## License + +See [LICENSE.md](../LICENSE.md) diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..f88fc74 --- /dev/null +++ b/errors.go @@ -0,0 +1,7 @@ +package version + +import "errors" + +var ( + ErrInvalidVersion = errors.New("invalid version") +) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..6b953b1 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/annybs/version + +go 1.21 diff --git a/parse.go b/parse.go new file mode 100644 index 0000000..a378679 --- /dev/null +++ b/parse.go @@ -0,0 +1,92 @@ +package version + +import ( + "strconv" + "strings" +) + +const ( + sectionMajor = 0 + sectionMinor = 1 + sectionPatch = 2 + sectionExtension = 3 +) + +func MustParse(str string) *Version { + v, err := Parse(str) + if err != nil { + panic(err) + } + return v +} + +func Parse(str string) (*Version, error) { + v := &Version{Text: string(str)} + + if len(str) == 0 { + return nil, ErrInvalidVersion + } + + section := sectionMajor + chars := []byte{} + + commit := func() error { + if len(chars) == 0 { + return ErrInvalidVersion + } + + if section < sectionExtension { + n, err := strconv.Atoi(string(chars)) + if err != nil { + return ErrInvalidVersion + } + + switch section { + case sectionMajor: + v.Major = n + case sectionMinor: + v.Minor = n + case sectionPatch: + v.Patch = n + } + } else { + v.Extension = string(chars) + } + + chars = []byte{} + return nil + } + + for i := 0; i < len(str); i++ { + c := str[i] + + if i == 0 && strings.IndexByte("vV", c) > -1 { + continue + } + + if section < sectionExtension { + if strings.IndexByte("0123456789", c) > -1 { + chars = append(chars, c) + } else { + if err := commit(); err != nil { + return nil, err + } + if c == '.' { + section++ + } else { + section = sectionExtension + chars = append(chars, c) + } + } + } else { + chars = append(chars, c) + } + } + if len(chars) > 0 { + if err := commit(); err != nil { + return nil, err + } + } + + return v, nil +} diff --git a/parse_test.go b/parse_test.go new file mode 100644 index 0000000..b9204f8 --- /dev/null +++ b/parse_test.go @@ -0,0 +1,52 @@ +package version + +import ( + "errors" + "testing" +) + +func TestVersion_Parse(t *testing.T) { + type TestCase struct { + Input string + Expected Version + Err error + } + + testCases := []TestCase{ + {Input: "0.0.0", Expected: Version{Text: "0.0.0"}}, + {Input: "1.0.0", Expected: Version{Major: 1, Text: "1.0.0"}}, + {Input: "0.1.0", Expected: Version{Minor: 1, Text: "0.1.0"}}, + {Input: "0.0.1", Expected: Version{Patch: 1, Text: "0.0.1"}}, + {Input: "10.20.30", Expected: Version{Major: 10, Minor: 20, Patch: 30, Text: "10.20.30"}}, + {Input: "27.31.15", Expected: Version{Major: 27, Minor: 31, Patch: 15, Text: "27.31.15"}}, + {Input: "v1.2.3", Expected: Version{Major: 1, Minor: 2, Patch: 3, Text: "v1.2.3"}}, + {Input: "v1", Expected: Version{Major: 1, Text: "v1"}}, + {Input: "v2.31", Expected: Version{Major: 2, Minor: 31, Text: "v2.31"}}, + {Input: "v1.2.0a", Expected: Version{Major: 1, Minor: 2, Extension: "a", Text: "v1.2.0a"}}, + {Input: "v1.2a", Expected: Version{Major: 1, Minor: 2, Extension: "a", Text: "v1.2a"}}, + {Input: "v1-alpha2", Expected: Version{Major: 1, Extension: "-alpha2", Text: "v1-alpha2"}}, + {Input: "invalid version", Err: ErrInvalidVersion}, + {Input: "v.01", Err: ErrInvalidVersion}, + {Input: "v-any", Err: ErrInvalidVersion}, + } + + for i, testCase := range testCases { + actual, err := Parse(testCase.Input) + + if testCase.Err != nil { + if err == nil { + t.Errorf("test %d failed (expected error %v, actual nil)", i, testCase.Err) + } else if !errors.Is(err, testCase.Err) { + t.Errorf("test %d failed (expected error %q, actual error %q)", i, testCase.Err, err) + } else { + t.Logf("test %d succeeded with error %q for %q\n", i, testCase.Err, testCase.Input) + } + } else if err != nil { + t.Errorf("test %d failed (expected error nil, actual error %q)", i, err) + } else if actual.Major != testCase.Expected.Major || actual.Minor != testCase.Expected.Minor || actual.Patch != testCase.Expected.Patch || actual.Text != testCase.Expected.Text { + t.Errorf("test %d failed (expected %v, actual %v)", i, testCase.Expected, actual) + } else { + t.Logf("test %d succeeded with %v\n", i, actual) + } + } +} diff --git a/version.go b/version.go new file mode 100644 index 0000000..919dfcd --- /dev/null +++ b/version.go @@ -0,0 +1,59 @@ +package version + +import ( + "fmt" +) + +// Version is a structured representation of a version number +type Version struct { + Major int + Minor int + Patch int + Extension string + + Text string +} + +// Compare this version (a) with another version (b). +// This function returns -1 if a is less than b, 1 if a is greater than b, or 0 if a is equal to b. +// +// Extensions such as pre-release version or build metadata are ignored when comparing versions. +func (a *Version) Compare(b *Version) int { + if a.Major == b.Major { + if a.Minor == b.Minor { + if a.Patch == b.Patch { + return 0 + } else if a.Patch > b.Patch { + return 1 + } + } else if a.Minor > b.Minor { + return 1 + } + } else if a.Major > b.Major { + return 1 + } + + return -1 +} + +// Less performs a simple comparison of this version (a) with another version (b). +// This function returns true if a is less than b, or false otherwise. +// +// Extensions such as pre-release version or build metadata are ignored when comparing versions. +func (a *Version) Less(b *Version) bool { + return a.Compare(b) < 0 +} + +// SemanticString returns a version string conforming to the standard described in Semantic Versioning 2.0.0. +// +// See https://semver.org/#is-v123-a-semantic-version +func (v *Version) SemanticString() string { + return fmt.Sprintf("%d.%d.%d%s", v.Major, v.Minor, v.Patch, v.Extension) +} + +func (v *Version) String() string { + if v.Text != "" { + return v.Text + } + return v.SemanticString() +} diff --git a/version_test.go b/version_test.go new file mode 100644 index 0000000..b2e1649 --- /dev/null +++ b/version_test.go @@ -0,0 +1,101 @@ +package version + +import ( + "testing" +) + +func TestVersion_String(t *testing.T) { + type TestCase struct { + Expected string // Expected + Input Version // Input + } + + testCases := []TestCase{ + {Expected: "0.0.0", Input: Version{}}, + {Expected: "1.0.0", Input: Version{Major: 1}}, + {Expected: "0.1.0", Input: Version{Minor: 1}}, + {Expected: "0.0.1", Input: Version{Patch: 1}}, + {Expected: "10.20.30", Input: Version{Major: 10, Minor: 20, Patch: 30}}, + {Expected: "27.31.15", Input: Version{Major: 27, Minor: 31, Patch: 15}}, + {Expected: "v1.2.3", Input: Version{Major: 1, Minor: 2, Patch: 3, Text: "v1.2.3"}}, + {Expected: "v1.2.3", Input: Version{Text: "v1.2.3"}}, + {Expected: "v1.2.0a", Input: Version{Major: 1, Minor: 2, Extension: "a", Text: "v1.2.0a"}}, + {Expected: "v1-alpha2", Input: Version{Major: 1, Extension: "-alpha2", Text: "v1-alpha2"}}, + } + + for i, testCase := range testCases { + actual := testCase.Input.String() + + if actual != testCase.Expected { + t.Errorf("test %d failed (expected %s, actual %s)", i, testCase.Expected, actual) + } else { + t.Logf("test %d succeeded with %s\n", i, actual) + } + } +} + +func TestVersion_Compare(t *testing.T) { + type TestCase struct { + A *Version + B *Version + Expected int + } + + testCases := []TestCase{ + {A: MustParse("0.0.1"), B: MustParse("0.0.1"), Expected: 0}, + {A: MustParse("0.1.0"), B: MustParse("0.1.0"), Expected: 0}, + {A: MustParse("1.0.0"), B: MustParse("1.0.0"), Expected: 0}, + {A: MustParse("1.0.0"), B: MustParse("0.1.0"), Expected: 1}, + {A: MustParse("1.0.0"), B: MustParse("0.0.1"), Expected: 1}, + {A: MustParse("1.0.0"), B: MustParse("1.1.0"), Expected: -1}, + {A: MustParse("1.0.0"), B: MustParse("1.0.1"), Expected: -1}, + {A: MustParse("1.0.0"), B: MustParse("2.0.0"), Expected: -1}, + {A: MustParse("1.1.0"), B: MustParse("1.2.0"), Expected: -1}, + {A: MustParse("1.1.1"), B: MustParse("1.2.0"), Expected: -1}, + {A: MustParse("1.20.0"), B: MustParse("1.2.0"), Expected: 1}, + {A: MustParse("1.20.0"), B: MustParse("1.2.20"), Expected: 1}, + {A: MustParse("1.20.0"), B: MustParse("1.20.1"), Expected: -1}, + } + + for i, testCase := range testCases { + actual := testCase.A.Compare(testCase.B) + if actual != testCase.Expected { + t.Errorf("test %d failed (expected %d, actual %d)", i, testCase.Expected, actual) + } else { + t.Logf("test %d succeeded with %d", i, actual) + } + } +} + +func TestVersion_Less(t *testing.T) { + type TestCase struct { + A *Version + B *Version + Expected bool + } + + testCases := []TestCase{ + {A: MustParse("0.0.1"), B: MustParse("0.0.1"), Expected: false}, + {A: MustParse("0.1.0"), B: MustParse("0.1.0"), Expected: false}, + {A: MustParse("1.0.0"), B: MustParse("1.0.0"), Expected: false}, + {A: MustParse("1.0.0"), B: MustParse("0.1.0"), Expected: false}, + {A: MustParse("1.0.0"), B: MustParse("0.0.1"), Expected: false}, + {A: MustParse("1.0.0"), B: MustParse("1.1.0"), Expected: true}, + {A: MustParse("1.0.0"), B: MustParse("1.0.1"), Expected: true}, + {A: MustParse("1.0.0"), B: MustParse("2.0.0"), Expected: true}, + {A: MustParse("1.1.0"), B: MustParse("1.2.0"), Expected: true}, + {A: MustParse("1.1.1"), B: MustParse("1.2.0"), Expected: true}, + {A: MustParse("1.20.0"), B: MustParse("1.2.0"), Expected: false}, + {A: MustParse("1.20.0"), B: MustParse("1.2.20"), Expected: false}, + {A: MustParse("1.20.0"), B: MustParse("1.20.1"), Expected: true}, + } + + for i, testCase := range testCases { + actual := testCase.A.Less(testCase.B) + if actual != testCase.Expected { + t.Errorf("test %d failed (expected %v, actual %v)", i, testCase.Expected, actual) + } else { + t.Logf("test %d succeeded with %v", i, actual) + } + } +}