Files
mesh-delivery/modules/mesh-delivery-sdk/seatgen/gogen.go
T
jschoubben 67caa79640 H-A-376: the runner's list answers short rows, show each mover's row
list answers by default the newest 20 short rows (key, call, start, end,
outcome, movers' count and the first failed or could-not-run step with its
module and why), and the whole pipelines only with full and a limit. show
answers one mover's steps when it names one, the steps its filters hold,
or else each mover's row, never every step of every mover. The tools
program describes each seat verb's tool from its contract, which seatgen
now generates a VerbDescription for; runner_cancel reads the whole
pipelines with full. The runner seat's form is 6.

Builds: design 47 H2 H-A-376, H-A-449, §789
2026-11-22 02:14:51 +01:00

602 lines
20 KiB
Go

package seatgen
import (
"bytes"
"encoding/json"
"fmt"
"go/format"
"sort"
"strconv"
"strings"
"git.novox.be/novox/mesh-sdk/go/seatdef"
)
const header = "// Code generated by seatgen from the seat definitions; DO NOT EDIT.\n\n"
func goFiles(m *Module, out map[string][]byte) error {
// Each seat defined whole is a Go module of its own, so a change of one seat moves only its importers, whose
// fingerprint counts a Go module's go.mod and go.sum (ADR 0625 §1, §2). A seat's module imports nothing: the parts it
// includes are written into it whole. There is no seat list and no root Go module (ADR 0635 §1): the list is read
// from the bus.
for _, name := range m.Order {
d := m.Seats[name]
if d.Grain != seatdef.Whole {
continue
}
src, test, err := goWhole(m, d)
if err != nil {
return fmt.Errorf("seat %s: %w", name, err)
}
dir := "go/seats/" + pkgName(name) + "/"
out[dir+"go.mod"] = goMod(SeatGoModule(m.GoModule, name))
out[dir+"contract.go"] = src
if test != nil {
out[dir+"contract_test.go"] = test
}
}
return nil
}
// SeatGoModule is the Go module of a seat defined whole, beside the seat set's.
func SeatGoModule(root, seat string) string { return root + "/seats/" + pkgName(seat) }
func goMod(path string) []byte { return []byte("module " + path + "\n\ngo 1.26.0\n") }
func sortedKeys[V any](m map[string]V) []string {
out := make([]string, 0, len(m))
for k := range m {
out = append(out, k)
}
sort.Strings(out)
return out
}
func gofmt(b *bytes.Buffer) ([]byte, error) {
src, err := format.Source(b.Bytes())
if err != nil {
return nil, fmt.Errorf("the generated Go does not parse: %w\n%s", err, b.String())
}
return src, nil
}
// declareAll declares a definition's types: its own named types, then each verb's input and answer, each accept,
// event, holder event, record and provision; a name given by a part (in fromPart) is an alias of the part's.
func declareAll(d seatdef.Definition, fromPart map[string]string) (*typer, error) {
tp := newTyper()
decl := func(name, desc string, s map[string]any) error {
if s == nil {
return nil
}
if _, ok := fromPart[name]; ok {
tp.taken[name] = true
return nil
}
return tp.declare(name, desc, s)
}
for _, n := range d.DefNames() {
if err := decl(n, "", d.Defs[n]); err != nil {
return nil, err
}
}
// A body of `bodies` yields its type alone: the sending is mesh-sdk's (design 47 G §452, §454).
for _, n := range sortedKeys(d.Bodies) {
if err := decl(n, "", d.Bodies[n]); err != nil {
return nil, err
}
}
for _, v := range d.Verbs {
if err := decl(ident(v.Name)+"Input", "is what "+v.Name+" takes.", v.Input); err != nil {
return nil, err
}
if err := decl(ident(v.Name)+"Answer", "is what "+v.Name+" answers.", v.Answer); err != nil {
return nil, err
}
if v.Answer != nil {
tp.readBy = append(tp.readBy, ident(v.Name)+"Answer")
}
}
for _, a := range d.Accepts {
if err := decl(ident(acceptName(a))+"Accept", "is the body of "+acceptName(a)+".", a.Body); err != nil {
return nil, err
}
if a.Body != nil {
tp.accepts = append(tp.accepts, ident(acceptName(a))+"Accept")
}
}
for _, e := range d.Events {
if err := decl(ident(e.Name)+"Event", "is the body of the event "+e.Name+".", e.Body); err != nil {
return nil, err
}
if e.Body != nil {
tp.readBy = append(tp.readBy, ident(e.Name)+"Event")
}
}
for _, e := range d.HolderEvents {
if err := decl(ident(e.Name)+"HolderEvent", "is the body of the holder's event "+e.Name+".", e.Body); err != nil {
return nil, err
}
if e.Body != nil {
tp.readBy = append(tp.readBy, ident(e.Name)+"HolderEvent")
}
}
for _, st := range d.States {
for _, kf := range st.KeyForms {
vn, err := stateValueName(st.Name, kf)
if err != nil {
return nil, fmt.Errorf("state %s: %w", st.Name, err)
}
if err := decl(vn, "is one value of the state "+st.Name+" under the key "+kf.Key+".", kf.Value); err != nil {
return nil, err
}
if kf.Value != nil {
tp.readBy = append(tp.readBy, vn)
tp.values[vn] = true
}
}
}
for _, p := range d.Provisions {
if err := decl(ident(p.Name)+"Form", "is the form of the provision "+p.Name+".", p.Form); err != nil {
return nil, err
}
}
tp.addForms()
return tp, nil
}
func oneLine(s string) string {
s = strings.TrimSpace(s)
if s == "" {
return "no description."
}
return strings.ReplaceAll(s, "\n", " ")
}
func writeNames(b *bytes.Buffer, prefix string, names []string) {
if len(names) == 0 {
return
}
b.WriteString("const (\n")
for _, n := range names {
fmt.Fprintf(b, "\t%s%s = %q\n", prefix, ident(n), n)
}
b.WriteString(")\n\n")
}
func writeList(b *bytes.Buffer, fn, prefix, what string, names []string) {
if len(names) == 0 {
return
}
fmt.Fprintf(b, "// %s is %s: a new copy each call.\nfunc %s() []string {\n\treturn []string{", fn, what, fn)
for i, n := range names {
if i > 0 {
b.WriteString(", ")
}
b.WriteString(prefix + ident(n))
}
b.WriteString("}\n}\n\n")
}
// writeVerbDescriptions writes VerbDescription, each verb's description as the seat's definition says it, so a tools
// program serving the seat's verbs as tools describes each from the one copy (design 47 H2 H-A-449).
func writeVerbDescriptions(b *bytes.Buffer, verbs []seatdef.Verb) {
if len(verbs) == 0 {
return
}
b.WriteString("// VerbDescription is a verb's description as the seat's definition says it, \"\" for a verb the seat does not serve or\n// does not describe (design 47 H2 H-A-449).\nfunc VerbDescription(verb string) string {\n\tswitch verb {\n")
for _, v := range verbs {
if v.Description == "" {
continue
}
fmt.Fprintf(b, "\tcase Verb%s:\n\t\treturn %q\n", ident(v.Name), v.Description)
}
b.WriteString("\t}\n\treturn \"\"\n}\n\n")
}
// goWhole is a whole seat's package: its names, subjects and types, and the test of its fixtures.
func goWhole(m *Module, d seatdef.Definition) ([]byte, []byte, error) {
fromPart := map[string]string{}
tp, err := declareAll(d, fromPart)
if err != nil {
return nil, nil, err
}
pkg := pkgName(d.Seat)
var b bytes.Buffer
b.WriteString(header)
fmt.Fprintf(&b, "// Package %s is the contract of the seat %q, defined whole (seats/%s/seat.json): %s\npackage %s\n\n",
pkg, d.Seat, d.Seat, oneLine(d.Description), pkg)
if len(tp.readBy) > 0 {
b.WriteString(goStatesImports(d.States))
}
fmt.Fprintf(&b, "// Seat is the seat's name.\nconst Seat = %q\n\n", d.Seat)
writeForms(&b, m, d, tp)
writeNames(&b, "Verb", seatdef.VerbNames(d.Verbs))
writeNames(&b, "Accept", acceptNames(d.Accepts))
writeNames(&b, "Event", seatdef.MessageNames(d.Events))
writeList(&b, "Verbs", "Verb", "every verb the seat serves, in its definition's order", seatdef.VerbNames(d.Verbs))
writeVerbDescriptions(&b, d.Verbs)
writeList(&b, "Events", "Event", "every event published on the seat's subject", seatdef.MessageNames(d.Events))
if f := d.Family; f != nil {
fmt.Fprintf(&b, "// Family is where the seat's events are kept: the shared events stream, filtered to the seat's subjects.\nconst (\n\tFamily = %q\n\tFamilyMaxAgeDays = %d\n)\n\n", f.Name, f.MaxAgeDays)
}
for _, e := range d.Events {
subj := seatdef.EventSubject(d.Seat, e.Name)
if e.Keep != nil {
fmt.Fprintf(&b, "// Stream%s is the stream %s is kept in.\nconst Stream%s = %q\n\n", ident(e.Name), e.Name, ident(e.Name), keptStream(d.Seat, e.Name))
}
if parts := e.Parts(); len(parts) > 0 {
goPartsSubject(&b, d.Seat, e, parts)
continue
}
if e.ByCaller {
fmt.Fprintf(&b, "// Subject%s is where %s is published for one caller.\nfunc Subject%s(caller string) string { return %q + caller }\n\n",
ident(e.Name), e.Name, ident(e.Name), subj+".")
} else {
fmt.Fprintf(&b, "// Subject%s is where %s is published.\nconst Subject%s = %q\n\n", ident(e.Name), e.Name, ident(e.Name), subj)
}
}
for _, a := range d.Accepts {
subj := acceptSubject(d.Seat, a)
if a.ByCaller {
fmt.Fprintf(&b, "// AcceptSubject%s is where one caller sends %s.\nfunc AcceptSubject%s(caller string) string { return %q + caller }\n\n",
ident(acceptName(a)), acceptName(a), ident(acceptName(a)), subj+".")
} else {
fmt.Fprintf(&b, "// AcceptSubject%s is where %s is sent.\nconst AcceptSubject%s = %q\n\n", ident(acceptName(a)), acceptName(a), ident(acceptName(a)), subj)
}
}
if len(d.HolderEvents) > 0 {
b.WriteString("// The holder's own events, listed beside the seat and published under the holder's subjects (ADR 0620 §2).\nconst (\n")
for _, e := range d.HolderEvents {
fmt.Fprintf(&b, "\tHolderEvent%s = %q\n", ident(e.Name), e.Name)
}
b.WriteString(")\n\n")
for _, e := range d.HolderEvents {
before, after, _ := strings.Cut(e.Subject, "<holder>")
fmt.Fprintf(&b, "// HolderSubject%s is where the holder publishes %s", ident(e.Name), e.Name)
if e.Family != "" {
fmt.Fprintf(&b, ", kept in %s", e.Family)
}
fmt.Fprintf(&b, ".\nfunc HolderSubject%s(holder string) string { return %q + holder + %q }\n\n", ident(e.Name), before, after)
}
}
if len(d.States) > 0 {
if err := goStates(&b, d.States); err != nil {
return nil, nil, err
}
}
if err := goTypes(&b, tp, map[string]*typ{}); err != nil {
return nil, nil, err
}
goDecoders(&b, tp)
if bytes.Contains(b.Bytes(), []byte("time.Time")) {
// A moment is the standard library's time (the only import a contract may have besides its states' readers).
raw := b.Bytes()
if bytes.Contains(raw, []byte("\nimport (\n")) {
raw = bytes.Replace(raw, []byte("\nimport (\n"), []byte("\nimport (\n\t\"time\"\n"), 1)
} else {
raw = bytes.Replace(raw, []byte("package "+pkg+"\n\n"), []byte("package "+pkg+"\n\nimport \"time\"\n\n"), 1)
}
b = *bytes.NewBuffer(raw)
}
src, err := gofmt(&b)
if err != nil {
return nil, nil, err
}
test, err := goFixtures(m, d, pkg, tp, fromPart)
return src, test, err
}
// goTypes writes every declared type; also are the kinds of types declared elsewhere (a part's), by name.
func goTypes(b *bytes.Buffer, tp *typer, also map[string]*typ) error {
kinds := map[string]*typ{}
for k, v := range also {
kinds[k] = v
}
for _, t := range tp.types {
kinds[t.name] = t.t
}
for _, t := range tp.types {
desc := strings.TrimSpace(t.desc)
if desc == "" {
desc = "is a type of the contract."
} else if !strings.HasPrefix(desc, "is ") {
desc = "is " + strings.TrimSuffix(desc, ".") + "."
}
fmt.Fprintf(b, "// %s %s\n", t.name, oneLine(desc))
if t.t.kind != "struct" {
fmt.Fprintf(b, "type %s = %s\n\n", t.name, goType(t.t, kinds, true))
goEnum(b, t.name, t.name, t.t)
continue
}
fmt.Fprintf(b, "type %s struct {\n", t.name)
for _, f := range t.t.fields {
if f.desc != "" {
fmt.Fprintf(b, "\t// %s\n", oneLine(f.desc))
}
tag := f.json
if !f.required {
tag += ",omitempty"
}
fmt.Fprintf(b, "\t%s %s `json:%q`\n", ident(f.json), goType(f.t, kinds, f.required), tag)
}
b.WriteString("}\n\n")
for _, f := range t.t.fields {
goEnum(b, t.name+"'s "+f.json, t.name+ident(f.json), f.t)
}
}
return nil
}
// goEnum writes a string's enum as one constant per value, named prefix and the value (RemoveInputReasonRetention), so a
// caller names a value the definition gives and never writes it by hand.
func goEnum(b *bytes.Buffer, what, prefix string, t *typ) {
if t.kind != "string" || len(t.enum) == 0 {
return
}
fmt.Fprintf(b, "// The values of %s.\nconst (\n", what)
for _, v := range t.enum {
fmt.Fprintf(b, "\t%s%s = %q\n", prefix, ident(v), v)
}
b.WriteString(")\n\n")
}
func goType(t *typ, kinds map[string]*typ, required bool) string {
if t.null {
n := *t
n.null = false
return "*" + goType(&n, kinds, true)
}
switch t.kind {
case "string":
return "string"
case "time":
if required {
return "time.Time"
}
return "*time.Time"
case "integer":
return "int64"
case "number":
return "float64"
case "boolean":
return "bool"
case "object":
return "map[string]any"
case "map":
return "map[string]" + goType(t.elem, kinds, true)
case "slice":
return "[]" + goType(t.elem, kinds, true)
case "ref":
if k, ok := kinds[t.ref]; ok && k.kind == "struct" && !required {
return "*" + t.ref
}
return t.ref
}
return "any"
}
// goFixtures is the test that every fixture of the seat decodes into its type and reads back as its wire says, and
// that the subject it names is the one the contract gives.
func goFixtures(m *Module, d seatdef.Definition, pkg string, tp *typer, fromPart map[string]string) ([]byte, error) {
fx := m.Fixtures[d.Seat]
if len(fx) == 0 {
return nil, nil
}
has := map[string]bool{}
for _, t := range tp.types {
has[t.name] = true
}
for n := range fromPart {
has[n] = true
}
var b bytes.Buffer
b.WriteString(header)
fmt.Fprintf(&b, "package %s\n\nimport (\n\t\"encoding/json\"\n\t\"reflect\"\n\t\"regexp\"\n\t\"testing\"\n)\n\n", pkg)
fmt.Fprintf(&b, "// TestFixtures reads every fixture of seats/%s/fixtures: each decodes into its type, a field the type does not\n"+
"// know is dropped (ADR 0608 §6), and it reads back as its wire says; each subject named is the contract's.\n", d.Seat)
b.WriteString("func TestFixtures(t *testing.T) {\n\tcases := []struct {\n\t\tname, subject, given, wire string\n\t\tdecode func([]byte) (any, error)\n\t\tsubjectIs *regexp.Regexp\n\t}{\n")
for _, f := range fx {
kind, name, _ := f.Instance()
typeName, pattern, err := fixtureTarget(d, kind, name)
if err != nil {
return nil, fmt.Errorf("fixture %q: %w", f.Name, err)
}
given, _ := json.Marshal(f.Given)
wire := given
if f.Wire != nil {
wire, _ = json.Marshal(f.Wire)
}
decoder := "func(b []byte) (any, error) { var v map[string]any; err := json.Unmarshal(b, &v); return v, err }"
if has[typeName] {
decoder = fmt.Sprintf("func(b []byte) (any, error) { var v %s; err := json.Unmarshal(b, &v); return v, err }", typeName)
}
subjectIs := "nil"
if pattern != "" {
subjectIs = "regexp.MustCompile(" + strconv.Quote(pattern) + ")"
} else if f.Subject != "" {
return nil, fmt.Errorf("fixture %q names a subject, and a verb's message has none in the contract", f.Name)
}
if pattern != "" && f.Subject == "" {
return nil, fmt.Errorf("fixture %q of %s names no subject (design 26: each event's subject is named)", f.Name, f.Of)
}
fmt.Fprintf(&b, "\t\t{name: %q, subject: %q, given: %s, wire: %s,\n\t\t\tdecode: %s, subjectIs: %s},\n",
f.Name, f.Subject, strconv.Quote(string(given)), strconv.Quote(string(wire)), decoder, subjectIs)
}
b.WriteString(` }
for _, c := range cases {
v, err := c.decode([]byte(c.given))
if err != nil {
t.Errorf("%s: does not decode: %v", c.name, err)
continue
}
back, err := json.Marshal(v)
if err != nil {
t.Errorf("%s: does not encode: %v", c.name, err)
continue
}
var got, want any
if err := json.Unmarshal(back, &got); err != nil {
t.Fatal(err)
}
if err := json.Unmarshal([]byte(c.wire), &want); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("%s: reads back as %s, want %s", c.name, back, c.wire)
}
if c.subjectIs != nil && !c.subjectIs.MatchString(c.subject) {
t.Errorf("%s: subject %q is not the contract's %s", c.name, c.subject, c.subjectIs)
}
}
}
`)
return gofmt(&b)
}
// fixtureTarget is the type a fixture decodes into and the pattern its subject must match, if it has one.
func fixtureTarget(d seatdef.Definition, kind, name string) (string, string, error) {
switch kind {
case "verb-input", "verb-answer":
for _, v := range d.Verbs {
if v.Name == name {
if kind == "verb-input" {
return ident(name) + "Input", "", nil
}
return ident(name) + "Answer", "", nil
}
}
case "accept", "event":
ms, subj := d.Accepts, seatdef.AcceptSubject(d.Seat, name)
suffix := "Accept"
if kind == "event" {
ms, subj, suffix = d.Events, seatdef.EventSubject(d.Seat, name), "Event"
}
for _, msg := range ms {
if (kind == "event" && msg.Name == name) || (kind == "accept" && acceptName(msg) == name) {
if kind == "event" && len(msg.Parts()) > 0 {
return ident(name) + suffix, partsPattern(d.Seat, msg), nil
}
if kind == "accept" {
subj = acceptSubject(d.Seat, msg)
}
p := "^" + regexp(subj)
if msg.ByCaller {
p += `\.[A-Za-z0-9_-]+`
}
return ident(name) + suffix, p + "$", nil
}
}
case "state":
if n, ok := stateFormFor(d, name); ok {
return n, "", nil
}
case "holder-event":
for _, e := range d.HolderEvents {
if e.Name == name {
before, after, _ := strings.Cut(e.Subject, "<holder>")
p := "^" + regexp(before)
if strings.Contains(e.Subject, "<holder>") {
p += "[a-z0-9-]+" + regexp(after)
}
return ident(name) + "HolderEvent", p + "$", nil
}
}
}
return "", "", fmt.Errorf("the definition has no %s %q", kind, name)
}
// acceptName is an accepted kind's name as the generated names carry it: the plain kind, or for a kind on a stream
// `<stream>.<kind>` (design 47 G §455), so `step` on `check` and on `delivery` stay two; its subject is acceptSubject.
func acceptName(m seatdef.Message) string {
if m.Stream != "" {
return m.Stream + "." + m.Name
}
return m.Name
}
// acceptSubject is where an accepted kind is sent: `accept.<kind>`, or for a kind on a stream its queue alone,
// `accept.<stream>`, since the kind is in the body only (design 47 G §600, A-170); `step` on `check` and `check` share it.
func acceptSubject(seat string, m seatdef.Message) string {
if m.Stream != "" {
return seatdef.AcceptSubject(seat, m.Stream)
}
return seatdef.AcceptSubject(seat, m.Name)
}
func acceptNames(ms []seatdef.Message) []string {
out := make([]string, len(ms))
for i, m := range ms {
out[i] = acceptName(m)
}
return out
}
func regexp(s string) string {
var b strings.Builder
for _, r := range s {
if strings.ContainsRune(`\.+*?()|[]{}^$`, r) {
b.WriteRune('\\')
}
b.WriteRune(r)
}
return b.String()
}
// keptStream is the bus's name of the stream an event naming `keep` is kept in: `SEAT_<seat>_<event>`, which the bus
// takes (no dot) and the grants part makes (design 47 G §260, §396).
func keptStream(seat, event string) string { return "SEAT_" + seat + "_" + event }
// subjectExpr is the Go or JavaScript string expression of an event's subject with its parts as arguments.
func subjectExpr(seat string, e seatdef.Message) []string {
var expr []string
lit := "mesh.seat." + seat + ".event."
for i, tok := range strings.Split(e.SubjectPattern(), ".") {
if i > 0 {
lit += "."
}
if strings.HasPrefix(tok, "<") {
expr = append(expr, fmt.Sprintf("%q", lit), goParam(tok[1:len(tok)-1]))
lit = ""
continue
}
lit += tok
}
if lit != "" {
expr = append(expr, fmt.Sprintf("%q", lit))
}
return expr
}
// goPartsSubject writes the subject of an event whose subject carries parts (design 47 G §260, §397): a function taking
// each part in order, and the filter that hears every instance of it.
func goPartsSubject(b *bytes.Buffer, seat string, e seatdef.Message, parts []string) {
args := make([]string, len(parts))
for i, p := range parts {
args[i] = goParam(p)
}
fmt.Fprintf(b, "// Subject%s is where %s is published, on %s.\nfunc Subject%s(%s string) string { return %s }\n\n",
ident(e.Name), e.Name, "mesh.seat."+seat+".event."+e.SubjectPattern(), ident(e.Name), strings.Join(args, ", "),
strings.Join(subjectExpr(seat, e), " + "))
fmt.Fprintf(b, "// Subject%sAll hears every %s.\nconst Subject%sAll = %q\n\n", ident(e.Name), e.Name, ident(e.Name),
"mesh.seat."+seat+".event."+e.Local())
}
// goParam is a part's name as a parameter: its words joined, the first lower-case.
func goParam(part string) string {
id := ident(part)
return strings.ToLower(id[:1]) + id[1:]
}
// partsPattern is the pattern a fixture's subject of an event with parts must match: a part is one token.
func partsPattern(seat string, e seatdef.Message) string {
toks := strings.Split(e.SubjectPattern(), ".")
for i, tok := range toks {
if strings.HasPrefix(tok, "<") {
toks[i] = `[^.*> ]+`
} else {
toks[i] = regexp(tok)
}
}
return "^" + regexp("mesh.seat."+seat+".event.") + strings.Join(toks, `\.`) + "$"
}