Skip to content

Commit

Permalink
internal/history: rename Version to GoVer
Browse files Browse the repository at this point in the history
Make it more clear that this type represents a Go-specific version,
rather than a version that follows the Semantic Versioning 2.0.0
specification. The type already has Go-specific methods, for example
IsMajor reports true for "1.14" and IsMinor reports true for "1.14.1".
This is consistent with the terminology used by Go releases, but
very surprising if considered from the perspective of semver.

Document some differences of the Go-specific version convention
compared to semver, and describe the X, Y, Z fields in more detail.

This change makes it viable to add a String method to GoVer type
documented to print a Go-specific version string, which will be
useful in more places in CL 229483.

For golang/go#32450.

Change-Id: If7482fdb4a739ff5b89b7133402d94412057f590
Reviewed-on: https://go-review.googlesource.com/c/website/+/229481
Reviewed-by: Carlos Amedee <carlos@golang.org>
  • Loading branch information
passionSeven committed Apr 23, 2020
1 parent 02cb727 commit 19fded7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
19 changes: 6 additions & 13 deletions cmd/golangorg/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ func (h releaseHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// sortReleases returns a sorted list of Go releases, suitable to be
// displayed on the Release History page. Releases are arranged into
// major releases, each with minor revisions.
func sortReleases(rs map[history.Version]history.Release) []Major {
func sortReleases(rs map[history.GoVer]history.Release) []Major {
var major []Major
byMajorVersion := make(map[history.Version]Major)
byMajorVersion := make(map[history.GoVer]Major)
for v, r := range rs {
switch {
case v.IsMajor():
Expand Down Expand Up @@ -108,8 +108,8 @@ func sortReleases(rs map[history.Version]history.Release) []Major {

// majorOf takes a Go version like 1.5, 1.5.1, 1.5.2, etc.,
// and returns the corresponding major version like 1.5.
func majorOf(v history.Version) history.Version {
return history.Version{X: v.X, Y: v.Y, Z: 0}
func majorOf(v history.GoVer) history.GoVer {
return history.GoVer{X: v.X, Y: v.Y, Z: 0}
}

type releaseTemplateData struct {
Expand All @@ -125,20 +125,13 @@ type Major struct {

// Release represents a Go release entry as displayed on the release history page.
type Release struct {
ver history.Version
ver history.GoVer
rel history.Release
}

// V returns the Go release version string, like "1.14", "1.14.1", "1.14.2", etc.
func (r Release) V() string {
switch {
case r.ver.Z != 0:
return fmt.Sprintf("%d.%d.%d", r.ver.X, r.ver.Y, r.ver.Z)
case r.ver.Y != 0:
return fmt.Sprintf("%d.%d", r.ver.X, r.ver.Y)
default:
return fmt.Sprintf("%d", r.ver.X)
}
return r.ver.String()
}

// Date returns the date of the release, formatted for display on the release history page.
Expand Down
38 changes: 30 additions & 8 deletions internal/history/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package history

import (
"fmt"
"html/template"
"time"
)
Expand All @@ -29,7 +30,7 @@ type Release struct {
//
// It contains entries for releases of Go 1.9 and newer.
// Older releases are listed in doc/devel/release.html.
var Releases = map[Version]Release{
var Releases = map[GoVer]Release{
{1, 14, 2}: {
Date: Date{2020, 4, 8},

Expand Down Expand Up @@ -457,20 +458,41 @@ of non-Git repositories under certain conditions.`,
},
}

// Version represents the version of a Go release.
type Version struct {
X int // 1 or higher.
Y int // 0 or higher.
Z int // 0 or higher.
// GoVer represents a Go release version.
//
// In contrast to Semantic Versioning 2.0.0,
// trailing zero components are omitted,
// a version like Go 1.14 is considered a major Go release,
// a version like Go 1.14.1 is considered a minor Go release.
//
// See proposal golang.org/issue/32450 for background, details,
// and a discussion of the costs involved in making a change.
type GoVer struct {
X int // X is the 1st component of a Go X.Y.Z version. It must be 1 or higher.
Y int // Y is the 2nd component of a Go X.Y.Z version. It must be 0 or higher.
Z int // Z is the 3rd component of a Go X.Y.Z version. It must be 0 or higher.
}

// String returns the Go release version string,
// like "1.14", "1.14.1", "1.14.2", and so on.
func (v GoVer) String() string {
switch {
case v.Z != 0:
return fmt.Sprintf("%d.%d.%d", v.X, v.Y, v.Z)
case v.Y != 0:
return fmt.Sprintf("%d.%d", v.X, v.Y)
default:
return fmt.Sprintf("%d", v.X)
}
}

// IsMajor reports whether version v is considered to be a major Go release.
// For example, Go 1.14 and 1.13 are major Go releases.
func (v Version) IsMajor() bool { return v.Z == 0 }
func (v GoVer) IsMajor() bool { return v.Z == 0 }

// IsMinor reports whether version v is considered to be a minor Go release.
// For example, Go 1.14.1 and 1.13.9 are minor Go releases.
func (v Version) IsMinor() bool { return v.Z != 0 }
func (v GoVer) IsMinor() bool { return v.Z != 0 }

// Date represents the date (year, month, day) of a Go release.
//
Expand Down

0 comments on commit 19fded7

Please sign in to comment.