147 lines
4.6 KiB
Go
147 lines
4.6 KiB
Go
// 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 (
|
|
SchemaID = "notarius.dnd.item_events"
|
|
SchemaName = "notarius_dnd_item_events_v1"
|
|
SchemaVersion = "v1"
|
|
MediaType = "application/json"
|
|
)
|
|
|
|
//go:embed assets/schemas/dnd_item_events.v1.json
|
|
var schemaAssets embed.FS
|
|
|
|
var _ contracts.ArtifactCodec[dnd.ItemEventList] = (*Codec)(nil)
|
|
|
|
type Codec struct{}
|
|
|
|
func New() *Codec { return &Codec{} }
|
|
|
|
func (c *Codec) Kind() contracts.ArtifactKind { return dnd.ItemEventListKind }
|
|
|
|
func (c *Codec) Schema() contracts.ArtifactSchema {
|
|
raw, err := schemaAssets.ReadFile("assets/schemas/dnd_item_events.v1.json")
|
|
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 }
|
|
|
|
func (c *Codec) Metadata(value dnd.ItemEventList) map[string]any {
|
|
return map[string]any{"event_count": len(value.Events)}
|
|
}
|
|
|
|
func (c *Codec) Encode(value dnd.ItemEventList) ([]byte, error) {
|
|
if err := validate(value); err != nil {
|
|
return nil, fmt.Errorf("encode dnd item event 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) Decode(content []byte) (dnd.ItemEventList, error) {
|
|
value, err := c.DecodeCandidate(content)
|
|
if err != nil {
|
|
return dnd.ItemEventList{}, err
|
|
}
|
|
if err := validate(value); err != nil {
|
|
return dnd.ItemEventList{}, fmt.Errorf("decode dnd item event 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)
|
|
if err != nil {
|
|
return dnd.ItemEventList{}, err
|
|
}
|
|
return cloneList(value), nil
|
|
}
|
|
|
|
func validate(value dnd.ItemEventList) error {
|
|
if value.Events == nil {
|
|
return fmt.Errorf("events must be present")
|
|
}
|
|
for index, event := range value.Events {
|
|
prefix := fmt.Sprintf("events[%d]", index)
|
|
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
|
|
}
|
|
|
|
func cloneList(value dnd.ItemEventList) dnd.ItemEventList {
|
|
if value.Events == nil {
|
|
return dnd.ItemEventList{}
|
|
}
|
|
cloned := dnd.ItemEventList{Events: make([]dnd.ItemEvent, len(value.Events))}
|
|
for index, event := range value.Events {
|
|
cloned.Events[index] = event
|
|
if event.Quantity != nil {
|
|
quantity := *event.Quantity
|
|
cloned.Events[index].Quantity = &quantity
|
|
}
|
|
if event.SourceRefs != nil {
|
|
cloned.Events[index].SourceRefs = append([]source.SourceRef(nil), event.SourceRefs...)
|
|
}
|
|
}
|
|
return cloned
|
|
}
|