style: restructure cli and registry packages (#43)
## Description The `internal/cli` package had grown to contain both CLI utilities (source/destination I/O) and registry-level abstractions (repr, conversion, engine, marshaler). This PR separates concerns by moving registry types into `internal/registry` and keeping only CLI I/O types in `internal/cli`. It also simplifies several core abstractions and aligns naming conventions. - Move `Source`, `Destination` from `internal/config` to `internal/cli`. - Move `Repr`, `Conversion`, `Engine`, `Process`, `Codec` from `internal/cli` to `internal/registry`. - Rename "marshalers" to "codecs" throughout the codebase. - Simplify `codec.Codec[T, U]` to `codec.Codec[T]` (string-based marshaling only). - Add `codec.Conversion[T, U]` as a function type alias. - Change `engine.Engine[T]` from an interface to a function type. - Merge `Engine.Load()` + `Process.Set()` into a single `Engine.Load(Repr)` call. - Convert `Saccharine2Lambda` from a struct to standalone conversion functions. - Replace registry methods (`MustAddMarshaler`, `MustAddEngine`, `MustAddConversions`) with generic free functions (`RegisterCodec`, `RegisterEngine`, `RegisterConversion`). - Remove unused `internal/config` package (`Config`, `GetLogger`, `ParseFromArgs`). - Remove unused `pkg/emitter` package. - Rename `Id()` to `ID()` per Go conventions. - Add documentation comments and enable `checkPublicInterface` lint rule. - Rename `reduce_one.go` to `reduce_once.go`. ### Decisions - `Engine[T]` is now a function type (`func(T) (Process[T], error)`) rather than an interface, since the only method was `Load`. - `Codec[T, U]` was split into `Codec[T]` (string marshaling) and `Conversion[T, U]` (type-to-type conversion function), which better reflects how they are actually used. - Registration uses free generic functions (`RegisterCodec`, `RegisterEngine`, `RegisterConversion`) instead of methods on `Registry`, enabling type inference at the call site. ## Benefits - Clearer separation of concerns between CLI I/O and the registry's internal type system. - Simpler abstractions: fewer interfaces, fewer wrapper types, fewer indirections. - Removing unused packages (`config`, `emitter`) reduces maintenance burden. - Naming conventions (`ID`, codecs, `reduce_once`) are more idiomatic. ## Checklist - [x] Code follows conventional commit format. - [x] Branch follows naming convention (`<type>/<description>`). - [x] Tests pass (if applicable). - [x] Documentation updated (if applicable). Reviewed-on: #43 Co-authored-by: M.V. Hutz <git@maximhutz.me> Co-committed-by: M.V. Hutz <git@maximhutz.me>
This commit was merged in pull request #43.
This commit is contained in:
50
internal/registry/codec.go
Normal file
50
internal/registry/codec.go
Normal 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
|
||||
}
|
||||
43
internal/registry/conversion.go
Normal file
43
internal/registry/conversion.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package registry
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.maximhutz.com/max/lambda/pkg/codec"
|
||||
)
|
||||
|
||||
type Conversion interface {
|
||||
InType() string
|
||||
OutType() string
|
||||
|
||||
Run(Repr) (Repr, error)
|
||||
}
|
||||
|
||||
type convertedConversion[T, U any] struct {
|
||||
conversion codec.Conversion[T, U]
|
||||
inType, outType string
|
||||
}
|
||||
|
||||
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.conversion(t)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NewRepr(c.outType, u), nil
|
||||
}
|
||||
|
||||
func (c convertedConversion[T, U]) InType() string { return c.inType }
|
||||
|
||||
func (c convertedConversion[T, U]) OutType() string { return c.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
|
||||
}
|
||||
@@ -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]
|
||||
}
|
||||
|
||||
46
internal/registry/engine.go
Normal file
46
internal/registry/engine.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package registry
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.maximhutz.com/max/lambda/pkg/engine"
|
||||
)
|
||||
|
||||
type Engine interface {
|
||||
Load(Repr) (Process, error)
|
||||
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(r Repr) (Process, error) {
|
||||
t, ok := r.Data().(T)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("'ncorrent format '%s' for engine '%s'", r.ID(), e.inType)
|
||||
}
|
||||
|
||||
process, err := e.engine(t)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return convertedProcess[T]{process, e.inType}, nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
31
internal/registry/process.go
Normal file
31
internal/registry/process.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package registry
|
||||
|
||||
import (
|
||||
"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]) Step(i int) bool {
|
||||
return b.process.Step(i)
|
||||
}
|
||||
@@ -4,68 +4,23 @@ import (
|
||||
"fmt"
|
||||
"iter"
|
||||
"maps"
|
||||
|
||||
"git.maximhutz.com/max/lambda/internal/cli"
|
||||
)
|
||||
|
||||
type Registry struct {
|
||||
marshalers map[string]cli.Marshaler
|
||||
converter *Converter
|
||||
engines map[string]cli.Engine
|
||||
codecs map[string]Codec
|
||||
converter *Converter
|
||||
engines map[string]Engine
|
||||
}
|
||||
|
||||
func New() *Registry {
|
||||
return &Registry{
|
||||
marshalers: map[string]cli.Marshaler{},
|
||||
converter: NewConverter(),
|
||||
engines: map[string]cli.Engine{},
|
||||
codecs: map[string]Codec{},
|
||||
converter: NewConverter(),
|
||||
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,8 +43,8 @@ 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) {
|
||||
path, err := r.ConversionPath(repr.Id(), outType)
|
||||
func (r *Registry) ConvertTo(repr Repr, outType string) (Repr, error) {
|
||||
path, err := r.ConversionPath(repr.ID(), outType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -105,17 +60,17 @@ func (r *Registry) ConvertTo(repr cli.Repr, outType string) (cli.Repr, error) {
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (r *Registry) Marshal(repr cli.Repr) (string, error) {
|
||||
m, ok := r.marshalers[repr.Id()]
|
||||
func (r *Registry) Marshal(repr Repr) (string, error) {
|
||||
m, ok := r.codecs[repr.ID()]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("no marshaler for '%s'", repr.Id())
|
||||
return "", fmt.Errorf("no marshaler for '%s'", repr.ID())
|
||||
}
|
||||
|
||||
return m.Encode(repr)
|
||||
}
|
||||
|
||||
func (r *Registry) Unmarshal(s string, outType string) (cli.Repr, error) {
|
||||
m, ok := r.marshalers[outType]
|
||||
func (r *Registry) Unmarshal(s string, outType string) (Repr, error) {
|
||||
m, ok := r.codecs[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]
|
||||
|
||||
24
internal/registry/repr.go
Normal file
24
internal/registry/repr.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package registry
|
||||
|
||||
// A Repr is a lambda calculus expression. It can have any type of
|
||||
// representation, so long as that class is known to the registry it is handled
|
||||
// by.
|
||||
type Repr interface {
|
||||
// ID returns the name of the underlying representation. It is assumed that
|
||||
// if two expressions have the same Id(), they have the same representation.
|
||||
ID() string
|
||||
|
||||
// The base expression data.
|
||||
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} }
|
||||
Reference in New Issue
Block a user