Adopt location registry durable contract
This commit is contained in:
@@ -13,25 +13,25 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
SchemaID = "notarius.dnd.locations"
|
||||
SchemaName = "notarius_dnd_locations_v1"
|
||||
SchemaID = "notarius.dnd.location_registry"
|
||||
SchemaName = "notarius_dnd_location_registry_v1"
|
||||
SchemaVersion = "v1"
|
||||
MediaType = "application/json"
|
||||
)
|
||||
|
||||
//go:embed assets/schemas/dnd_locations.v1.json
|
||||
//go:embed assets/schemas/dnd_location_registry.v1.json
|
||||
var schemaAssets embed.FS
|
||||
|
||||
var _ contracts.ArtifactCodec[dnd.LocationList] = (*Codec)(nil)
|
||||
var _ contracts.ArtifactCodec[dnd.LocationRegistry] = (*Codec)(nil)
|
||||
|
||||
type Codec struct{}
|
||||
|
||||
func New() *Codec { return &Codec{} }
|
||||
|
||||
func (c *Codec) Kind() contracts.ArtifactKind { return dnd.LocationListKind }
|
||||
func (c *Codec) Kind() contracts.ArtifactKind { return dnd.LocationRegistryKind }
|
||||
|
||||
func (c *Codec) Schema() contracts.ArtifactSchema {
|
||||
raw, err := schemaAssets.ReadFile("assets/schemas/dnd_locations.v1.json")
|
||||
raw, err := schemaAssets.ReadFile("assets/schemas/dnd_location_registry.v1.json")
|
||||
if err != nil {
|
||||
return contracts.ArtifactSchema{}
|
||||
}
|
||||
@@ -45,41 +45,41 @@ func (c *Codec) Schema() contracts.ArtifactSchema {
|
||||
|
||||
func (c *Codec) MediaType() string { return MediaType }
|
||||
|
||||
func (c *Codec) Metadata(value dnd.LocationList) map[string]any {
|
||||
func (c *Codec) Metadata(value dnd.LocationRegistry) map[string]any {
|
||||
return map[string]any{"location_count": len(value.Locations)}
|
||||
}
|
||||
|
||||
func (c *Codec) Encode(value dnd.LocationList) ([]byte, error) {
|
||||
func (c *Codec) Encode(value dnd.LocationRegistry) ([]byte, error) {
|
||||
if err := validate(value); err != nil {
|
||||
return nil, fmt.Errorf("encode dnd location list: %w", err)
|
||||
return nil, fmt.Errorf("encode dnd location registry: %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) EncodeCandidate(value dnd.LocationRegistry) ([]byte, error) {
|
||||
return candidatejson.EncodeCandidate("dnd location registry", value)
|
||||
}
|
||||
|
||||
func (c *Codec) Decode(content []byte) (dnd.LocationList, error) {
|
||||
func (c *Codec) Decode(content []byte) (dnd.LocationRegistry, error) {
|
||||
value, err := c.DecodeCandidate(content)
|
||||
if err != nil {
|
||||
return dnd.LocationList{}, err
|
||||
return dnd.LocationRegistry{}, err
|
||||
}
|
||||
if err := validate(value); err != nil {
|
||||
return dnd.LocationList{}, fmt.Errorf("decode dnd location list: %w", err)
|
||||
return dnd.LocationRegistry{}, fmt.Errorf("decode dnd location registry: %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 (c *Codec) DecodeCandidate(content []byte) (dnd.LocationRegistry, error) {
|
||||
return candidatejson.DecodeCandidate[dnd.LocationRegistry]("dnd location registry", content)
|
||||
}
|
||||
|
||||
func validate(value dnd.LocationList) error {
|
||||
func validate(value dnd.LocationRegistry) error {
|
||||
if value.Locations == nil {
|
||||
return fmt.Errorf("locations must be present")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user