feat: moved cli to registry

This commit is contained in:
2026-02-06 23:42:19 -05:00
parent 58d0823069
commit a2355fcd56
9 changed files with 53 additions and 89 deletions

View File

@@ -0,0 +1,50 @@
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
}