Skip to content

Commit

Permalink
Introduce version struct
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianczech committed Mar 25, 2024
1 parent ead76f9 commit 4858269
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/translate/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
72 changes: 72 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -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
}
49 changes: 49 additions & 0 deletions pkg/version/version_test.go
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit 4858269

Please sign in to comment.