Select the most current local schema version by name
This commit is contained in:
@@ -12,7 +12,7 @@ func TestAlignedLevels(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
schema, err := set.Select("rpg/party@0.2.0")
|
||||
schema, err := set.Select("rpg/party")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
46
internal/validation/selection_test.go
Normal file
46
internal/validation/selection_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package validation
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
)
|
||||
|
||||
func TestCurrentSelection(t *testing.T) {
|
||||
files := fstest.MapFS{}
|
||||
catalog := Catalog{FormatVersion: 1}
|
||||
for _, version := range []string{"0.9.0", "0.10.0-rc.1", "0.10.0", "0.1.0", "0.10.0+build"} {
|
||||
path := "sample/v" + version + "/schema.json"
|
||||
record := Record{Name: "sample", Version: version, Contract: "maximumdirect.sample.v1", ID: schemaBase + path, Path: path}
|
||||
catalog.Schemas = append(catalog.Schemas, record)
|
||||
data, err := json.Marshal(map[string]any{"$schema": "https://json-schema.org/draft/2020-12/schema", "$id": record.ID, "type": "object", "properties": map[string]any{"schema_version": map[string]any{"const": record.Contract}}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
files[path] = &fstest.MapFile{Data: data}
|
||||
}
|
||||
data, err := json.Marshal(catalog)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
files["catalog.json"] = &fstest.MapFile{Data: data}
|
||||
set, err := Load(files)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
selected, err := set.Select("sample")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if selected.Record.Version != "0.10.0" {
|
||||
t.Fatalf("selected %s", selected.Record.Version)
|
||||
}
|
||||
if len(set.Current()) != 1 || set.Current()[0] != selected {
|
||||
t.Fatal("selection and discovery disagree")
|
||||
}
|
||||
for _, name := range []string{"", "missing", "sample@0.9.0"} {
|
||||
if _, err := set.Select(name); err == nil {
|
||||
t.Fatalf("invalid selection %q accepted", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/santhosh-tekuri/jsonschema/v6"
|
||||
"golang.org/x/mod/semver"
|
||||
)
|
||||
|
||||
const schemaBase = "https://schemas.maximumdirect.net/"
|
||||
@@ -29,6 +30,7 @@ type Set struct {
|
||||
Catalog Catalog
|
||||
schemas map[string]*Schema
|
||||
selections map[string]string
|
||||
current map[string]*Schema
|
||||
}
|
||||
type Schema struct {
|
||||
Record Record
|
||||
@@ -45,7 +47,7 @@ func Load(root fs.FS) (*Set, error) {
|
||||
if !json.Valid(data) {
|
||||
return nil, fmt.Errorf("catalog.json: expected JSON syntax")
|
||||
}
|
||||
set := &Set{schemas: make(map[string]*Schema), selections: make(map[string]string)}
|
||||
set := &Set{schemas: make(map[string]*Schema), selections: make(map[string]string), current: make(map[string]*Schema)}
|
||||
if err := json.Unmarshal(data, &set.Catalog); err != nil {
|
||||
return nil, fmt.Errorf("catalog.json: %w", err)
|
||||
}
|
||||
@@ -58,18 +60,9 @@ func Load(root fs.FS) (*Set, error) {
|
||||
paths := make(map[string]bool)
|
||||
for i, record := range set.Catalog.Schemas {
|
||||
label := fmt.Sprintf("catalog.json schemas[%d] (%s@%s)", i, record.Name, record.Version)
|
||||
if !fs.ValidPath(record.Name) || !schemaVersion.MatchString(record.Version) {
|
||||
if !fs.ValidPath(record.Name) || !schemaVersion.MatchString(record.Version) || !semver.IsValid("v"+record.Version) {
|
||||
return nil, fmt.Errorf("%s: invalid schema name or SemVer", label)
|
||||
}
|
||||
// Numeric prerelease identifiers have no leading zero under SemVer.
|
||||
pre, _, _ := strings.Cut(record.Version, "+")
|
||||
if _, pre, ok := strings.Cut(pre, "-"); ok {
|
||||
for _, part := range strings.Split(pre, ".") {
|
||||
if len(part) > 1 && part[0] == '0' && strings.Trim(part, "0123456789") == "" {
|
||||
return nil, fmt.Errorf("%s: invalid numeric prerelease identifier", label)
|
||||
}
|
||||
}
|
||||
}
|
||||
expected := record.Name + "/v" + record.Version + "/schema.json"
|
||||
contractPrefix := "maximumdirect." + strings.ReplaceAll(record.Name, "/", ".") + ".v"
|
||||
generation := strings.TrimPrefix(record.Contract, contractPrefix)
|
||||
@@ -114,6 +107,10 @@ func Load(root fs.FS) (*Set, error) {
|
||||
paths[record.Path] = true
|
||||
set.selections[key] = record.ID
|
||||
set.schemas[record.ID] = &Schema{Record: record}
|
||||
previous := set.current[record.Name]
|
||||
if previous == nil || semver.Compare("v"+record.Version, "v"+previous.Record.Version) > 0 {
|
||||
set.current[record.Name] = set.schemas[record.ID]
|
||||
}
|
||||
}
|
||||
// Only version roots at the canonical schema paths are active contracts.
|
||||
err = fs.WalkDir(root, ".", func(file string, entry fs.DirEntry, err error) error {
|
||||
@@ -168,11 +165,25 @@ func (offlineLoader) Load(url string) (any, error) {
|
||||
}
|
||||
|
||||
func (s *Set) Select(selection string) (*Schema, error) {
|
||||
id, ok := s.selections[selection]
|
||||
schema, ok := s.current[selection]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unknown exact schema selection %q; use name@version from catalog.json", selection)
|
||||
return nil, fmt.Errorf("unknown schema name %q; use a name from catalog.json", selection)
|
||||
}
|
||||
return s.schemas[id], nil
|
||||
return schema, nil
|
||||
}
|
||||
|
||||
// Current returns each name's highest local version in stable name order.
|
||||
func (s *Set) Current() []*Schema {
|
||||
names := make([]string, 0, len(s.current))
|
||||
for name := range s.current {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
result := make([]*Schema, 0, len(names))
|
||||
for _, name := range names {
|
||||
result = append(result, s.current[name])
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (s *Schema) Validate(value any) error {
|
||||
|
||||
Reference in New Issue
Block a user