feat: new system
This commit is contained in:
@@ -6,33 +6,19 @@ import (
|
||||
"git.maximhutz.com/max/lambda/pkg/codec"
|
||||
)
|
||||
|
||||
type Codec interface {
|
||||
codec.Codec[Repr, Repr]
|
||||
|
||||
type Conversion interface {
|
||||
InType() string
|
||||
OutType() string
|
||||
|
||||
Run(Repr) (Repr, error)
|
||||
}
|
||||
|
||||
type convertedCodec[T, U any] struct {
|
||||
type forwardCodec[T, U any] struct {
|
||||
codec codec.Codec[T, U]
|
||||
inType, outType string
|
||||
}
|
||||
|
||||
func (c convertedCodec[T, U]) Decode(r Repr) (Repr, error) {
|
||||
u, ok := r.Data().(U)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("could not parse '%v' as '%s'", r, c.inType)
|
||||
}
|
||||
|
||||
t, err := c.codec.Decode(u)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NewRepr(c.outType, t), nil
|
||||
}
|
||||
|
||||
func (c convertedCodec[T, U]) Encode(r Repr) (Repr, error) {
|
||||
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.outType)
|
||||
@@ -46,10 +32,36 @@ func (c convertedCodec[T, U]) Encode(r Repr) (Repr, error) {
|
||||
return NewRepr(c.inType, u), nil
|
||||
}
|
||||
|
||||
func (c convertedCodec[T, U]) InType() string { return c.inType }
|
||||
func (c forwardCodec[T, U]) InType() string { return c.inType }
|
||||
|
||||
func (c convertedCodec[T, U]) OutType() string { return c.outType }
|
||||
func (c forwardCodec[T, U]) OutType() string { return c.outType }
|
||||
|
||||
func ConvertCodec[T, U any](e codec.Codec[T, U], inType, outType string) Codec {
|
||||
return convertedCodec[T, U]{e, inType, 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.inType)
|
||||
}
|
||||
|
||||
t, err := c.codec.Decode(u)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NewRepr(c.outType, t), nil
|
||||
}
|
||||
|
||||
func (c backwardCodec[T, U]) InType() string { return c.outType }
|
||||
|
||||
func (c backwardCodec[T, U]) OutType() string { return c.inType }
|
||||
|
||||
func ConvertCodec[T, U any](e codec.Codec[T, U], inType, outType string) []Conversion {
|
||||
return []Conversion{
|
||||
forwardCodec[T, U]{e, inType, outType},
|
||||
backwardCodec[T, U]{e, inType, outType},
|
||||
}
|
||||
}
|
||||
|
||||
27
internal/registry/converter.go
Normal file
27
internal/registry/converter.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package registry
|
||||
|
||||
import (
|
||||
"git.maximhutz.com/max/lambda/internal/cli"
|
||||
)
|
||||
|
||||
type Converter struct {
|
||||
data map[string][]cli.Conversion
|
||||
}
|
||||
|
||||
func NewConverter() *Converter {
|
||||
return &Converter{data: map[string][]cli.Conversion{}}
|
||||
}
|
||||
|
||||
func (g *Converter) Add(c cli.Conversion) {
|
||||
conversionsFromIn, ok := g.data[c.InType()]
|
||||
if !ok {
|
||||
conversionsFromIn = []cli.Conversion{}
|
||||
}
|
||||
|
||||
conversionsFromIn = append(conversionsFromIn, c)
|
||||
g.data[c.InType()] = conversionsFromIn
|
||||
}
|
||||
|
||||
func (g *Converter) ConversionsFrom(t string) []cli.Conversion {
|
||||
return g.data[t]
|
||||
}
|
||||
@@ -8,20 +8,23 @@ import (
|
||||
|
||||
type Registry struct {
|
||||
marshalers map[string]cli.Marshaler
|
||||
codecs []cli.Codec
|
||||
converter *Converter
|
||||
engines map[string]cli.Engine
|
||||
}
|
||||
|
||||
func New() *Registry {
|
||||
return &Registry{
|
||||
marshalers: map[string]cli.Marshaler{},
|
||||
codecs: []cli.Codec{},
|
||||
converter: NewConverter(),
|
||||
engines: map[string]cli.Engine{},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Registry) AddCodec(c cli.Codec) error {
|
||||
r.codecs = append(r.codecs, c)
|
||||
func (r *Registry) AddConversions(conversions ...cli.Conversion) error {
|
||||
for _, conversion := range conversions {
|
||||
r.converter.Add(conversion)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -52,8 +55,31 @@ func (r *Registry) GetEngine(name string) (cli.Engine, error) {
|
||||
return e, nil
|
||||
}
|
||||
|
||||
func (r *Registry) GetDefaultEngine(id string) (cli.Engine, error) {
|
||||
for _, engine := range r.engines {
|
||||
if engine.InType() == id {
|
||||
return engine, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("no engine for '%s'", id)
|
||||
}
|
||||
|
||||
func (r *Registry) ConvertTo(repr cli.Repr, outType string) (cli.Repr, error) {
|
||||
panic("")
|
||||
path, err := r.ConversionPath(repr.Id(), outType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := repr
|
||||
for _, conversion := range path {
|
||||
result, err = conversion.Run(result)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("converting '%s' to '%s': %w", conversion.InType(), conversion.OutType(), err)
|
||||
}
|
||||
}
|
||||
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (r *Registry) Marshal(repr cli.Repr) (string, error) {
|
||||
@@ -73,3 +99,52 @@ func (r *Registry) Unmarshal(s string, outType string) (cli.Repr, error) {
|
||||
|
||||
return m.Decode(s)
|
||||
}
|
||||
|
||||
func reverse[T any](list []T) []T {
|
||||
if list == nil {
|
||||
return list
|
||||
}
|
||||
|
||||
reversed := []T{}
|
||||
|
||||
for i := len(list) - 1; i >= 0; i-- {
|
||||
reversed = append(reversed, list[i])
|
||||
}
|
||||
|
||||
return reversed
|
||||
}
|
||||
|
||||
func (r *Registry) ConversionPath(from, to string) ([]cli.Conversion, error) {
|
||||
backtrack := map[string]cli.Conversion{}
|
||||
iteration := []string{from}
|
||||
for len(iteration) > 0 {
|
||||
nextIteration := []string{}
|
||||
|
||||
for _, item := range iteration {
|
||||
for _, conversion := range r.converter.ConversionsFrom(item) {
|
||||
if _, ok := backtrack[conversion.OutType()]; ok {
|
||||
continue
|
||||
}
|
||||
|
||||
nextIteration = append(nextIteration, conversion.OutType())
|
||||
backtrack[conversion.OutType()] = conversion
|
||||
}
|
||||
}
|
||||
|
||||
iteration = nextIteration
|
||||
}
|
||||
|
||||
reversedPath := []cli.Conversion{}
|
||||
current := to
|
||||
for current != from {
|
||||
conversion, ok := backtrack[current]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("no valid conversion from '%s' to '%s'", from, to)
|
||||
}
|
||||
|
||||
reversedPath = append(reversedPath, conversion)
|
||||
current = conversion.InType()
|
||||
}
|
||||
|
||||
return reverse(reversedPath), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user