Run D&D spell lanes through typed artifacts
This commit is contained in:
@@ -63,10 +63,12 @@ type ArtifactCodecRegistry struct {
|
||||
}
|
||||
|
||||
type artifactCodecEntry struct {
|
||||
spec ArtifactCodecSpec
|
||||
valueType reflect.Type
|
||||
encode func(any) ([]byte, error)
|
||||
decode func([]byte) (any, error)
|
||||
spec ArtifactCodecSpec
|
||||
valueType reflect.Type
|
||||
encode func(any) ([]byte, error)
|
||||
encodeCandidate func(any) ([]byte, error)
|
||||
metadata func(any) map[string]any
|
||||
decode func([]byte) (any, error)
|
||||
}
|
||||
|
||||
func NewArtifactCodecRegistry() *ArtifactCodecRegistry {
|
||||
@@ -118,6 +120,29 @@ func RegisterArtifactCodec[T any](registry *ArtifactCodecRegistry, codec contrac
|
||||
return decoded, nil
|
||||
},
|
||||
}
|
||||
entry.encodeCandidate = entry.encode
|
||||
if candidate, ok := any(codec).(interface{ EncodeCandidate(T) ([]byte, error) }); ok {
|
||||
entry.encodeCandidate = func(value any) ([]byte, error) {
|
||||
typed, err := exactTypedValue[T]("encode candidate artifact", value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
content, err := candidate.EncodeCandidate(typed)
|
||||
if err != nil {
|
||||
return nil, &ArtifactCodecOperationError{Operation: "encode", Kind: spec.Kind, Err: err}
|
||||
}
|
||||
return append([]byte(nil), content...), nil
|
||||
}
|
||||
}
|
||||
if provider, ok := any(codec).(interface{ Metadata(T) map[string]any }); ok {
|
||||
entry.metadata = func(value any) map[string]any {
|
||||
typed, err := exactTypedValue[T]("artifact metadata", value)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return cloneMetadata(provider.Metadata(typed))
|
||||
}
|
||||
}
|
||||
if registry.entries == nil {
|
||||
registry.entries = make(map[contracts.ArtifactKind]artifactCodecEntry)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user