refactor: rewrite CLI and internal architecture (#41)
## Description The old architecture used a monolithic `main()` with a custom arg parser, an event-emitter-based runtime, and a plugin system for optional features. This PR rewrites the CLI and internal architecture to be modular, extensible, and built around a registry of interchangeable components. - Replace custom CLI arg parsing with Cobra subcommands (`convert`, `reduce`, `engine list`). - Introduce a registry system (`internal/registry`) for marshalers, codecs, and engines, with BFS-based conversion path resolution. - Add type-erased adapter layer (`internal/cli`) with `Repr`, `Engine`, `Process`, `Marshaler`, and `Conversion` interfaces wrapping generic `pkg/` types. - Replace the event-emitter-based `Runtime` with a simpler `Engine`/`Process` model (`pkg/engine`). - Add generic `Codec[T, U]` and `Marshaler[T]` interfaces (`pkg/codec`). - Merge `saccharine/token` sub-package into `saccharine` and rename scanner functions from `parse*` to `scan*`. - Make saccharine-to-lambda conversion bidirectional (encode and decode). - Add `lambda.Marshaler` and `saccharine.Marshaler` implementing `codec.Marshaler`. - Remove old infrastructure: `pkg/runtime`, `pkg/expr`, `internal/plugins`, `internal/statistics`. - Add `make lint` target and update golangci-lint config. ### Decisions - Cobra was chosen for the CLI framework to support nested subcommands and standard flag handling. - The registry uses BFS to find conversion paths between representations, allowing multi-hop conversions without hardcoding routes. - Type erasure via `cli.Repr` (wrapping `any`) enables the registry to work with heterogeneous types while keeping `pkg/` generics type-safe. - The old plugin/event system was removed entirely rather than adapted, since the new `Process` model can support hooks differently in the future. ## Benefits - Subcommands make the CLI self-documenting and easier to extend with new functionality. - The registry pattern decouples representations, conversions, and engines, making it trivial to add new ones. - BFS conversion routing means adding a single codec automatically enables transitive conversions. - Simpler `Engine`/`Process` model reduces complexity compared to the event-emitter runtime. - Consolidating the `token` sub-package reduces import depth and package sprawl. ## Checklist - [x] Code follows conventional commit format. - [x] Branch follows naming convention (`<type>/<description>`). Always use underscores. - [ ] Tests pass (if applicable). - [ ] Documentation updated (if applicable). Reviewed-on: #41 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 #41.
This commit is contained in:
173
internal/registry/registry.go
Normal file
173
internal/registry/registry.go
Normal file
@@ -0,0 +1,173 @@
|
||||
package registry
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func New() *Registry {
|
||||
return &Registry{
|
||||
marshalers: map[string]cli.Marshaler{},
|
||||
converter: NewConverter(),
|
||||
engines: map[string]cli.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) {
|
||||
e, ok := r.engines[name]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("engine '%s' not found", name)
|
||||
}
|
||||
|
||||
return e, nil
|
||||
}
|
||||
|
||||
func (r Registry) ListEngines() iter.Seq[cli.Engine] {
|
||||
return maps.Values(r.engines)
|
||||
}
|
||||
|
||||
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) {
|
||||
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) {
|
||||
m, ok := r.marshalers[repr.Id()]
|
||||
if !ok {
|
||||
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]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("no marshaler for '%s'", outType)
|
||||
}
|
||||
|
||||
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