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,67 @@
package registry
import (
"fmt"
"git.maximhutz.com/max/lambda/pkg/codec"
)
type Conversion interface {
InType() string
OutType() string
Run(Repr) (Repr, error)
}
type forwardCodec[T, U any] struct {
codec codec.Codec[T, U]
inType, outType string
}
func (c forwardCodec[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)
if err != nil {
return nil, err
}
return NewRepr(c.outType, u), nil
}
func (c forwardCodec[T, U]) InType() string { return c.inType }
func (c forwardCodec[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})
return nil
}

View File

@@ -1,27 +1,23 @@
package registry
import (
"git.maximhutz.com/max/lambda/internal/cli"
)
type Converter struct {
data map[string][]cli.Conversion
data map[string][]Conversion
}
func NewConverter() *Converter {
return &Converter{data: map[string][]cli.Conversion{}}
return &Converter{data: map[string][]Conversion{}}
}
func (g *Converter) Add(c cli.Conversion) {
func (g *Converter) Add(c Conversion) {
conversionsFromIn, ok := g.data[c.InType()]
if !ok {
conversionsFromIn = []cli.Conversion{}
conversionsFromIn = []Conversion{}
}
conversionsFromIn = append(conversionsFromIn, c)
g.data[c.InType()] = conversionsFromIn
}
func (g *Converter) ConversionsFrom(t string) []cli.Conversion {
func (g *Converter) ConversionsFrom(t string) []Conversion {
return g.data[t]
}

View File

@@ -0,0 +1,36 @@
package registry
import (
"fmt"
"git.maximhutz.com/max/lambda/pkg/engine"
)
type Engine interface {
Load() Process
Name() string
InType() string
}
type convertedEngine[T any] struct {
engine engine.Engine[T]
name string
inType string
}
func (e convertedEngine[T]) InType() string { return e.inType }
func (e convertedEngine[T]) Name() string { return e.name }
func (e convertedEngine[T]) Load() Process {
return convertedProcess[T]{e.engine.Load(), e.inType}
}
func RegisterEngine[T any](registry *Registry, e engine.Engine[T], name, inType string) error {
if _, ok := registry.engines[name]; ok {
return fmt.Errorf("engine '%s' already registered", name)
}
registry.engines[name] = &convertedEngine[T]{e, name, inType}
return nil
}

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
}

View File

@@ -0,0 +1,41 @@
package registry
import (
"fmt"
"git.maximhutz.com/max/lambda/pkg/engine"
)
type Process interface {
engine.Process[Repr]
InType() string
}
type convertedProcess[T any] struct {
process engine.Process[T]
inType string
}
func (e convertedProcess[T]) InType() string { return e.inType }
func (b convertedProcess[T]) Get() (Repr, error) {
s, err := b.process.Get()
if err != nil {
return nil, err
}
return NewRepr(b.inType, s), nil
}
func (b convertedProcess[T]) Set(r Repr) error {
if t, ok := r.Data().(T); ok {
return b.process.Set(t)
}
return fmt.Errorf("Incorrent format '%s' for engine '%s'.", r.Id(), b.inType)
}
func (b convertedProcess[T]) Step(i int) bool {
return b.process.Step(i)
}

View File

@@ -4,68 +4,23 @@ import (
"fmt"
"iter"
"maps"
"git.maximhutz.com/max/lambda/internal/cli"
)
type Registry struct {
marshalers map[string]cli.Marshaler
marshalers map[string]Marshaler
converter *Converter
engines map[string]cli.Engine
engines map[string]Engine
}
func New() *Registry {
return &Registry{
marshalers: map[string]cli.Marshaler{},
marshalers: map[string]Marshaler{},
converter: NewConverter(),
engines: map[string]cli.Engine{},
engines: map[string]Engine{},
}
}
func (r *Registry) AddConversions(conversions ...cli.Conversion) error {
for _, conversion := range conversions {
r.converter.Add(conversion)
}
return nil
}
func (r *Registry) MustAddConversions(conversions ...cli.Conversion) {
if err := r.AddConversions(conversions...); err != nil {
panic(err)
}
}
func (r *Registry) AddMarshaler(c cli.Marshaler) error {
if _, ok := r.marshalers[c.InType()]; ok {
return fmt.Errorf("marshaler for '%s' already registered", c.InType())
}
r.marshalers[c.InType()] = c
return nil
}
func (r *Registry) MustAddMarshaler(c cli.Marshaler) {
if err := r.AddMarshaler(c); err != nil {
panic(err)
}
}
func (r *Registry) AddEngine(e cli.Engine) error {
if _, ok := r.engines[e.Name()]; ok {
return fmt.Errorf("engine '%s' already registered", e.Name())
}
r.engines[e.Name()] = e
return nil
}
func (r *Registry) MustAddEngine(e cli.Engine) {
if err := r.AddEngine(e); err != nil {
panic(err)
}
}
func (r Registry) GetEngine(name string) (cli.Engine, error) {
func (r Registry) GetEngine(name string) (Engine, error) {
e, ok := r.engines[name]
if !ok {
return nil, fmt.Errorf("engine '%s' not found", name)
@@ -74,11 +29,11 @@ func (r Registry) GetEngine(name string) (cli.Engine, error) {
return e, nil
}
func (r Registry) ListEngines() iter.Seq[cli.Engine] {
func (r Registry) ListEngines() iter.Seq[Engine] {
return maps.Values(r.engines)
}
func (r *Registry) GetDefaultEngine(id string) (cli.Engine, error) {
func (r *Registry) GetDefaultEngine(id string) (Engine, error) {
for _, engine := range r.engines {
if engine.InType() == id {
return engine, nil
@@ -88,7 +43,7 @@ func (r *Registry) GetDefaultEngine(id string) (cli.Engine, error) {
return nil, fmt.Errorf("no engine for '%s'", id)
}
func (r *Registry) ConvertTo(repr cli.Repr, outType string) (cli.Repr, error) {
func (r *Registry) ConvertTo(repr Repr, outType string) (Repr, error) {
path, err := r.ConversionPath(repr.Id(), outType)
if err != nil {
return nil, err
@@ -105,7 +60,7 @@ func (r *Registry) ConvertTo(repr cli.Repr, outType string) (cli.Repr, error) {
return result, err
}
func (r *Registry) Marshal(repr cli.Repr) (string, error) {
func (r *Registry) Marshal(repr Repr) (string, error) {
m, ok := r.marshalers[repr.Id()]
if !ok {
return "", fmt.Errorf("no marshaler for '%s'", repr.Id())
@@ -114,7 +69,7 @@ func (r *Registry) Marshal(repr cli.Repr) (string, error) {
return m.Encode(repr)
}
func (r *Registry) Unmarshal(s string, outType string) (cli.Repr, error) {
func (r *Registry) Unmarshal(s string, outType string) (Repr, error) {
m, ok := r.marshalers[outType]
if !ok {
return nil, fmt.Errorf("no marshaler for '%s'", outType)
@@ -137,8 +92,8 @@ func reverse[T any](list []T) []T {
return reversed
}
func (r *Registry) ConversionPath(from, to string) ([]cli.Conversion, error) {
backtrack := map[string]cli.Conversion{}
func (r *Registry) ConversionPath(from, to string) ([]Conversion, error) {
backtrack := map[string]Conversion{}
iteration := []string{from}
for len(iteration) > 0 {
nextIteration := []string{}
@@ -157,7 +112,7 @@ func (r *Registry) ConversionPath(from, to string) ([]cli.Conversion, error) {
iteration = nextIteration
}
reversedPath := []cli.Conversion{}
reversedPath := []Conversion{}
current := to
for current != from {
conversion, ok := backtrack[current]

21
internal/registry/repr.go Normal file
View File

@@ -0,0 +1,21 @@
package registry
type Repr interface {
// Id returns to name of the objects underlying representation. If is
// assumed that if two Repr objects have the same Id(), they share the same
// representation.
Id() string
Data() any
}
type baseRepr struct {
id string
data any
}
func (r baseRepr) Id() string { return r.id }
func (r baseRepr) Data() any { return r.data }
func NewRepr(id string, data any) Repr { return baseRepr{id, data} }