feat: marshalers to codecs

This commit is contained in:
2026-02-07 00:36:05 -05:00
parent b61ca744e0
commit 30382bee7e
9 changed files with 79 additions and 107 deletions

View File

@@ -0,0 +1,50 @@
package registry
import (
"fmt"
"reflect"
"git.maximhutz.com/max/lambda/pkg/codec"
)
type Codec interface {
codec.Codec[Repr]
InType() string
}
type convertedCodec[T any] struct {
codec codec.Codec[T]
inType string
}
func (c convertedCodec[T]) Decode(s string) (Repr, error) {
t, err := c.codec.Decode(s)
if err != nil {
return nil, err
}
return NewRepr(c.inType, t), nil
}
func (c convertedCodec[T]) Encode(r Repr) (string, error) {
t, ok := r.Data().(T)
if !ok {
dataType := reflect.TypeOf(r.Data())
allowedType := reflect.TypeFor[T]()
return "", fmt.Errorf("Codec for '%s' cannot parse '%s'", allowedType, dataType)
}
return c.codec.Encode(t)
}
func (c convertedCodec[T]) InType() string { return c.inType }
func RegisterCodec[T any](registry *Registry, m codec.Codec[T], inType string) error {
if _, ok := registry.codecs[inType]; ok {
return fmt.Errorf("Codec for '%s' already registered", inType)
}
registry.codecs[inType] = convertedCodec[T]{m, inType}
return nil
}

View File

@@ -13,18 +13,18 @@ type Conversion interface {
Run(Repr) (Repr, error)
}
type forwardCodec[T, U any] struct {
codec codec.Codec[T, U]
type convertedConversion[T, U any] struct {
conversion codec.Conversion[T, U]
inType, outType string
}
func (c forwardCodec[T, U]) Run(r Repr) (Repr, error) {
func (c convertedConversion[T, U]) Run(r Repr) (Repr, error) {
t, ok := r.Data().(T)
if !ok {
return nil, fmt.Errorf("could not parse '%v' as '%s'", t, c.inType)
}
u, err := c.codec.Encode(t)
u, err := c.conversion(t)
if err != nil {
return nil, err
}
@@ -32,36 +32,12 @@ func (c forwardCodec[T, U]) Run(r Repr) (Repr, error) {
return NewRepr(c.outType, u), nil
}
func (c forwardCodec[T, U]) InType() string { return c.inType }
func (c convertedConversion[T, U]) InType() string { return c.inType }
func (c forwardCodec[T, U]) OutType() string { return c.outType }
func (c convertedConversion[T, U]) OutType() string { return c.outType }
type backwardCodec[T, U any] struct {
codec codec.Codec[T, U]
inType, outType string
}
func (c backwardCodec[T, U]) Run(r Repr) (Repr, error) {
u, ok := r.Data().(U)
if !ok {
return nil, fmt.Errorf("could not parse '%v' as '%s'", r, c.outType)
}
t, err := c.codec.Decode(u)
if err != nil {
return nil, err
}
return NewRepr(c.inType, t), nil
}
func (c backwardCodec[T, U]) InType() string { return c.outType }
func (c backwardCodec[T, U]) OutType() string { return c.inType }
func RegisterCodec[T, U any](registry *Registry, c codec.Codec[T, U], inType, outType string) error {
registry.converter.Add(forwardCodec[T, U]{c, inType, outType})
registry.converter.Add(backwardCodec[T, U]{c, inType, outType})
func RegisterConversion[T, U any](registry *Registry, conversion func(T) (U, error), inType, outType string) error {
registry.converter.Add(convertedConversion[T, U]{conversion, inType, outType})
return nil
}

View File

@@ -1,50 +0,0 @@
package registry
import (
"fmt"
"reflect"
"git.maximhutz.com/max/lambda/pkg/codec"
)
type Marshaler interface {
codec.Marshaler[Repr]
InType() string
}
type convertedMarshaler[T any] struct {
codec codec.Marshaler[T]
inType string
}
func (c convertedMarshaler[T]) Decode(s string) (Repr, error) {
t, err := c.codec.Decode(s)
if err != nil {
return nil, err
}
return NewRepr(c.inType, t), nil
}
func (c convertedMarshaler[T]) Encode(r Repr) (string, error) {
t, ok := r.Data().(T)
if !ok {
dataType := reflect.TypeOf(r.Data())
allowedType := reflect.TypeFor[T]()
return "", fmt.Errorf("marshaler for '%s' cannot parse '%s'", allowedType, dataType)
}
return c.codec.Encode(t)
}
func (c convertedMarshaler[T]) InType() string { return c.inType }
func RegisterMarshaler[T any](registry *Registry, m codec.Marshaler[T], inType string) error {
if _, ok := registry.marshalers[inType]; ok {
return fmt.Errorf("marshaler for '%s' already registered", inType)
}
registry.marshalers[inType] = convertedMarshaler[T]{m, inType}
return nil
}

View File

@@ -7,16 +7,16 @@ import (
)
type Registry struct {
marshalers map[string]Marshaler
converter *Converter
engines map[string]Engine
codecs map[string]Codec
converter *Converter
engines map[string]Engine
}
func New() *Registry {
return &Registry{
marshalers: map[string]Marshaler{},
converter: NewConverter(),
engines: map[string]Engine{},
codecs: map[string]Codec{},
converter: NewConverter(),
engines: map[string]Engine{},
}
}
@@ -61,7 +61,7 @@ func (r *Registry) ConvertTo(repr Repr, outType string) (Repr, error) {
}
func (r *Registry) Marshal(repr Repr) (string, error) {
m, ok := r.marshalers[repr.ID()]
m, ok := r.codecs[repr.ID()]
if !ok {
return "", fmt.Errorf("no marshaler for '%s'", repr.ID())
}
@@ -70,7 +70,7 @@ func (r *Registry) Marshal(repr Repr) (string, error) {
}
func (r *Registry) Unmarshal(s string, outType string) (Repr, error) {
m, ok := r.marshalers[outType]
m, ok := r.codecs[outType]
if !ok {
return nil, fmt.Errorf("no marshaler for '%s'", outType)
}