diff --git a/version.go b/version.go index 57ba3ec..85094a8 100644 --- a/version.go +++ b/version.go @@ -34,6 +34,15 @@ func (v Version) String() string { return d } +// RoutableString returns the string representation of the version that can used while setting up the router. +// If the version is before the DefaultPivotDate, it will return the stability in the string. +func (v Version) RoutableString() string { + if v.Date.Before(DefaultPivotDate.Date) { + return v.String() + } + return v.DateString() +} + // AddDays returns the version corresponding to adding the given number of days // to the version date. func (v Version) AddDays(days int) Version { diff --git a/version_test.go b/version_test.go index bd5867f..c25df4d 100644 --- a/version_test.go +++ b/version_test.go @@ -557,3 +557,54 @@ func TestVersionIndex_ResolveGAorBetaStability(t *testing.T) { }) } } + +func TestVersion_RoutableString(t *testing.T) { + tests := []struct { + name string + v Version + want string + }{ + { + name: "ga before pivot date", + v: MustParseVersion("2021-06-07"), + want: "2021-06-07", + }, + { + name: "beta before pivot date", + v: MustParseVersion("2021-06-07~beta"), + want: "2021-06-07~beta", + }, + { + name: "beta before pivot date", + v: MustParseVersion("2021-06-07~experimental"), + want: "2021-06-07~experimental", + }, + { + name: "ga on pivot date", + v: MustParseVersion("2024-10-15"), + want: "2024-10-15", + }, + { + name: "beta on pivot date", + v: MustParseVersion("2024-10-15~beta"), + want: "2024-10-15", + }, + { + name: "ga after pivot date", + v: MustParseVersion("2024-11-15"), + want: "2024-11-15", + }, + { + name: "beta after pivot date", + v: MustParseVersion("2024-11-15~beta"), + want: "2024-11-15", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.v.RoutableString(); got != tt.want { + t.Errorf("RoutableString() = %v, want %v", got, tt.want) + } + }) + } +}