Select the most current local schema version by name
This commit is contained in:
@@ -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