Files
notarius/internal/modules/dnd/codec/itemevents/codec.go

150 lines
4.9 KiB
Go
Raw Normal View History

2026-07-25 21:41:17 +00:00
// Package itemevents encodes durable D&D item-event artifacts.
package itemevents
import (
"embed"
"fmt"
"strings"
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/codec/candidatejson"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/itemevents"
)
const (
2026-08-05 19:55:34 +00:00
SchemaID = "notarius.dnd.item_occurrences"
SchemaName = "notarius_dnd_item_occurrences_v1"
2026-07-25 21:41:17 +00:00
SchemaVersion = "v1"
MediaType = "application/json"
)
2026-08-05 19:55:34 +00:00
//go:embed assets/schemas/dnd_item_occurrences.v1.json
2026-07-25 21:41:17 +00:00
var schemaAssets embed.FS
2026-08-05 19:55:34 +00:00
var _ contracts.ArtifactCodec[dnd.ItemOccurrenceList] = (*Codec)(nil)
2026-07-25 21:41:17 +00:00
type Codec struct{}
func New() *Codec { return &Codec{} }
2026-08-05 19:55:34 +00:00
func (c *Codec) Kind() contracts.ArtifactKind { return dnd.ItemOccurrenceListKind }
2026-07-25 21:41:17 +00:00
func (c *Codec) Schema() contracts.ArtifactSchema {
2026-08-05 19:55:34 +00:00
raw, err := schemaAssets.ReadFile("assets/schemas/dnd_item_occurrences.v1.json")
2026-07-25 21:41:17 +00:00
if err != nil {
return contracts.ArtifactSchema{}
}
return contracts.ArtifactSchema{
ID: SchemaID,
Name: SchemaName,
Version: SchemaVersion,
JSONSchema: append([]byte(nil), raw...),
}
}
func (c *Codec) MediaType() string { return MediaType }
2026-08-05 19:55:34 +00:00
func (c *Codec) Metadata(value dnd.ItemOccurrenceList) map[string]any {
return map[string]any{"occurrence_count": len(value.Occurrences)}
2026-07-25 21:41:17 +00:00
}
2026-08-05 19:55:34 +00:00
func (c *Codec) Encode(value dnd.ItemOccurrenceList) ([]byte, error) {
2026-07-25 21:41:17 +00:00
if err := validate(value); err != nil {
2026-08-05 19:55:34 +00:00
return nil, fmt.Errorf("encode dnd item occurrence list: %w", err)
2026-07-25 21:41:17 +00:00
}
return c.EncodeCandidate(value)
}
// EncodeCandidate provides the durable representation before semantic
// validators have approved a value.
2026-08-05 19:55:34 +00:00
func (c *Codec) EncodeCandidate(value dnd.ItemOccurrenceList) ([]byte, error) {
return candidatejson.EncodeCandidate("dnd item occurrence list", cloneList(value))
2026-07-25 21:41:17 +00:00
}
2026-08-05 19:55:34 +00:00
func (c *Codec) Decode(content []byte) (dnd.ItemOccurrenceList, error) {
2026-07-25 21:41:17 +00:00
value, err := c.DecodeCandidate(content)
if err != nil {
2026-08-05 19:55:34 +00:00
return dnd.ItemOccurrenceList{}, err
2026-07-25 21:41:17 +00:00
}
if err := validate(value); err != nil {
2026-08-05 19:55:34 +00:00
return dnd.ItemOccurrenceList{}, fmt.Errorf("decode dnd item occurrence list: %w", err)
2026-07-25 21:41:17 +00:00
}
return value, nil
}
// DecodeCandidate reads one strict durable JSON value before semantic
// validators have approved it.
2026-08-05 19:55:34 +00:00
func (c *Codec) DecodeCandidate(content []byte) (dnd.ItemOccurrenceList, error) {
value, err := candidatejson.DecodeCandidate[dnd.ItemOccurrenceList]("dnd item occurrence list", content)
2026-07-25 21:41:17 +00:00
if err != nil {
2026-08-05 19:55:34 +00:00
return dnd.ItemOccurrenceList{}, err
2026-07-25 21:41:17 +00:00
}
return cloneList(value), nil
}
2026-08-05 19:55:34 +00:00
func validate(value dnd.ItemOccurrenceList) error {
if value.Occurrences == nil {
2026-07-25 21:41:17 +00:00
return fmt.Errorf("events must be present")
}
2026-08-05 19:55:34 +00:00
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)
}
2026-07-25 21:41:17 +00:00
if strings.TrimSpace(event.Name) == "" {
return fmt.Errorf("%s.name must not be empty", prefix)
}
if !itemevents.SupportedKind(event.Kind) {
return fmt.Errorf("%s.kind must be supported", prefix)
}
if event.From != "" && strings.TrimSpace(event.From) == "" {
return fmt.Errorf("%s.from must not be empty when present", prefix)
}
if event.To != "" && strings.TrimSpace(event.To) == "" {
return fmt.Errorf("%s.to must not be empty when present", prefix)
}
if !itemevents.ValidHolderCombination(event.Kind, event.From, event.To) {
return fmt.Errorf("%s holders are incompatible with %q", prefix, event.Kind)
}
if event.Quantity != nil && *event.Quantity < 1 {
return fmt.Errorf("%s.quantity must be positive when present", prefix)
}
if len(event.SourceRefs) == 0 {
return fmt.Errorf("%s.source_refs must contain at least one reference", prefix)
}
for refIndex, ref := range event.SourceRefs {
refPrefix := fmt.Sprintf("%s.source_refs[%d]", prefix, refIndex)
if strings.TrimSpace(ref.SourceID) == "" {
return fmt.Errorf("%s.source_id must not be empty", refPrefix)
}
if ref.StartUnitID <= 0 {
return fmt.Errorf("%s.start_unit_id must be positive", refPrefix)
}
if ref.EndUnitID <= 0 {
return fmt.Errorf("%s.end_unit_id must be positive", refPrefix)
}
}
}
return nil
}
2026-08-05 19:55:34 +00:00
func cloneList(value dnd.ItemOccurrenceList) dnd.ItemOccurrenceList {
if value.Occurrences == nil {
return dnd.ItemOccurrenceList{}
2026-07-25 21:41:17 +00:00
}
2026-08-05 19:55:34 +00:00
cloned := dnd.ItemOccurrenceList{Occurrences: make([]dnd.ItemOccurrence, len(value.Occurrences))}
for index, event := range value.Occurrences {
cloned.Occurrences[index] = event
2026-07-25 21:41:17 +00:00
if event.Quantity != nil {
quantity := *event.Quantity
2026-08-05 19:55:34 +00:00
cloned.Occurrences[index].Quantity = &quantity
2026-07-25 21:41:17 +00:00
}
if event.SourceRefs != nil {
2026-08-05 19:55:34 +00:00
cloned.Occurrences[index].SourceRefs = append([]source.SourceRef(nil), event.SourceRefs...)
2026-07-25 21:41:17 +00:00
}
}
return cloned
}