From 48582692a323d2c7786ff86a5f8985bcbaaa13ea Mon Sep 17 00:00:00 2001 From: Sebastian Czech Date: Mon, 25 Mar 2024 12:13:15 +0100 Subject: [PATCH] Introduce version struct --- pkg/translate/structs_test.go | 10 +++++ pkg/version/version.go | 72 +++++++++++++++++++++++++++++++++++ pkg/version/version_test.go | 49 ++++++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 pkg/version/version.go create mode 100644 pkg/version/version_test.go diff --git a/pkg/translate/structs_test.go b/pkg/translate/structs_test.go index 415be26d..743e804b 100644 --- a/pkg/translate/structs_test.go +++ b/pkg/translate/structs_test.go @@ -222,3 +222,13 @@ func TestNestedSpecs(t *testing.T) { assert.Contains(t, nestedSpecs, "A") assert.Contains(t, nestedSpecs, "AB") } + +func TestCreateGoSuffixFromVersion(t *testing.T) { + // given + + // when + suffix := CreateGoSuffixFromVersion("10.1.1") + + // then + assert.Equal(t, "_10_1_1", suffix) +} diff --git a/pkg/version/version.go b/pkg/version/version.go new file mode 100644 index 00000000..df8175eb --- /dev/null +++ b/pkg/version/version.go @@ -0,0 +1,72 @@ +package version + +import ( + "errors" + "fmt" + "strconv" + "strings" +) + +// Version is the version number struct. +type Version struct { + Major, Minor, Patch int + Hotfix string +} + +// Gte tests if this version number is greater than or equal to the argument. +func (v Version) Gte(o Version) bool { + if v.Major != o.Major { + return v.Major > o.Major + } + + if v.Minor != o.Minor { + return v.Minor > o.Minor + } + + return v.Patch >= o.Patch +} + +// String returns the version number as a string. +func (v Version) String() string { + if v.Hotfix == "" { + return fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Patch) + } else { + return fmt.Sprintf("%d.%d.%d-%s", v.Major, v.Minor, v.Patch, v.Hotfix) + } +} + +// New returns a version number from the given string. +func New(version string) (Version, error) { + parts := strings.Split(version, ".") + if len(parts) != 3 { + return Version{}, errors.New("invalid version") + } + + major, err := strconv.Atoi(parts[0]) + if err != nil { + return Version{}, fmt.Errorf("major %s is not a number: %s", parts[0], err) + } + + minor, err := strconv.Atoi(parts[1]) + if err != nil { + return Version{}, fmt.Errorf("minor %s is not a number: %s", parts[0], err) + } + + patchWithHotfix := strings.Split(parts[2], "-") + + var hotfix string + if len(patchWithHotfix) == 1 { + hotfix = "" + } else if len(patchWithHotfix) == 2 { + hotfix = patchWithHotfix[1] + } else { + return Version{}, fmt.Errorf("patch %s is not formatted as expected", parts[2]) + } + + patch, err := strconv.Atoi(patchWithHotfix[0]) + if err != nil { + return Version{}, fmt.Errorf("patch %s is not a number: %s", parts[2], err) + } + + return Version{major, minor, patch, hotfix}, nil +} diff --git a/pkg/version/version_test.go b/pkg/version/version_test.go new file mode 100644 index 00000000..b0a0f2f4 --- /dev/null +++ b/pkg/version/version_test.go @@ -0,0 +1,49 @@ +package version + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestCorrectVersion(t *testing.T) { + // given + + // when + version1011, err1011 := New("10.1.1") + version1011h2, err1011h2 := New("10.1.1-h2") + + // then + assert.NoError(t, err1011) + assert.Equal(t, 10, version1011.Major) + assert.Equal(t, 1, version1011.Minor) + assert.Equal(t, 1, version1011.Patch) + assert.Equal(t, "", version1011.Hotfix) + assert.NoError(t, err1011h2) + assert.Equal(t, 10, version1011h2.Major) + assert.Equal(t, 1, version1011h2.Minor) + assert.Equal(t, 1, version1011h2.Patch) + assert.Equal(t, "h2", version1011h2.Hotfix) +} + +func TestIncorrectVersion(t *testing.T) { + // given + + // when + _, err101 := New("10.1") + _, err1011h2h2 := New("10.1.1-h2-h2") + + // then + assert.Error(t, err101) + assert.Error(t, err1011h2h2) +} + +func TestVersionComparison(t *testing.T) { + // given + + // when + v1, _ := New("10.1.1") + v2, _ := New("10.2.1-h2") + + // then + assert.True(t, v2.Gte(v1)) +}