Adopt registry-backed item occurrences

This commit is contained in:
2026-08-05 19:55:34 +00:00
parent f91237e9c0
commit 3dfefd0e14
46 changed files with 996 additions and 1034 deletions

View File

@@ -1,17 +1,18 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "notarius.dnd.item_events",
"$id": "notarius.dnd.item_occurrences",
"type": "object",
"additionalProperties": false,
"required": ["events"],
"required": ["occurrences"],
"properties": {
"events": {
"occurrences": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"required": ["name", "kind", "source_refs"],
"required": ["item_id", "name", "kind", "source_refs"],
"properties": {
"item_id": {"type": "string", "minLength": 1},
"name": {"type": "string", "minLength": 1},
"kind": {"type": "string", "enum": ["discovered", "acquired", "lost", "consumed", "transferred"]},
"quantity": {"type": "integer", "minimum": 1},

View File

@@ -14,25 +14,25 @@ import (
)
const (
SchemaID = "notarius.dnd.item_events"
SchemaName = "notarius_dnd_item_events_v1"
SchemaID = "notarius.dnd.item_occurrences"
SchemaName = "notarius_dnd_item_occurrences_v1"
SchemaVersion = "v1"
MediaType = "application/json"
)
//go:embed assets/schemas/dnd_item_events.v1.json
//go:embed assets/schemas/dnd_item_occurrences.v1.json
var schemaAssets embed.FS
var _ contracts.ArtifactCodec[dnd.ItemEventList] = (*Codec)(nil)
var _ contracts.ArtifactCodec[dnd.ItemOccurrenceList] = (*Codec)(nil)
type Codec struct{}
func New() *Codec { return &Codec{} }
func (c *Codec) Kind() contracts.ArtifactKind { return dnd.ItemEventListKind }
func (c *Codec) Kind() contracts.ArtifactKind { return dnd.ItemOccurrenceListKind }
func (c *Codec) Schema() contracts.ArtifactSchema {
raw, err := schemaAssets.ReadFile("assets/schemas/dnd_item_events.v1.json")
raw, err := schemaAssets.ReadFile("assets/schemas/dnd_item_occurrences.v1.json")
if err != nil {
return contracts.ArtifactSchema{}
}
@@ -46,50 +46,53 @@ func (c *Codec) Schema() contracts.ArtifactSchema {
func (c *Codec) MediaType() string { return MediaType }
func (c *Codec) Metadata(value dnd.ItemEventList) map[string]any {
return map[string]any{"event_count": len(value.Events)}
func (c *Codec) Metadata(value dnd.ItemOccurrenceList) map[string]any {
return map[string]any{"occurrence_count": len(value.Occurrences)}
}
func (c *Codec) Encode(value dnd.ItemEventList) ([]byte, error) {
func (c *Codec) Encode(value dnd.ItemOccurrenceList) ([]byte, error) {
if err := validate(value); err != nil {
return nil, fmt.Errorf("encode dnd item event list: %w", err)
return nil, fmt.Errorf("encode dnd item occurrence list: %w", err)
}
return c.EncodeCandidate(value)
}
// EncodeCandidate provides the durable representation before semantic
// validators have approved a value.
func (c *Codec) EncodeCandidate(value dnd.ItemEventList) ([]byte, error) {
return candidatejson.EncodeCandidate("dnd item event list", cloneList(value))
func (c *Codec) EncodeCandidate(value dnd.ItemOccurrenceList) ([]byte, error) {
return candidatejson.EncodeCandidate("dnd item occurrence list", cloneList(value))
}
func (c *Codec) Decode(content []byte) (dnd.ItemEventList, error) {
func (c *Codec) Decode(content []byte) (dnd.ItemOccurrenceList, error) {
value, err := c.DecodeCandidate(content)
if err != nil {
return dnd.ItemEventList{}, err
return dnd.ItemOccurrenceList{}, err
}
if err := validate(value); err != nil {
return dnd.ItemEventList{}, fmt.Errorf("decode dnd item event list: %w", err)
return dnd.ItemOccurrenceList{}, fmt.Errorf("decode dnd item occurrence list: %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.ItemEventList, error) {
value, err := candidatejson.DecodeCandidate[dnd.ItemEventList]("dnd item event list", content)
func (c *Codec) DecodeCandidate(content []byte) (dnd.ItemOccurrenceList, error) {
value, err := candidatejson.DecodeCandidate[dnd.ItemOccurrenceList]("dnd item occurrence list", content)
if err != nil {
return dnd.ItemEventList{}, err
return dnd.ItemOccurrenceList{}, err
}
return cloneList(value), nil
}
func validate(value dnd.ItemEventList) error {
if value.Events == nil {
func validate(value dnd.ItemOccurrenceList) error {
if value.Occurrences == nil {
return fmt.Errorf("events must be present")
}
for index, event := range value.Events {
prefix := fmt.Sprintf("events[%d]", index)
for index, event := range value.Occurrences {
prefix := fmt.Sprintf("occurrences[%d]", index)
if strings.TrimSpace(event.ItemID) == "" {
return fmt.Errorf("%s.item_id must not be empty", prefix)
}
if strings.TrimSpace(event.Name) == "" {
return fmt.Errorf("%s.name must not be empty", prefix)
}
@@ -127,19 +130,19 @@ func validate(value dnd.ItemEventList) error {
return nil
}
func cloneList(value dnd.ItemEventList) dnd.ItemEventList {
if value.Events == nil {
return dnd.ItemEventList{}
func cloneList(value dnd.ItemOccurrenceList) dnd.ItemOccurrenceList {
if value.Occurrences == nil {
return dnd.ItemOccurrenceList{}
}
cloned := dnd.ItemEventList{Events: make([]dnd.ItemEvent, len(value.Events))}
for index, event := range value.Events {
cloned.Events[index] = event
cloned := dnd.ItemOccurrenceList{Occurrences: make([]dnd.ItemOccurrence, len(value.Occurrences))}
for index, event := range value.Occurrences {
cloned.Occurrences[index] = event
if event.Quantity != nil {
quantity := *event.Quantity
cloned.Events[index].Quantity = &quantity
cloned.Occurrences[index].Quantity = &quantity
}
if event.SourceRefs != nil {
cloned.Events[index].SourceRefs = append([]source.SourceRef(nil), event.SourceRefs...)
cloned.Occurrences[index].SourceRefs = append([]source.SourceRef(nil), event.SourceRefs...)
}
}
return cloned

View File

@@ -14,14 +14,13 @@ import (
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
)
func validList() dnd.ItemEventList {
func validList() dnd.ItemOccurrenceList {
quantity := 12
return dnd.ItemEventList{Events: []dnd.ItemEvent{
{Name: "Hidden Cache", Kind: dnd.ItemEventKindDiscovered, SourceRefs: refs(1, 1)},
{Name: "Gold Pieces", Kind: dnd.ItemEventKindAcquired, Quantity: &quantity, To: "party", SourceRefs: refs(2, 2)},
{Name: "Torch", Kind: dnd.ItemEventKindLost, From: "party", SourceRefs: refs(3, 3)},
{Name: "Healing Potion", Kind: dnd.ItemEventKindConsumed, From: "party", SourceRefs: refs(4, 4)},
{Name: "Moonblade", Kind: dnd.ItemEventKindTransferred, From: "Aria", To: "Borin", SourceRefs: refs(5, 5)},
return dnd.ItemOccurrenceList{Occurrences: []dnd.ItemOccurrence{{ItemID: "item", Name: "Hidden Cache", Kind: dnd.ItemOccurrenceKindDiscovered, SourceRefs: refs(1, 1)},
{ItemID: "item", Name: "Gold Pieces", Kind: dnd.ItemOccurrenceKindAcquired, Quantity: &quantity, To: "party", SourceRefs: refs(2, 2)},
{ItemID: "item", Name: "Torch", Kind: dnd.ItemOccurrenceKindLost, From: "party", SourceRefs: refs(3, 3)},
{ItemID: "item", Name: "Healing Potion", Kind: dnd.ItemOccurrenceKindConsumed, From: "party", SourceRefs: refs(4, 4)},
{ItemID: "item", Name: "Moonblade", Kind: dnd.ItemOccurrenceKindTransferred, From: "Aria", To: "Borin", SourceRefs: refs(5, 5)},
}}
}
@@ -42,18 +41,18 @@ func TestCodecRoundTripAndIdentities(t *testing.T) {
}
schema := codec.Schema()
if codec.Kind() != dnd.ItemEventListKind || codec.MediaType() != MediaType || schema.ID != SchemaID || schema.Name != SchemaName || schema.Version != SchemaVersion || !json.Valid(schema.JSONSchema) {
if codec.Kind() != dnd.ItemOccurrenceListKind || codec.MediaType() != MediaType || schema.ID != SchemaID || schema.Name != SchemaName || schema.Version != SchemaVersion || !json.Valid(schema.JSONSchema) {
t.Fatalf("codec identity/schema = %q/%q %#v", codec.Kind(), codec.MediaType(), schema)
}
registry := pipeline.NewArtifactCodecRegistry()
if err := pipeline.RegisterArtifactCodec(registry, codec); err != nil {
t.Fatal(err)
}
spec, ok := registry.Spec(dnd.ItemEventListKind)
spec, ok := registry.Spec(dnd.ItemOccurrenceListKind)
if !ok || spec.SchemaDigest != contracts.DigestArtifactSchema(schema) {
t.Fatalf("registered spec = %#v, %t", spec, ok)
}
if _, err := registry.Encode(dnd.ItemEventListKind, dnd.NPCRegistry{}); err == nil {
if _, err := registry.Encode(dnd.ItemOccurrenceListKind, dnd.NPCRegistry{}); err == nil {
t.Fatal("Encode() error = nil, want exact type rejection")
} else {
var typeErr *pipeline.ArtifactCodecTypeError
@@ -65,13 +64,13 @@ func TestCodecRoundTripAndIdentities(t *testing.T) {
func TestCodecSupportsEmptyListAndPreservesInvalidCandidates(t *testing.T) {
codec := New()
empty := dnd.ItemEventList{Events: []dnd.ItemEvent{}}
if content, err := codec.Encode(empty); err != nil || string(content) != `{"events":[]}` {
empty := dnd.ItemOccurrenceList{Occurrences: []dnd.ItemOccurrence{}}
if content, err := codec.Encode(empty); err != nil || string(content) != `{"occurrences":[]}` {
t.Fatalf("Encode() = %s, %v", content, err)
}
zero := 0
candidate := dnd.ItemEventList{Events: []dnd.ItemEvent{{
Name: " ", Kind: dnd.ItemEventKindTransferred, Quantity: &zero, From: "party", To: "Party",
candidate := dnd.ItemOccurrenceList{Occurrences: []dnd.ItemOccurrence{{
Name: " ", Kind: dnd.ItemOccurrenceKindTransferred, Quantity: &zero, From: "party", To: "Party",
SourceRefs: []source.SourceRef{{SourceID: "", StartUnitID: 0, EndUnitID: -1}},
}}}
content, err := codec.EncodeCandidate(candidate)
@@ -88,15 +87,15 @@ func TestCodecSupportsEmptyListAndPreservesInvalidCandidates(t *testing.T) {
}
func TestCodecRejectsStrictJSONAndApprovedBoundaries(t *testing.T) {
validJSON := `{"events":[{"name":"Ring","kind":"acquired","to":"party","source_refs":[{"source_id":"session","start_unit_id":1,"end_unit_id":1}]}]}`
validJSON := `{"occurrences":[{"item_id":"ring","name":"Ring","kind":"acquired","to":"party","source_refs":[{"source_id":"session","start_unit_id":1,"end_unit_id":1}]}]}`
tests := []struct {
name, raw, want string
}{
{"malformed", `{`, "decode dnd item event list"},
{"unknown top level", `{"events":[],"unexpected":true}`, "unknown field"},
{"malformed", `{`, "decode dnd item occurrence list"},
{"unknown top level", `{"occurrences":[],"unexpected":true}`, "unknown field"},
{"unknown event field", strings.Replace(validJSON, `"to":"party"`, `"to":"party","unexpected":true`, 1), "unknown field"},
{"unknown reference field", strings.Replace(validJSON, `"end_unit_id":1`, `"end_unit_id":1,"unexpected":true`, 1), "unknown field"},
{"trailing", `{"events":[]} {}`, "multiple JSON values"},
{"trailing", `{"occurrences":[]} {}`, "multiple JSON values"},
{"missing list", `{}`, "events must be present"},
{"zero quantity", strings.Replace(validJSON, `"to":"party"`, `"quantity":0,"to":"party"`, 1), "quantity must be positive"},
{"negative quantity", strings.Replace(validJSON, `"to":"party"`, `"quantity":-1,"to":"party"`, 1), "quantity must be positive"},
@@ -125,13 +124,13 @@ func TestCodecDeepCopiesBoundaryValuesAndMetadata(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if decoded.Events[1].Quantity == value.Events[1].Quantity || &decoded.Events[1].SourceRefs[0] == &value.Events[1].SourceRefs[0] {
if decoded.Occurrences[1].Quantity == value.Occurrences[1].Quantity || &decoded.Occurrences[1].SourceRefs[0] == &value.Occurrences[1].SourceRefs[0] {
t.Fatal("DecodeCandidate() retained caller-owned event fields")
}
*decoded.Events[1].Quantity = 99
decoded.Events[1].SourceRefs[0].SourceID = "changed"
if *value.Events[1].Quantity != 12 || value.Events[1].SourceRefs[0].SourceID != "session" {
t.Fatal("decoded item event aliases input")
*decoded.Occurrences[1].Quantity = 99
decoded.Occurrences[1].SourceRefs[0].SourceID = "changed"
if *value.Occurrences[1].Quantity != 12 || value.Occurrences[1].SourceRefs[0].SourceID != "session" {
t.Fatal("decoded item occurrence aliases input")
}
first := codec.Schema()
@@ -141,7 +140,7 @@ func TestCodecDeepCopiesBoundaryValuesAndMetadata(t *testing.T) {
}
metadata := codec.Metadata(value)
metadata["payload"] = bytes.Repeat([]byte("x"), 10)
if next := codec.Metadata(value); len(next) != 1 || next["event_count"] != len(value.Events) {
if next := codec.Metadata(value); len(next) != 1 || next["occurrence_count"] != len(value.Occurrences) {
t.Fatalf("Metadata() = %#v", next)
}
}