Migrate NPC registry durable contract
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$id": "notarius.dnd.npcs",
|
||||
"$id": "notarius.dnd.npc_registry",
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["npcs"],
|
||||
@@ -12,25 +12,25 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
SchemaID = "notarius.dnd.npcs"
|
||||
SchemaName = "notarius_dnd_npcs_v1"
|
||||
SchemaID = "notarius.dnd.npc_registry"
|
||||
SchemaName = "notarius_dnd_npc_registry_v1"
|
||||
SchemaVersion = "v1"
|
||||
MediaType = "application/json"
|
||||
)
|
||||
|
||||
//go:embed assets/schemas/dnd_npcs.v1.json
|
||||
//go:embed assets/schemas/dnd_npc_registry.v1.json
|
||||
var schemaAssets embed.FS
|
||||
|
||||
var _ contracts.ArtifactCodec[dnd.NPCList] = (*Codec)(nil)
|
||||
var _ contracts.ArtifactCodec[dnd.NPCRegistry] = (*Codec)(nil)
|
||||
|
||||
type Codec struct{}
|
||||
|
||||
func New() *Codec { return &Codec{} }
|
||||
|
||||
func (c *Codec) Kind() contracts.ArtifactKind { return dnd.NPCListKind }
|
||||
func (c *Codec) Kind() contracts.ArtifactKind { return dnd.NPCRegistryKind }
|
||||
|
||||
func (c *Codec) Schema() contracts.ArtifactSchema {
|
||||
raw, err := schemaAssets.ReadFile("assets/schemas/dnd_npcs.v1.json")
|
||||
raw, err := schemaAssets.ReadFile("assets/schemas/dnd_npc_registry.v1.json")
|
||||
if err != nil {
|
||||
return contracts.ArtifactSchema{}
|
||||
}
|
||||
@@ -44,41 +44,41 @@ func (c *Codec) Schema() contracts.ArtifactSchema {
|
||||
|
||||
func (c *Codec) MediaType() string { return MediaType }
|
||||
|
||||
func (c *Codec) Metadata(value dnd.NPCList) map[string]any {
|
||||
func (c *Codec) Metadata(value dnd.NPCRegistry) map[string]any {
|
||||
return map[string]any{"npc_count": len(value.NPCs)}
|
||||
}
|
||||
|
||||
func (c *Codec) Encode(value dnd.NPCList) ([]byte, error) {
|
||||
func (c *Codec) Encode(value dnd.NPCRegistry) ([]byte, error) {
|
||||
if err := validate(value); err != nil {
|
||||
return nil, fmt.Errorf("encode dnd npc list: %w", err)
|
||||
return nil, fmt.Errorf("encode dnd npc 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.NPCList) ([]byte, error) {
|
||||
return candidatejson.EncodeCandidate("dnd npc list", value)
|
||||
func (c *Codec) EncodeCandidate(value dnd.NPCRegistry) ([]byte, error) {
|
||||
return candidatejson.EncodeCandidate("dnd npc registry", value)
|
||||
}
|
||||
|
||||
func (c *Codec) Decode(content []byte) (dnd.NPCList, error) {
|
||||
func (c *Codec) Decode(content []byte) (dnd.NPCRegistry, error) {
|
||||
value, err := c.DecodeCandidate(content)
|
||||
if err != nil {
|
||||
return dnd.NPCList{}, err
|
||||
return dnd.NPCRegistry{}, err
|
||||
}
|
||||
if err := validate(value); err != nil {
|
||||
return dnd.NPCList{}, fmt.Errorf("decode dnd npc list: %w", err)
|
||||
return dnd.NPCRegistry{}, fmt.Errorf("decode dnd npc 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.NPCList, error) {
|
||||
return candidatejson.DecodeCandidate[dnd.NPCList]("dnd npc list", content)
|
||||
func (c *Codec) DecodeCandidate(content []byte) (dnd.NPCRegistry, error) {
|
||||
return candidatejson.DecodeCandidate[dnd.NPCRegistry]("dnd npc registry", content)
|
||||
}
|
||||
|
||||
func validate(value dnd.NPCList) error {
|
||||
func validate(value dnd.NPCRegistry) error {
|
||||
if value.NPCs == nil {
|
||||
return fmt.Errorf("npcs must be present")
|
||||
}
|
||||
|
||||
@@ -15,8 +15,8 @@ import (
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/npcs/identity"
|
||||
)
|
||||
|
||||
func validList() dnd.NPCList {
|
||||
return dnd.NPCList{NPCs: []dnd.NPC{{
|
||||
func validList() dnd.NPCRegistry {
|
||||
return dnd.NPCRegistry{NPCs: []dnd.NPC{{
|
||||
ID: identity.DeriveID("Mira Thorn"),
|
||||
Name: "Mira Thorn",
|
||||
SourceRefs: []source.SourceRef{{SourceID: "session-alpha", StartUnitID: 1, EndUnitID: 2}},
|
||||
@@ -24,7 +24,7 @@ func validList() dnd.NPCList {
|
||||
}
|
||||
|
||||
func TestCodecMatchesMaintainedDurableFixture(t *testing.T) {
|
||||
raw, err := os.ReadFile("testdata/dnd_npcs.v1.json")
|
||||
raw, err := os.ReadFile("testdata/dnd_npc_registry.v1.json")
|
||||
if err != nil {
|
||||
t.Fatalf("read durable fixture: %v", err)
|
||||
}
|
||||
@@ -53,7 +53,7 @@ func TestCodecMatchesMaintainedDurableFixture(t *testing.T) {
|
||||
func TestCodecOwnsDurableSchemaAndRegistersExactType(t *testing.T) {
|
||||
codec := New()
|
||||
schema := codec.Schema()
|
||||
if codec.Kind() != dnd.NPCListKind || codec.MediaType() != MediaType {
|
||||
if codec.Kind() != dnd.NPCRegistryKind || 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 TestCodecOwnsDurableSchemaAndRegistersExactType(t *testing.T) {
|
||||
if err := pipeline.RegisterArtifactCodec(registry, codec); err != nil {
|
||||
t.Fatalf("RegisterArtifactCodec() error = %v", err)
|
||||
}
|
||||
spec, ok := registry.Spec(dnd.NPCListKind)
|
||||
spec, ok := registry.Spec(dnd.NPCRegistryKind)
|
||||
if !ok || spec.SchemaDigest != contracts.DigestArtifactSchema(schema) {
|
||||
t.Fatalf("registered spec = %#v, %t", spec, ok)
|
||||
}
|
||||
@@ -98,7 +98,7 @@ func TestCodecStrictlyRejectsMalformedOrUnknownJSON(t *testing.T) {
|
||||
|
||||
func TestCodecCandidatePreservesInvalidTypedValues(t *testing.T) {
|
||||
codec := New()
|
||||
candidate := dnd.NPCList{NPCs: []dnd.NPC{{Name: "Mira Thorn", SourceRefs: []source.SourceRef{}}}}
|
||||
candidate := dnd.NPCRegistry{NPCs: []dnd.NPC{{Name: "Mira Thorn", SourceRefs: []source.SourceRef{}}}}
|
||||
content, err := codec.EncodeCandidate(candidate)
|
||||
if err != nil || !json.Valid(content) {
|
||||
t.Fatalf("EncodeCandidate() = %s, %v; want JSON", content, err)
|
||||
@@ -116,11 +116,11 @@ func TestCodecRejectsEveryRequiredShapeBoundary(t *testing.T) {
|
||||
base := validList().NPCs[0]
|
||||
tests := []struct {
|
||||
name string
|
||||
value dnd.NPCList
|
||||
value dnd.NPCRegistry
|
||||
want string
|
||||
}{
|
||||
{name: "blank name", value: dnd.NPCList{NPCs: []dnd.NPC{{ID: base.ID, Name: " ", SourceRefs: base.SourceRefs}}}, want: "name must not be empty"},
|
||||
{name: "empty source refs", value: dnd.NPCList{NPCs: []dnd.NPC{{ID: base.ID, Name: base.Name, SourceRefs: []source.SourceRef{}}}}, want: "source_refs must not be empty"},
|
||||
{name: "blank name", value: dnd.NPCRegistry{NPCs: []dnd.NPC{{ID: base.ID, Name: " ", SourceRefs: base.SourceRefs}}}, want: "name must not be empty"},
|
||||
{name: "empty source refs", value: dnd.NPCRegistry{NPCs: []dnd.NPC{{ID: base.ID, Name: base.Name, SourceRefs: []source.SourceRef{}}}}, want: "source_refs must not be empty"},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"npcs": [
|
||||
{
|
||||
"id": "npc:sha256:99a16589618a04f535a7d21fdcc71a0b1c05d22f752cd492065b1086d97bc3d7",
|
||||
"id": "npc:sha256:35ba5f679aee69e07ae3bd65c44278f29539d5dc9bb5225db1c0060555b23221",
|
||||
"name": "Mira Thorn",
|
||||
"source_refs": [{"source_id": "session-alpha", "start_unit_id": 1, "end_unit_id": 2}]
|
||||
}
|
||||
Reference in New Issue
Block a user