Adopt location registry durable contract

This commit is contained in:
2026-08-05 19:05:42 +00:00
parent 2f61118e78
commit c006b163d5
41 changed files with 196 additions and 196 deletions

View File

@@ -1,7 +1,7 @@
{
"occurrences": [
{
"location_id": "location:sha256:cdd2b57615b56a4e92d506cf053cd3985e11d4df4895c5383b30ec2ce9f39ed0",
"location_id": "location:sha256:4ea39088943a692f120ae8740419c3baa1e7026c0e2a26ab03255f0b7e20215c",
"name": "The Old Tavern",
"kind": "visited",
"source_refs": [

View File

@@ -1,6 +1,6 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "notarius.dnd.locations",
"$id": "notarius.dnd.location_registry",
"type": "object",
"additionalProperties": false,
"required": ["locations"],

View File

@@ -13,25 +13,25 @@ import (
)
const (
SchemaID = "notarius.dnd.locations"
SchemaName = "notarius_dnd_locations_v1"
SchemaID = "notarius.dnd.location_registry"
SchemaName = "notarius_dnd_location_registry_v1"
SchemaVersion = "v1"
MediaType = "application/json"
)
//go:embed assets/schemas/dnd_locations.v1.json
//go:embed assets/schemas/dnd_location_registry.v1.json
var schemaAssets embed.FS
var _ contracts.ArtifactCodec[dnd.LocationList] = (*Codec)(nil)
var _ contracts.ArtifactCodec[dnd.LocationRegistry] = (*Codec)(nil)
type Codec struct{}
func New() *Codec { return &Codec{} }
func (c *Codec) Kind() contracts.ArtifactKind { return dnd.LocationListKind }
func (c *Codec) Kind() contracts.ArtifactKind { return dnd.LocationRegistryKind }
func (c *Codec) Schema() contracts.ArtifactSchema {
raw, err := schemaAssets.ReadFile("assets/schemas/dnd_locations.v1.json")
raw, err := schemaAssets.ReadFile("assets/schemas/dnd_location_registry.v1.json")
if err != nil {
return contracts.ArtifactSchema{}
}
@@ -45,41 +45,41 @@ func (c *Codec) Schema() contracts.ArtifactSchema {
func (c *Codec) MediaType() string { return MediaType }
func (c *Codec) Metadata(value dnd.LocationList) map[string]any {
func (c *Codec) Metadata(value dnd.LocationRegistry) map[string]any {
return map[string]any{"location_count": len(value.Locations)}
}
func (c *Codec) Encode(value dnd.LocationList) ([]byte, error) {
func (c *Codec) Encode(value dnd.LocationRegistry) ([]byte, error) {
if err := validate(value); err != nil {
return nil, fmt.Errorf("encode dnd location list: %w", err)
return nil, fmt.Errorf("encode dnd location registry: %w", err)
}
return c.EncodeCandidate(value)
}
// EncodeCandidate provides the durable representation before semantic
// validators have approved a value.
func (c *Codec) EncodeCandidate(value dnd.LocationList) ([]byte, error) {
return candidatejson.EncodeCandidate("dnd location list", value)
func (c *Codec) EncodeCandidate(value dnd.LocationRegistry) ([]byte, error) {
return candidatejson.EncodeCandidate("dnd location registry", value)
}
func (c *Codec) Decode(content []byte) (dnd.LocationList, error) {
func (c *Codec) Decode(content []byte) (dnd.LocationRegistry, error) {
value, err := c.DecodeCandidate(content)
if err != nil {
return dnd.LocationList{}, err
return dnd.LocationRegistry{}, err
}
if err := validate(value); err != nil {
return dnd.LocationList{}, fmt.Errorf("decode dnd location list: %w", err)
return dnd.LocationRegistry{}, fmt.Errorf("decode dnd location registry: %w", err)
}
return value, nil
}
// DecodeCandidate reads one strict durable JSON value before semantic
// validators have approved it.
func (c *Codec) DecodeCandidate(content []byte) (dnd.LocationList, error) {
return candidatejson.DecodeCandidate[dnd.LocationList]("dnd location list", content)
func (c *Codec) DecodeCandidate(content []byte) (dnd.LocationRegistry, error) {
return candidatejson.DecodeCandidate[dnd.LocationRegistry]("dnd location registry", content)
}
func validate(value dnd.LocationList) error {
func validate(value dnd.LocationRegistry) error {
if value.Locations == nil {
return fmt.Errorf("locations must be present")
}

View File

@@ -15,9 +15,9 @@ import (
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/locations/identity"
)
func validList() dnd.LocationList {
func validList() dnd.LocationRegistry {
refs := []source.SourceRef{{SourceID: "session-alpha", StartUnitID: 1, EndUnitID: 2}}
return dnd.LocationList{Locations: []dnd.Location{{
return dnd.LocationRegistry{Locations: []dnd.Location{{
ID: identity.DeriveID("The Old Tavern", refs),
Name: "The Old Tavern",
SourceRefs: refs,
@@ -25,7 +25,7 @@ func validList() dnd.LocationList {
}
func TestCodecMatchesMaintainedDurableFixture(t *testing.T) {
raw, err := os.ReadFile("testdata/dnd_locations.v1.json")
raw, err := os.ReadFile("testdata/dnd_location_registry.v1.json")
if err != nil {
t.Fatalf("read durable fixture: %v", err)
}
@@ -53,7 +53,7 @@ func TestCodecMatchesMaintainedDurableFixture(t *testing.T) {
func TestCodecOwnsDurableSchemaAndMetadata(t *testing.T) {
codec := New()
schema := codec.Schema()
if codec.Kind() != dnd.LocationListKind || codec.MediaType() != MediaType {
if codec.Kind() != dnd.LocationRegistryKind || codec.MediaType() != MediaType {
t.Fatalf("codec identity = %q/%q", codec.Kind(), codec.MediaType())
}
if schema.ID != SchemaID || schema.Name != SchemaName || schema.Version != SchemaVersion || !json.Valid(schema.JSONSchema) {
@@ -67,7 +67,7 @@ func TestCodecOwnsDurableSchemaAndMetadata(t *testing.T) {
if err := pipeline.RegisterArtifactCodec(registry, codec); err != nil {
t.Fatalf("RegisterArtifactCodec() error = %v", err)
}
if spec, ok := registry.Spec(dnd.LocationListKind); !ok || spec.SchemaDigest != contracts.DigestArtifactSchema(schema) {
if spec, ok := registry.Spec(dnd.LocationRegistryKind); !ok || spec.SchemaDigest != contracts.DigestArtifactSchema(schema) {
t.Fatalf("registered spec = %#v, %t", spec, ok)
}
first := schema.JSONSchema
@@ -84,11 +84,11 @@ func TestCodecOwnsDurableSchemaAndMetadata(t *testing.T) {
func TestCodecSupportsEmptyListsAndCandidateSemanticFailures(t *testing.T) {
codec := New()
empty := dnd.LocationList{Locations: []dnd.Location{}}
empty := dnd.LocationRegistry{Locations: []dnd.Location{}}
if content, err := codec.Encode(empty); err != nil || string(content) != `{"locations":[]}` {
t.Fatalf("Encode() = %s, %v", content, err)
}
for _, candidate := range []dnd.LocationList{
for _, candidate := range []dnd.LocationRegistry{
{},
empty,
{Locations: []dnd.Location{{ID: "bad", Name: " ", SourceRefs: nil}}},
@@ -109,7 +109,7 @@ func TestCodecSupportsEmptyListsAndCandidateSemanticFailures(t *testing.T) {
func TestCodecRejectsStructuralJSONBeforeSemanticApproval(t *testing.T) {
valid := `{"locations":[{"id":"location:sha256:0000000000000000000000000000000000000000000000000000000000000000","name":"The Tavern","source_refs":[{"source_id":"session","start_unit_id":1,"end_unit_id":1}]}]}`
for _, test := range []struct{ name, raw, want string }{
{"malformed", `{`, "decode dnd location list"},
{"malformed", `{`, "decode dnd location registry"},
{"unknown top-level", `{"locations":[],"unexpected":true}`, "unknown field"},
{"unknown location field", strings.Replace(valid, `"name":"The Tavern"`, `"name":"The Tavern","unexpected":true`, 1), "unknown field"},
{"unknown source reference field", strings.Replace(valid, `"end_unit_id":1`, `"end_unit_id":1,"unexpected":true`, 1), "unknown field"},
@@ -128,14 +128,14 @@ func TestCodecRejectsApprovedShapeBoundaries(t *testing.T) {
base := validList().Locations[0]
for _, test := range []struct {
name string
value dnd.LocationList
value dnd.LocationRegistry
want string
}{
{"nil locations", dnd.LocationList{}, "locations must be present"},
{"invalid ID", dnd.LocationList{Locations: []dnd.Location{{ID: "bad", Name: base.Name, SourceRefs: base.SourceRefs}}}, "id must match location ID pattern"},
{"blank name", dnd.LocationList{Locations: []dnd.Location{{ID: base.ID, Name: " ", SourceRefs: base.SourceRefs}}}, "name must not be empty"},
{"empty source refs", dnd.LocationList{Locations: []dnd.Location{{ID: base.ID, Name: base.Name, SourceRefs: nil}}}, "source_refs must contain"},
{"malformed source reference", dnd.LocationList{Locations: []dnd.Location{{ID: base.ID, Name: base.Name, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 0, EndUnitID: 1}}}}}, "start_unit_id must be positive"},
{"nil locations", dnd.LocationRegistry{}, "locations must be present"},
{"invalid ID", dnd.LocationRegistry{Locations: []dnd.Location{{ID: "bad", Name: base.Name, SourceRefs: base.SourceRefs}}}, "id must match location ID pattern"},
{"blank name", dnd.LocationRegistry{Locations: []dnd.Location{{ID: base.ID, Name: " ", SourceRefs: base.SourceRefs}}}, "name must not be empty"},
{"empty source refs", dnd.LocationRegistry{Locations: []dnd.Location{{ID: base.ID, Name: base.Name, SourceRefs: nil}}}, "source_refs must contain"},
{"malformed source reference", dnd.LocationRegistry{Locations: []dnd.Location{{ID: base.ID, Name: base.Name, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 0, EndUnitID: 1}}}}}, "start_unit_id must be positive"},
} {
t.Run(test.name, func(t *testing.T) {
if _, err := New().Encode(test.value); err == nil || !strings.Contains(err.Error(), test.want) {

View File

@@ -1,7 +1,7 @@
{
"locations": [
{
"id": "location:sha256:cdd2b57615b56a4e92d506cf053cd3985e11d4df4895c5383b30ec2ce9f39ed0",
"id": "location:sha256:4ea39088943a692f120ae8740419c3baa1e7026c0e2a26ab03255f0b7e20215c",
"name": "The Old Tavern",
"source_refs": [
{"source_id": "session-alpha", "start_unit_id": 1, "end_unit_id": 2}