This repository has been archived by the owner on Dec 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refs #1199
- Loading branch information
Showing
12 changed files
with
375 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package apiversion | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
) | ||
|
||
// MajorVersion is the current major API Version. | ||
const MajorVersion = 2 | ||
|
||
// MinorVersion is the current minor API Version. | ||
const MinorVersion = 1 | ||
|
||
// APIVersion is the current API Version. | ||
var APIVersion = Format(MajorVersion, MinorVersion) | ||
|
||
// SupportedVersions is a slice of supported versions. | ||
var SupportedVersions []string | ||
|
||
// SupportedVersionsJSON is an JSON array of supported versions. | ||
var SupportedVersionsJSON string | ||
|
||
var regexpAPIVersion = regexp.MustCompile(`^v(\d+)\.(\d+)$`) | ||
|
||
func init() { | ||
for i := 0; i <= MinorVersion; i++ { | ||
SupportedVersions = append(SupportedVersions, Format(MajorVersion, i)) | ||
} | ||
bytes, err := json.Marshal(SupportedVersions) | ||
if err != nil { | ||
panic(err) | ||
} | ||
SupportedVersionsJSON = string(bytes) | ||
} | ||
|
||
// Format formats major and minor into `v<major>.<minor>`. | ||
func Format(major, minor int) string { | ||
return fmt.Sprintf("v%d.%d", major, minor) | ||
} | ||
|
||
// Parse parses API version into major and minor. | ||
func Parse(apiVersion string) (major int, minor int, ok bool) { | ||
output := regexpAPIVersion.FindAllStringSubmatch(apiVersion, -1) | ||
if len(output) <= 0 { | ||
return | ||
} | ||
major, _ = strconv.Atoi(output[0][1]) | ||
minor, _ = strconv.Atoi(output[0][2]) | ||
ok = true | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package apiversion | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestFormat(t *testing.T) { | ||
Convey("Format", t, func() { | ||
So(Format(1, 2), ShouldEqual, "v1.2") | ||
}) | ||
} | ||
|
||
func TestParse(t *testing.T) { | ||
Convey("Parse", t, func() { | ||
var major, minor int | ||
var ok bool | ||
|
||
major, minor, ok = Parse("v1.2") | ||
So(ok, ShouldBeTrue) | ||
So(major, ShouldEqual, 1) | ||
So(minor, ShouldEqual, 2) | ||
|
||
_, _, ok = Parse("v1.2 ") | ||
So(ok, ShouldBeFalse) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.