112 lines
3.4 KiB
Go
112 lines
3.4 KiB
Go
// Package locations encodes durable D&D location artifacts.
|
|
package locations
|
|
|
|
import (
|
|
"embed"
|
|
"fmt"
|
|
"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/codec/candidatejson"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/locations/identity"
|
|
)
|
|
|
|
const (
|
|
SchemaID = "notarius.dnd.locations"
|
|
SchemaName = "notarius_dnd_locations_v1"
|
|
SchemaVersion = "v1"
|
|
MediaType = "application/json"
|
|
)
|
|
|
|
//go:embed assets/schemas/dnd_locations.v1.json
|
|
var schemaAssets embed.FS
|
|
|
|
var _ contracts.ArtifactCodec[dnd.LocationList] = (*Codec)(nil)
|
|
|
|
type Codec struct{}
|
|
|
|
func New() *Codec { return &Codec{} }
|
|
|
|
func (c *Codec) Kind() contracts.ArtifactKind { return dnd.LocationListKind }
|
|
|
|
func (c *Codec) Schema() contracts.ArtifactSchema {
|
|
raw, err := schemaAssets.ReadFile("assets/schemas/dnd_locations.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.LocationList) map[string]any {
|
|
return map[string]any{"location_count": len(value.Locations)}
|
|
}
|
|
|
|
func (c *Codec) Encode(value dnd.LocationList) ([]byte, error) {
|
|
if err := validate(value); err != nil {
|
|
return nil, fmt.Errorf("encode dnd location 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.LocationList) ([]byte, error) {
|
|
return candidatejson.EncodeCandidate("dnd location list", value)
|
|
}
|
|
|
|
func (c *Codec) Decode(content []byte) (dnd.LocationList, error) {
|
|
value, err := c.DecodeCandidate(content)
|
|
if err != nil {
|
|
return dnd.LocationList{}, err
|
|
}
|
|
if err := validate(value); err != nil {
|
|
return dnd.LocationList{}, fmt.Errorf("decode dnd location 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.LocationList, error) {
|
|
return candidatejson.DecodeCandidate[dnd.LocationList]("dnd location list", content)
|
|
}
|
|
|
|
func validate(value dnd.LocationList) error {
|
|
if value.Locations == nil {
|
|
return fmt.Errorf("locations must be present")
|
|
}
|
|
for index, location := range value.Locations {
|
|
prefix := fmt.Sprintf("locations[%d]", index)
|
|
if !identity.IsValidID(location.ID) {
|
|
return fmt.Errorf("%s.id must match location ID pattern", prefix)
|
|
}
|
|
if strings.TrimSpace(location.Name) == "" {
|
|
return fmt.Errorf("%s.name must not be empty", prefix)
|
|
}
|
|
if len(location.SourceRefs) == 0 {
|
|
return fmt.Errorf("%s.source_refs must contain at least one reference", prefix)
|
|
}
|
|
for refIndex, ref := range location.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
|
|
}
|