Files
mesh-delivery/sdk/seatdefs/seatdefs.go
T
jschoubben 541eaa20fe
mesh/delivery merged: source.main moved to novox/mesh-delivery@21eaa55b277a
mesh/delivery-group group fix/47-G-bridge merged: every member is merged
mesh/check checking 541eaa20fec4 on 21eaa55b277a
Bridge for G's take-in: the pre-G package directories contracts/go and sdk again, unused
The running pre-G indexer publishes a repository's packages only when every package directory it published last is
still in the tree; G moved contracts/go and sdk to modules/, so mesh-delivery's packages, G's renamed paths among them,
are never published and nothing importing them builds (2026-11-13, indexer journal 'its packages are not published').
These are the trees of e70754ef, unchanged, without sdk/module.json (no second module). Nothing imports them; they go
once G's indexer runs and publishes without them.

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-13 14:34:40 +01:00

173 lines
5.9 KiB
Go

// Package seatdefs is how a seat definition is read from a contracts module at a commit, and which contracts module may
// define a seat (novox/hq ADR 0608 §2-§3, ADR 0635 §2, ADR 0636 §5, ADR 0634 §1-§2): the one reading the indexer keeps
// in its state seat-definitions and the checker lays over the seat list for a case's heads, so both take the same
// definitions from the same modules. It names no transport: the caller's Forge binds the forge's listing and its
// get-file to its own calls.
package seatdefs
import (
"context"
"errors"
"fmt"
"path"
"slices"
"strings"
"git.novox.be/novox/mesh-delivery/contracts/go/clients/meshcatalogclient"
"git.novox.be/novox/mesh-sdk/go/catalogue"
"git.novox.be/novox/mesh-sdk/go/seatdef"
)
// ErrUnanswered wraps a read the forge did not answer (never a file or a directory it does not hold): the caller tries
// again rather than taking the module's definitions as gone.
var ErrUnanswered = errors.New("the forge did not answer")
// Forge is what the reading asks of the forge.
type Forge struct {
// Files lists every file under each directory at a commit, by directory then path relative to it; a directory the
// commit lacks is absent from the answer (no error).
Files func(ctx context.Context, repository, commit string, dirs []string) (map[string]map[string]string, error)
// File reads one file at a commit; a file the commit does not hold is nil with no error.
File func(ctx context.Context, repository, commit, path string) ([]byte, error)
}
// Module is a contracts module: its name, its directory in the repository and its definitions' directory.
type Module struct{ Module, Dir, Defs string }
// Of is the contracts module a manifest in a directory is (a package artifact naming `generated-from`), and whether it
// is one at all.
func Of(dir string, m catalogue.Manifest) (Module, bool) {
if m.Build == nil {
return Module{}, false
}
for _, a := range m.Build.Artifacts {
if a.GeneratedFrom == "" {
continue
}
base := strings.Trim(dir, "/")
if base == meshcatalogclient.RootPath {
base = ""
}
return Module{Module: m.Module, Dir: dir, Defs: path.Clean(path.Join(base, a.GeneratedFrom))}, true
}
return Module{}, false
}
// ContractsModules are the contracts modules among raw manifests by directory, in directory order. A manifest that does
// not decode is no contracts module here: the manifest validator says why it is refused.
func ContractsModules(manifests map[string]string) []Module {
var out []Module
for dir, raw := range manifests {
m, err := catalogue.Decode([]byte(raw))
if err != nil {
continue
}
if c, ok := Of(dir, m); ok {
out = append(out, c)
}
}
slices.SortFunc(out, func(a, b Module) int { return strings.Compare(a.Dir, b.Dir) })
return out
}
// under is a path of the module's definitions' directory.
func (c Module) under(rel string) string {
if c.Defs == "." || c.Defs == "" {
return rel
}
return c.Defs + "/" + rel
}
// Seats names the seats a contracts module defines at a commit: every directory of its definitions' `seats/` that holds
// a `seat.json`, whatever the seat's grain (ADR 0635 §2, §6), from the forge's one listing of that directory. No index
// file names them.
func (f Forge) Seats(ctx context.Context, repository, commit string, c Module) ([]string, error) {
if f.Files == nil {
return nil, fmt.Errorf("%w: no listing of %s's seats", ErrUnanswered, c.Module)
}
dir := c.under("seats")
got, err := f.Files(ctx, repository, commit, []string{dir})
if err != nil {
return nil, fmt.Errorf("%w: the seats of %s at %.12s: %w", ErrUnanswered, c.Module, commit, err)
}
var seats []string
for p := range got[dir] {
if seat, ok := strings.CutSuffix(p, "/seat.json"); ok && seat != "" && !strings.Contains(seat, "/") {
seats = append(seats, seat)
}
}
slices.Sort(seats)
return seats, nil
}
// Read reads a contracts module's definitions at a commit: each seat's definition (Seats) and the parts it includes,
// each resolved. A seat whose definition is missing, does not parse, names another seat than its directory's, or whose
// part does not parse or resolve, is answered in unparsed by its words; a read the forge did not answer is an error
// wrapping ErrUnanswered.
func (f Forge) Read(ctx context.Context, repository, commit string, c Module) (defs map[string]seatdef.Definition, unparsed map[string]string, err error) {
if f.File == nil {
return nil, nil, fmt.Errorf("%w: no reader of %s's definitions", ErrUnanswered, c.Module)
}
file := func(rel string) ([]byte, error) {
p := c.under(rel)
raw, err := f.File(ctx, repository, commit, p)
if err != nil {
return nil, fmt.Errorf("%w: %s at %.12s: %w", ErrUnanswered, p, commit, err)
}
if raw == nil {
return nil, fmt.Errorf("%s at %.12s: the forge holds no such file", p, commit)
}
return raw, nil
}
seats, err := f.Seats(ctx, repository, commit, c)
if err != nil {
return nil, nil, err
}
defs, unparsed = map[string]seatdef.Definition{}, map[string]string{}
parts := map[string]seatdef.Part{}
for _, seat := range seats {
raw, err := file("seats/" + seat + "/seat.json")
if err != nil {
if errors.Is(err, ErrUnanswered) {
return nil, nil, err
}
unparsed[seat] = err.Error()
continue
}
d, err := seatdef.Parse(raw)
if err == nil && d.Seat != seat {
err = fmt.Errorf("it names the seat %q, not %q", d.Seat, seat)
}
if err == nil {
for _, name := range d.Includes {
if _, ok := parts[name]; ok {
continue
}
praw, perr := file("parts/" + name + ".json")
if perr == nil {
var p seatdef.Part
if p, perr = seatdef.ParsePart(praw); perr == nil {
parts[name] = p
}
}
if errors.Is(perr, ErrUnanswered) {
return nil, nil, perr
}
if perr != nil {
err = fmt.Errorf("its part %s: %w", name, perr)
break
}
}
}
if err == nil {
d, err = seatdef.Resolve(d, parts)
}
if err != nil {
unparsed[seat] = err.Error()
continue
}
defs[seat] = d
}
return defs, unparsed, nil
}