Add shared metadata maps and stage-name helpers
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -28,6 +29,15 @@ type Schema struct {
|
||||
SHA256 string `json:"sha256"`
|
||||
}
|
||||
|
||||
func (s Schema) DiagnosticsMap() map[string]any {
|
||||
return map[string]any{
|
||||
"id": s.ID,
|
||||
"version": s.Version,
|
||||
"name": s.Name,
|
||||
"sha256": s.SHA256,
|
||||
}
|
||||
}
|
||||
|
||||
var registry = map[Key]Schema{
|
||||
CorrectionSetKey: mustBuildSchema(
|
||||
correctionSetSchemaID,
|
||||
@@ -43,6 +53,20 @@ var registry = map[Key]Schema{
|
||||
),
|
||||
}
|
||||
|
||||
func Registered() []Schema {
|
||||
keys := make([]string, 0, len(registry))
|
||||
for key := range registry {
|
||||
keys = append(keys, string(key))
|
||||
}
|
||||
sort.Strings(keys)
|
||||
|
||||
out := make([]Schema, 0, len(keys))
|
||||
for _, key := range keys {
|
||||
out = append(out, cloneSchema(registry[Key(key)]))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Lookup returns a copy of the registered schema for the provided key.
|
||||
func Lookup(key Key) (Schema, bool) {
|
||||
schema, ok := registry[key]
|
||||
|
||||
@@ -89,3 +89,20 @@ func TestLookupReturnsSchemaCopy(t *testing.T) {
|
||||
t.Fatalf("expected lookup to return independent schema copy")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiagnosticsMapIncludesStableSchemaMetadataShapeForAllSchemas(t *testing.T) {
|
||||
registered := Registered()
|
||||
if len(registered) == 0 {
|
||||
t.Fatalf("expected registered response schemas")
|
||||
}
|
||||
|
||||
for _, schema := range registered {
|
||||
metadataMap := schema.DiagnosticsMap()
|
||||
if metadataMap["id"] != schema.ID ||
|
||||
metadataMap["version"] != schema.Version ||
|
||||
metadataMap["name"] != schema.Name ||
|
||||
metadataMap["sha256"] != schema.SHA256 {
|
||||
t.Fatalf("unexpected diagnostics metadata map for %q: %+v", schema.ID, metadataMap)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user