Skip to content

Commit

Permalink
Merge pull request #12 from bugst/normalized_string
Browse files Browse the repository at this point in the history
Added NormalizedString to be used as key in maps
  • Loading branch information
cmaglie authored Mar 16, 2023
2 parents 518e126 + 9dd7f57 commit e797f1e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
7 changes: 7 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestParser(t *testing.T) {
v, err := Parse(in)
require.NoError(t, err, "parsing '%s'", in)
require.Equal(t, in, v.String(), "printing of '%s'", in)
require.Equal(t, normalized, string(v.NormalizedString()), "normalized printing of '%s'", in)
dump := string(v.major) + ","
dump += string(v.minor) + ","
dump += string(v.patch)
Expand Down Expand Up @@ -194,6 +195,12 @@ func TestParser(t *testing.T) {
invalid("1.2.3.")
}

func TestNilVersionStringOutput(t *testing.T) {
var nilVersion *Version
require.Equal(t, "", nilVersion.String())
require.Equal(t, "", string(nilVersion.NormalizedString()))
}

func TestParseRelaxed(t *testing.T) {
bad := ParseRelaxed("bad")
require.Nil(t, bad.version)
Expand Down
13 changes: 13 additions & 0 deletions relaxed_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ func (v *RelaxedVersion) String() string {
return string(v.customversion)
}

// NormalizedString return a string representation of the version that is
// normalized (always have a major, minor and patch version when semver compliant).
// This is useful to be used in maps and other places where the version is used as a key.
func (v *RelaxedVersion) NormalizedString() NormalizedString {
if v == nil {
return ""
}
if v.version != nil {
return v.version.NormalizedString()
}
return NormalizedString(v.customversion)
}

// CompareTo compares the RelaxedVersion with the one passed as parameter.
// Returns -1, 0 or 1 if the version is respectively less than, equal
// or greater than the compared Version
Expand Down
3 changes: 3 additions & 0 deletions relaxed_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,7 @@ func TestRelaxedCompatibleWith(t *testing.T) {
func TestNilRelaxedVersionString(t *testing.T) {
var nilVersion *RelaxedVersion
require.Equal(t, "", nilVersion.String())
require.Equal(t, "", string(nilVersion.NormalizedString()))
require.Equal(t, "1.0.0", string(ParseRelaxed("1.0.0").NormalizedString()))
require.Equal(t, "invalid-semver", string(ParseRelaxed("invalid-semver").NormalizedString()))
}
47 changes: 47 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,53 @@ func (v *Version) String() string {
return res
}

// NormalizedString is a datatype to be used in maps and other places where the
// version is used as a key.
type NormalizedString string

// NormalizedString return a string representation of the version that is
// normalized to always have a major, minor and patch version. This is useful
// to be used in maps and other places where the version is used as a key.
func (v *Version) NormalizedString() NormalizedString {
if v == nil {
return ""
}
res := NormalizedString("")
if len(v.major) > 0 {
res += NormalizedString(v.major)
} else {
res += "0"
}

if len(v.minor) > 0 {
res += "." + NormalizedString(v.minor)
} else {
res += ".0"
}
if len(v.patch) > 0 {
res += "." + NormalizedString(v.patch)
} else {
res += ".0"
}
for i, prerelease := range v.prerelases {
if i == 0 {
res += "-"
} else {
res += "."
}
res += NormalizedString(prerelease)
}
for i, build := range v.builds {
if i == 0 {
res += "+"
} else {
res += "."
}
res += NormalizedString(build)
}
return res
}

var zero = []byte("0")

// Normalize transforms a truncated semver version in a strictly compliant semver
Expand Down

0 comments on commit e797f1e

Please sign in to comment.