## Description The `Repr` type name was unclear — it was intended to represent a lambda calculus expression, not a "representation." This PR renames `Repr` to `Expr` throughout the registry package for clarity. - Rename `Repr` interface to `Expr` and `baseRepr` struct to `baseExpr`. - Rename `repr.go` to `expr.go`. - Rename `ID()` method to `Repr()` to indicate the representation type. - Rename `NewRepr` constructor to `NewExpr`. - Update all usages in codec, conversion, engine, process, and registry files. - Add command aliases `conv` and `eng` for `convert` and `engine` subcommands. ## Benefits - The naming better reflects the domain: an `Expr` is an expression, and `Repr()` returns its representation kind. - Command aliases reduce typing for common subcommands. ## Checklist - [x] Code follows conventional commit format. - [x] Branch follows naming convention (`<type>/<description>`). Always use underscores. - [x] Tests pass (if applicable). - [x] Documentation updated (if applicable). Reviewed-on: #44 Co-authored-by: M.V. Hutz <git@maximhutz.me> Co-committed-by: M.V. Hutz <git@maximhutz.me>
51 lines
1015 B
Go
51 lines
1015 B
Go
package registry
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
|
|
"git.maximhutz.com/max/lambda/pkg/codec"
|
|
)
|
|
|
|
type Codec interface {
|
|
codec.Codec[Expr]
|
|
|
|
InType() string
|
|
}
|
|
|
|
type convertedCodec[T any] struct {
|
|
codec codec.Codec[T]
|
|
inType string
|
|
}
|
|
|
|
func (c convertedCodec[T]) Decode(s string) (Expr, error) {
|
|
t, err := c.codec.Decode(s)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return NewExpr(c.inType, t), nil
|
|
}
|
|
|
|
func (c convertedCodec[T]) Encode(r Expr) (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
|
|
}
|