Centralize D&D candidate JSON codecs
This commit is contained in:
@@ -1,15 +1,13 @@
|
||||
package scenedescriptions
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -59,11 +57,7 @@ func (c *Codec) Encode(value dnd.SceneDescriptionList) ([]byte, error) {
|
||||
// EncodeCandidate provides the durable representation before typed validators
|
||||
// have approved a value.
|
||||
func (c *Codec) EncodeCandidate(value dnd.SceneDescriptionList) ([]byte, error) {
|
||||
content, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("encode dnd scene description list: %w", err)
|
||||
}
|
||||
return content, nil
|
||||
return candidatejson.EncodeCandidate("dnd scene description list", value)
|
||||
}
|
||||
|
||||
func (c *Codec) Decode(content []byte) (dnd.SceneDescriptionList, error) {
|
||||
@@ -80,17 +74,7 @@ func (c *Codec) Decode(content []byte) (dnd.SceneDescriptionList, error) {
|
||||
// DecodeCandidate reads one strict durable JSON value before typed validators
|
||||
// have approved it.
|
||||
func (c *Codec) DecodeCandidate(content []byte) (dnd.SceneDescriptionList, error) {
|
||||
decoder := json.NewDecoder(bytes.NewReader(content))
|
||||
decoder.DisallowUnknownFields()
|
||||
var value dnd.SceneDescriptionList
|
||||
if err := decoder.Decode(&value); err != nil {
|
||||
return dnd.SceneDescriptionList{}, fmt.Errorf("decode dnd scene description list: %w", err)
|
||||
}
|
||||
var trailing any
|
||||
if err := decoder.Decode(&trailing); err != io.EOF {
|
||||
return dnd.SceneDescriptionList{}, fmt.Errorf("decode dnd scene description list: multiple JSON values")
|
||||
}
|
||||
return value, nil
|
||||
return candidatejson.DecodeCandidate[dnd.SceneDescriptionList]("dnd scene description list", content)
|
||||
}
|
||||
|
||||
func validate(value dnd.SceneDescriptionList) error {
|
||||
|
||||
Reference in New Issue
Block a user