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:
2
internal/cli/cli.go
Normal file
2
internal/cli/cli.go
Normal file
@@ -0,0 +1,2 @@
|
||||
// Package cli package provides various utilities to the 'lambda' program.
|
||||
package cli
|
||||
@@ -1,67 +0,0 @@
|
||||
package cli
|
||||
|
||||
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 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},
|
||||
}
|
||||
}
|
||||
29
internal/cli/destination.go
Normal file
29
internal/cli/destination.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// A Destination is method of writing output to the user.
|
||||
type Destination interface {
|
||||
// Write data to this destination.
|
||||
Write(data string) error
|
||||
}
|
||||
|
||||
// An StdoutDestination writes to stdout.
|
||||
type StdoutDestination struct{}
|
||||
|
||||
// Write outputs to standard output.
|
||||
func (d StdoutDestination) Write(data string) error {
|
||||
fmt.Println(data)
|
||||
return nil
|
||||
}
|
||||
|
||||
// A FileDestination writes to a file.
|
||||
type FileDestination struct{ Path string }
|
||||
|
||||
// Write outputs to a file.
|
||||
func (d FileDestination) Write(data string) error {
|
||||
return os.WriteFile(d.Path, []byte(data+"\n"), 0644)
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package cli
|
||||
|
||||
import "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 ConvertEngine[T any](e engine.Engine[T], name, inType string) Engine {
|
||||
return &convertedEngine[T]{e, name, inType}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
package cli
|
||||
|
||||
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 ConvertMarshaler[T any](e codec.Marshaler[T], inType string) Marshaler {
|
||||
return convertedMarshaler[T]{e, inType}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package cli
|
||||
|
||||
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)
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package cli
|
||||
|
||||
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} }
|
||||
44
internal/cli/source.go
Normal file
44
internal/cli/source.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
// A Source is a method of extracting input from the user.
|
||||
type Source interface {
|
||||
// Extract fetches data from this source.
|
||||
Extract() (string, error)
|
||||
}
|
||||
|
||||
// A StringSource is defined by a string.
|
||||
type StringSource struct{ Data string }
|
||||
|
||||
// Extract pulls input data from the internal string.
|
||||
func (s StringSource) Extract() (string, error) { return s.Data, nil }
|
||||
|
||||
// A StdinSource pulls from standard input.
|
||||
type StdinSource struct{}
|
||||
|
||||
// Extract pulls input data from standard input.
|
||||
func (s StdinSource) Extract() (string, error) {
|
||||
data, err := io.ReadAll(os.Stdin)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
// A FileSource reads from a file.
|
||||
type FileSource struct{ Path string }
|
||||
|
||||
// Extract pulls input data from the file source.
|
||||
func (s FileSource) Extract() (string, error) {
|
||||
data, err := os.ReadFile(s.Path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(data), nil
|
||||
}
|
||||
Reference in New Issue
Block a user