Skip to content

Commit

Permalink
kversion: remove patch from VersionGuess when we cannot know it
Browse files Browse the repository at this point in the history
Pre 1.0.0, patch numbers actually went v0.9.0.1; we already did not
include the final .1. Post 1.0.0, we cannot know patch numbers, so let's
exclude them.

Also changes from saying "tip" to "at least <final verison kversion
knows about>", so a user can know how out of date their kversion / kgo
is.

Also moves the cmp map to the top to only allocate it once. We either
clear it every loop, or it is non-empty which results in the "under"
case which returns within that loop.
  • Loading branch information
twmb committed Jan 18, 2021
1 parent 3ed5ec2 commit 3d3787e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
29 changes: 14 additions & 15 deletions pkg/kversion/kversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func (vs *Versions) Equal(other *Versions) bool {
// more verbose strings.
func (vs *Versions) VersionGuess() string {
var last string
cmp := make(map[int16]int16, len(maxTip))
for _, comparison := range []struct {
cmp []int16
name string
Expand All @@ -131,26 +132,24 @@ func (vs *Versions) VersionGuess() string {
{max0101, "v0.10.1"},
{max0102, "v0.10.2"},
{max0110, "v0.11.0"},
{max100, "v1.0.0"},
{max110, "v1.1.0"},
{max200, "v2.0.0"},
{max210, "v2.1.0"},
{max220, "v2.2.0"},
{max230, "v2.3.0"},
{max240, "v2.4.0"},
{max250, "v2.5.0"},
{max260, "v2.6.0"},
{max270, "v2.7.0"},
{maxTip, "tip"},
{max100, "v1.0"},
{max110, "v1.1"},
{max200, "v2.0"},
{max210, "v2.1"},
{max220, "v2.2"},
{max230, "v2.3"},
{max240, "v2.4"},
{max250, "v2.5"},
{max260, "v2.6"},
{max270, "v2.7"},
} {

var under, equal, over bool

cmp := make(map[int16]int16, len(comparison.cmp))
for k, v := range comparison.cmp {
cmp[int16(k)] = v
}

var under, equal, over bool

for k, v := range vs.k2v {
cmpv, has := cmp[int16(k)]
if has {
Expand Down Expand Up @@ -215,7 +214,7 @@ func (vs *Versions) VersionGuess() string {
// is no default case.
}

return last // we never reach this because we always return above
return "at least " + last
}

// Returns a string representation of the versions; the format may change.
Expand Down
11 changes: 10 additions & 1 deletion pkg/kversion/kversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,19 @@ func TestVersionGuess(t *testing.T) {
{
v := V2_7_0()
v.SetMaxKeyVersion(int16(len(v.k2v)+1), -1)
if got, exp := v.VersionGuess(), "v2.7.0"; got != exp {
if got, exp := v.VersionGuess(), "v2.7"; got != exp {
t.Errorf("got %s != exp %s without modifications", got, exp)
}
}

{
v := Tip()
v.SetMaxKeyVersion(0, 999)
if got, exp := v.VersionGuess(), "at least v2.7"; got != exp {
t.Errorf("got %s != exp %s without modifications", got, exp)
}
}

}

func TestEqual(t *testing.T) {
Expand Down

0 comments on commit 3d3787e

Please sign in to comment.