150 lines
4.4 KiB
Go
150 lines
4.4 KiB
Go
package npcs
|
|
|
|
import (
|
|
"bytes"
|
|
"embed"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/npcs/identity"
|
|
)
|
|
|
|
const (
|
|
SchemaID = "notarius.dnd.npcs"
|
|
SchemaName = "notarius_dnd_npcs_v1"
|
|
SchemaVersion = "v1"
|
|
MediaType = "application/json"
|
|
)
|
|
|
|
//go:embed assets/schemas/dnd_npcs.v1.json
|
|
var schemaAssets embed.FS
|
|
|
|
var _ contracts.ArtifactCodec[dnd.NPCList] = (*Codec)(nil)
|
|
|
|
type Codec struct{}
|
|
|
|
func New() *Codec { return &Codec{} }
|
|
|
|
func (c *Codec) Kind() contracts.ArtifactKind { return dnd.NPCListKind }
|
|
|
|
func (c *Codec) Schema() contracts.ArtifactSchema {
|
|
raw, err := schemaAssets.ReadFile("assets/schemas/dnd_npcs.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.NPCList) map[string]any {
|
|
return map[string]any{"npc_count": len(value.NPCs)}
|
|
}
|
|
|
|
func (c *Codec) Encode(value dnd.NPCList) ([]byte, error) {
|
|
if err := validate(value); err != nil {
|
|
return nil, fmt.Errorf("encode dnd npc 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.NPCList) ([]byte, error) {
|
|
content, err := json.Marshal(value)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("encode dnd npc list: %w", err)
|
|
}
|
|
return content, nil
|
|
}
|
|
|
|
func (c *Codec) Decode(content []byte) (dnd.NPCList, error) {
|
|
value, err := c.DecodeCandidate(content)
|
|
if err != nil {
|
|
return dnd.NPCList{}, err
|
|
}
|
|
if err := validate(value); err != nil {
|
|
return dnd.NPCList{}, fmt.Errorf("decode dnd npc 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.NPCList, error) {
|
|
decoder := json.NewDecoder(bytes.NewReader(content))
|
|
decoder.DisallowUnknownFields()
|
|
var value dnd.NPCList
|
|
if err := decoder.Decode(&value); err != nil {
|
|
return dnd.NPCList{}, fmt.Errorf("decode dnd npc list: %w", err)
|
|
}
|
|
var trailing any
|
|
if err := decoder.Decode(&trailing); err != io.EOF {
|
|
return dnd.NPCList{}, fmt.Errorf("decode dnd npc list: multiple JSON values")
|
|
}
|
|
return value, nil
|
|
}
|
|
|
|
func validate(value dnd.NPCList) error {
|
|
if value.NPCs == nil {
|
|
return fmt.Errorf("npcs must be present")
|
|
}
|
|
for index, npc := range value.NPCs {
|
|
prefix := fmt.Sprintf("npcs[%d]", index)
|
|
if !identity.IsValidID(npc.ID) {
|
|
return fmt.Errorf("%s.id must match npc ID pattern", prefix)
|
|
}
|
|
if strings.TrimSpace(npc.Name) == "" {
|
|
return fmt.Errorf("%s.name must not be empty", prefix)
|
|
}
|
|
if npc.Aliases == nil {
|
|
return fmt.Errorf("%s.aliases must be present", prefix)
|
|
}
|
|
for aliasIndex, alias := range npc.Aliases {
|
|
if strings.TrimSpace(alias) == "" {
|
|
return fmt.Errorf("%s.aliases[%d] must not be empty", prefix, aliasIndex)
|
|
}
|
|
}
|
|
if strings.TrimSpace(npc.Description) == "" {
|
|
return fmt.Errorf("%s.description must not be empty", prefix)
|
|
}
|
|
if npc.Relationships == nil {
|
|
return fmt.Errorf("%s.relationships must be present", prefix)
|
|
}
|
|
for relationshipIndex, relationship := range npc.Relationships {
|
|
relationshipPrefix := fmt.Sprintf("%s.relationships[%d]", prefix, relationshipIndex)
|
|
if strings.TrimSpace(relationship.Target) == "" {
|
|
return fmt.Errorf("%s.target must not be empty", relationshipPrefix)
|
|
}
|
|
if strings.TrimSpace(relationship.Relationship) == "" {
|
|
return fmt.Errorf("%s.relationship must not be empty", relationshipPrefix)
|
|
}
|
|
}
|
|
if len(npc.SourceRefs) == 0 {
|
|
return fmt.Errorf("%s.source_refs must not be empty", prefix)
|
|
}
|
|
for refIndex, ref := range npc.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
|
|
}
|