Resolve source and diagnostic release identities

This commit is contained in:
2026-12-05 20:57:10 +00:00
parent b0fce88a51
commit 0f9923d230
11 changed files with 279 additions and 57 deletions

View File

@@ -1,25 +1,71 @@
package versioncontract
import "strings"
import (
"strconv"
"strings"
"time"
)
// ValidStableVersion reports whether version uses the selected vMAJOR.MINOR.PATCH form.
func ValidStableVersion(version string) bool {
if !strings.HasPrefix(version, "v") {
return ValidIdentifier(version, "semver", "v")
}
// ValidIdentifier checks a production identifier without changing its spelling.
func ValidIdentifier(identifier, format, prefix string) bool {
if (prefix != "" && prefix != "v") || !strings.HasPrefix(identifier, prefix) {
return false
}
components := strings.Split(version[1:], ".")
if len(components) != 3 {
return false
}
for _, component := range components {
if component == "" || len(component) > 1 && component[0] == '0' {
value := strings.TrimPrefix(identifier, prefix)
switch format {
case "semver":
components := strings.Split(value, ".")
return len(components) == 3 && decimal(components[0], true) && decimal(components[1], true) && decimal(components[2], true)
case "date":
parts := strings.Split(value, ".")
if len(parts) > 2 || (len(parts) == 2 && (!decimal(parts[1], true) || parts[1] == "0")) {
return false
}
for i := 0; i < len(component); i++ {
if component[i] < '0' || component[i] > '9' {
return false
}
date := strings.Split(parts[0], "-")
if len(date) != 3 || len(date[0]) != 4 || len(date[1]) != 2 || len(date[2]) != 2 {
return false
}
year, month, ok := yearMonth(date[0], date[1])
if !ok || !decimal(date[2], false) {
return false
}
day, _ := strconv.Atoi(date[2])
parsed := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
return parsed.Year() == year && int(parsed.Month()) == month && parsed.Day() == day
case "year-month":
parts := strings.Split(value, ".")
if len(parts) != 3 || len(parts[0]) != 4 || len(parts[1]) != 2 || !decimal(parts[2], true) {
return false
}
_, _, ok := yearMonth(parts[0], parts[1])
return ok
default:
return false
}
}
func decimal(value string, canonical bool) bool {
if value == "" || (canonical && len(value) > 1 && value[0] == '0') {
return false
}
for i := 0; i < len(value); i++ {
if value[i] < '0' || value[i] > '9' {
return false
}
}
return true
}
func yearMonth(yearText, monthText string) (int, int, bool) {
if !decimal(yearText, false) || !decimal(monthText, false) {
return 0, 0, false
}
year, _ := strconv.Atoi(yearText)
month, _ := strconv.Atoi(monthText)
return year, month, year >= 1 && year <= 9999 && month >= 1 && month <= 12
}

View File

@@ -0,0 +1,45 @@
package versioncontract
import "testing"
func TestValidIdentifier(t *testing.T) {
tests := []struct {
format, prefix, value string
want bool
}{
{"semver", "v", "v1.2.3", true},
{"semver", "", "123456789012345678901234567890.0.3", true},
{"semver", "v", "1.2.3", false},
{"semver", "", "v1.2.3", false},
{"semver", "v", "v1.02.3", false},
{"semver", "v", "v1.2.3-rc.1", false},
{"date", "", "2024-02-29", true},
{"date", "v", "v2000-02-29.123456789012345678901234567890", true},
{"date", "", "1900-02-29", false},
{"date", "", "2023-02-29", false},
{"date", "", "0000-01-01", false},
{"date", "", "0001-01-01", true},
{"date", "", "9999-12-31", true},
{"date", "", "2024-2-29", false},
{"date", "", "2024-02-00", false},
{"date", "", "2024-02-29.0", false},
{"date", "", "2024-02-29.01", false},
{"date", "", "2024-02-29.1.2", false},
{"year-month", "", "2024.02.0", true},
{"year-month", "v", "v2024.12.123456789012345678901234567890", true},
{"year-month", "", "2024.00.0", false},
{"year-month", "", "2024.13.0", false},
{"year-month", "", "0000.01.0", false},
{"year-month", "", "2024.1.0", false},
{"year-month", "", "2024.01.00", false},
{"year-month", "", "2024.01", false},
{"date", "", "٢٠٢٤-02-29", false},
}
for _, tt := range tests {
t.Run(tt.format+"/"+tt.prefix+"/"+tt.value, func(t *testing.T) {
if got := ValidIdentifier(tt.value, tt.format, tt.prefix); got != tt.want {
t.Fatalf("ValidIdentifier(%q, %q, %q) = %v, want %v", tt.value, tt.format, tt.prefix, got, tt.want)
}
})
}
}