Centralize D&D candidate JSON codecs

This commit is contained in:
2026-07-25 13:05:02 +00:00
parent 8199d95dc1
commit d752c51aec
7 changed files with 100 additions and 95 deletions

View File

@@ -1,15 +1,13 @@
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/codec/candidatejson"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/npcs/identity"
)
@@ -60,11 +58,7 @@ func (c *Codec) Encode(value dnd.NPCList) ([]byte, error) {
// 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
return candidatejson.EncodeCandidate("dnd npc list", value)
}
func (c *Codec) Decode(content []byte) (dnd.NPCList, error) {
@@ -81,17 +75,7 @@ func (c *Codec) Decode(content []byte) (dnd.NPCList, error) {
// 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
return candidatejson.DecodeCandidate[dnd.NPCList]("dnd npc list", content)
}
func validate(value dnd.NPCList) error {