Files
uberbringer/internal/pkg/modules/registry.go
T
57_WolveandClaude Opus 5.5 3e9779f71a
Security / Vulnerability check (push) Successful in 41s
Test / Lint and test (push) Successful in 1m50s
Add the list block and the module layer with the note module
The list block prints items with a dot, dash, number or checkbox bullet,
wrapping a long item with a hanging indent; a checkbox list ticks its
done items as [√], or [x] on code pages without √. It stands alone or
inside a box, and its items, bullet and done fields carry their enums
and limits as jsonschema tags.

internal/pkg/modules holds modules that build ordinary jobs from their
own payloads: a registry by name, an Env with the clock, time zone and
time layout, strict payload decoding that reports every problem, ErrSkip
and PayloadError. The note module prints a boxed note with an optional
checklist and a time stamp in the footer.

Three routes join the route table: POST /api/v1/print/{module} and
POST /api/v1/preview/{module}, which take ?copies and a reserved
?printer, and GET /api/v1/modules. Print and the module route share one
submit path, and preview shares one too. New error codes:
invalid_payload, invalid_query, unknown_printer, module_not_found and
module_failed; a module that builds an invalid receipt is logged and
reported to Sentry once. [format] gains timezone and time_layout, the
binary embeds the time zone database, and uberbringer preview takes
--module.

The note receipt passed on the TM-T88IV, and its bytes are pinned in
TestPaperReceipts. docs/api.md describes the list block, the module
routes and the note payload; docs/swagger.json is regenerated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 10:55:50 -06:00

120 lines
3.4 KiB
Go

// Package modules turns plain payloads, such as a note's title, text and
// items, into receipt jobs built from the layout blocks. Each module owns
// its payload format; the HTTP routes and the command line look modules up
// by name in a Registry.
package modules
import (
"context"
"errors"
"fmt"
"maps"
"regexp"
"slices"
"strings"
"time"
"uberbringer/internal/pkg/receipt"
)
// ErrSkip is returned by Build when the payload asks for nothing to be
// printed.
var ErrSkip = errors.New("nothing to print")
// PayloadError lists every problem with a payload, each prefixed with the
// field it concerns, e.g. "done[0]: index 5 is out of range".
type PayloadError struct {
Problems []string
}
// Error implements error.
func (e *PayloadError) Error() string {
return "invalid payload: " + strings.Join(e.Problems, "; ")
}
// Env is what a module may know about the time a receipt is built at. A
// module never reads the clock itself, so that its output is reproducible.
type Env struct {
// Now is the time the receipt is built at.
Now time.Time
// Location is the time zone times are printed in. Nil means local time.
Location *time.Location
// TimeLayout is the Go time layout times are printed in, e.g.
// "02 Jan 15:04".
TimeLayout string
}
// Stamp returns Now in Location, formatted with TimeLayout.
func (e Env) Stamp() string {
loc := e.Location
if loc == nil {
loc = time.Local
}
return e.Now.In(loc).Format(e.TimeLayout)
}
// Module builds a receipt from its own payload format.
type Module interface {
// Name is the module's name in routes, e.g. "note" in
// /api/v1/print/note.
Name() string
// Description says in one line what the module prints.
Description() string
// Build turns body into a job. It returns a *PayloadError for a body
// that is not a valid payload, and ErrSkip for one that asks for
// nothing to be printed. The job it returns is expected to pass
// Job.Validate; one that does not is a bug in the module.
Build(ctx context.Context, body []byte, env Env) (receipt.Job, error)
}
// Info describes a module for listings.
type Info struct {
Name string
Description string
}
// validName is the form of a module name: it appears as a path segment.
var validName = regexp.MustCompile(`^[a-z][a-z0-9-]{0,31}$`)
// Registry holds the modules by name. It is safe for concurrent use once
// built.
type Registry struct {
modules map[string]Module
}
// NewRegistry returns a registry of mods. Each name must be unique and made
// of lowercase letters, digits and dashes, starting with a letter.
func NewRegistry(mods ...Module) (*Registry, error) {
r := &Registry{modules: make(map[string]Module, len(mods))}
for _, m := range mods {
name := m.Name()
if !validName.MatchString(name) {
return nil, fmt.Errorf("module name %q: want lowercase letters, digits and dashes, starting with a letter", name)
}
if _, ok := r.modules[name]; ok {
return nil, fmt.Errorf("module %q is registered twice", name)
}
r.modules[name] = m
}
return r, nil
}
// Get returns the module called name.
func (r *Registry) Get(name string) (Module, bool) {
m, ok := r.modules[name]
return m, ok
}
// List describes every module, sorted by name.
func (r *Registry) List() []Info {
infos := make([]Info, 0, len(r.modules))
for _, name := range slices.Sorted(maps.Keys(r.modules)) {
infos = append(infos, Info{Name: name, Description: r.modules[name].Description()})
}
return infos
}