## Summary - Added doc comments across the codebase: `pkg/lambda`, `pkg/saccharine`, `pkg/codec`, `pkg/engine`, `pkg/iterator`, `pkg/set`, `pkg/convert`, `internal/registry`, and `cmd/lambda`. - Made lambda and saccharine expression structs use public fields instead of getters, matching `go/ast` conventions. - Removed superfluous constructors for saccharine and lambda expression/statement types in favor of struct literals. - Consolidated saccharine token constructors into a single `NewToken` function. - Removed the unused `trace` package. ## Test plan - [x] `go build ./...` passes. - [x] `go test ./...` passes. - [ ] Verify `go doc` output renders correctly for documented packages. Reviewed-on: #45 Co-authored-by: M.V. Hutz <git@maximhutz.me> Co-committed-by: M.V. Hutz <git@maximhutz.me>
59 lines
1.6 KiB
Go
59 lines
1.6 KiB
Go
package registry
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
|
|
"git.maximhutz.com/max/lambda/pkg/codec"
|
|
)
|
|
|
|
// A Codec is a type-erased codec that serializes and deserializes expressions
|
|
// as Expr values, regardless of the underlying representation type.
|
|
type Codec interface {
|
|
codec.Codec[Expr]
|
|
|
|
// InType returns the name of the representation this codec handles.
|
|
InType() string
|
|
}
|
|
|
|
// A registeredCodec adapts a typed codec.Codec[T] into the type-erased Codec
|
|
// interface. It wraps decoded values into Expr on decode, and extracts the
|
|
// underlying T from an Expr on encode.
|
|
type registeredCodec[T any] struct {
|
|
codec codec.Codec[T]
|
|
inType string
|
|
}
|
|
|
|
func (c registeredCodec[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 registeredCodec[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 registeredCodec[T]) InType() string { return c.inType }
|
|
|
|
// RegisterCodec registers a typed codec under the given representation name.
|
|
// Returns an error if a codec for that representation is already registered.
|
|
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] = registeredCodec[T]{m, inType}
|
|
return nil
|
|
}
|