The generator wrote `accept.<stream>.<kind>` for a kind that rides a stream (step on check and delivery, run on check), so the runner's step landed under the named worker `accept.check.*` with `step` read as its asker. A-170 has the kind in the body only: the generated accept subject of a streamed kind is the queue's, `accept.<stream>`; its generated names stay `<stream>.<kind>`. The three build seats' contracts and fixtures are regenerated. Builds: design 47 G §600, A-170, A-184 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
261 lines
9.2 KiB
Go
261 lines
9.2 KiB
Go
package seatgen
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/seatdef"
|
|
)
|
|
|
|
const tsHeader = "// Code generated by seatgen from the seat definitions; DO NOT EDIT.\n\n"
|
|
|
|
// TSSeatDir is where a seat defined whole has its npm package (ADR 0625 §1): the indexer counts the npm package a
|
|
// lock resolves, so each such seat is a package of its own and a module is rebuilt only for the seats it uses.
|
|
func TSSeatDir(seat string) string { return "ts/seats/" + pkgName(seat) }
|
|
|
|
// TSSeatPackage is the npm package name of a seat defined whole, beside the seat set's package root.
|
|
func TSSeatPackage(root, seat string) string { return root + "-" + seat }
|
|
|
|
// tsPackage writes one npm package's package.json and its dependency-free package-lock.json at dir. The package ships
|
|
// its contract as JavaScript with its declarations (design 47 G §341): its importers need no build step of its own and
|
|
// nothing runs tsc over it, so it has no tsconfig.json and no script (F7: a generated package has no dependency, and its
|
|
// lock, which the build module requires of a package artifact, holds none).
|
|
func tsPackage(out map[string][]byte, dir, name, description string) {
|
|
pkg := map[string]any{
|
|
"name": name, "version": "0.1.0", "type": "module",
|
|
"description": description,
|
|
"exports": map[string]any{".": map[string]any{"types": "./index.d.ts", "default": "./index.js"}},
|
|
"types": "./index.d.ts",
|
|
"files": []any{"index.js", "index.d.ts", "parts"},
|
|
}
|
|
raw, _ := json.MarshalIndent(pkg, "", " ")
|
|
out[dir+"/package.json"] = append(raw, '\n')
|
|
lock := map[string]any{
|
|
"name": name, "version": "0.1.0", "lockfileVersion": 3, "requires": true,
|
|
"packages": map[string]any{"": map[string]any{"name": name, "version": "0.1.0"}},
|
|
}
|
|
raw, _ = json.MarshalIndent(lock, "", " ")
|
|
out[dir+"/package-lock.json"] = append(raw, '\n')
|
|
}
|
|
|
|
// tsOut is one contract file as its two forms: the JavaScript module a TypeScript module imports and the declarations
|
|
// that type it (design 47 G §341).
|
|
type tsOut struct{ js, dts bytes.Buffer }
|
|
|
|
// tsFiles writes the npm side: one package per seat defined whole at TSSeatDir (ADR 0625 §1), holding the parts it
|
|
// includes whole and importing nothing outside it. Each file is a JavaScript module with its declarations beside it
|
|
// (`index.js` and `index.d.ts`, `parts/<part>.js` and `parts/<part>.d.ts`; design 47 G §341).
|
|
func tsFiles(m *Module, out map[string][]byte) error {
|
|
partTypes := map[string]*typer{}
|
|
partFiles := map[string]*tsOut{}
|
|
for _, name := range sortedKeys(m.Parts) {
|
|
p := m.Parts[name]
|
|
d := seatdef.Definition{Grain: seatdef.Whole, Verbs: p.Verbs, Events: p.Events, Defs: p.Defs}
|
|
tp, err := declareAll(d, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
partTypes[name] = tp
|
|
o := &tsOut{}
|
|
o.js.WriteString(tsHeader)
|
|
o.dts.WriteString(tsHeader)
|
|
note := fmt.Sprintf("// The part %q several seats include whole: %s\n\n", p.Part, oneLine(p.Description))
|
|
o.js.WriteString(note)
|
|
o.dts.WriteString(note)
|
|
tsNames(o, "verbs", seatdef.VerbNames(p.Verbs))
|
|
tsNames(o, "events", seatdef.MessageNames(p.Events))
|
|
o.js.WriteString("export {};\n")
|
|
tsTypes(&o.dts, tp)
|
|
partFiles[name] = o
|
|
}
|
|
for _, name := range m.Order {
|
|
d := m.Seats[name]
|
|
if d.Grain != seatdef.Whole {
|
|
continue
|
|
}
|
|
dir := TSSeatDir(name)
|
|
tsPackage(out, dir, TSSeatPackage(m.TSPackage, name), fmt.Sprintf("The contract of the seat %s, generated from its definition.", name))
|
|
raw := m.Raw[name]
|
|
fromPart := map[string]string{}
|
|
for _, inc := range raw.Includes {
|
|
for _, t := range partTypes[inc].types {
|
|
fromPart[t.name] = inc
|
|
}
|
|
out[dir+"/parts/"+inc+".js"] = partFiles[inc].js.Bytes()
|
|
out[dir+"/parts/"+inc+".d.ts"] = partFiles[inc].dts.Bytes()
|
|
}
|
|
tp, err := declareAll(d, fromPart)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
o := &tsOut{}
|
|
o.js.WriteString(tsHeader)
|
|
o.dts.WriteString(tsHeader)
|
|
note := fmt.Sprintf("// The contract of the seat %q, defined whole (seats/%s/seat.json): %s\n\n", d.Seat, d.Seat, oneLine(d.Description))
|
|
o.js.WriteString(note)
|
|
o.dts.WriteString(note)
|
|
for _, inc := range raw.Includes {
|
|
fmt.Fprintf(&o.dts, "import type * as %s from \"./parts/%s.js\";\n\n", tsName(inc), inc)
|
|
}
|
|
fmt.Fprintf(&o.js, "export const seat = %q;\n\n", d.Seat)
|
|
fmt.Fprintf(&o.dts, "export declare const seat: %q;\n\n", d.Seat)
|
|
tsNames(o, "verbs", seatdef.VerbNames(d.Verbs))
|
|
tsNames(o, "accepts", acceptNames(d.Accepts))
|
|
tsNames(o, "events", seatdef.MessageNames(d.Events))
|
|
if f := d.Family; f != nil {
|
|
fmt.Fprintf(&o.js, "export const family = { name: %q, maxAgeDays: %d };\n\n", f.Name, f.MaxAgeDays)
|
|
fmt.Fprintf(&o.dts, "export declare const family: { readonly name: %q; readonly maxAgeDays: %d };\n\n", f.Name, f.MaxAgeDays)
|
|
}
|
|
for _, e := range d.Events {
|
|
if e.Keep != nil {
|
|
fmt.Fprintf(&o.js, "export const %sStream = %q;\n\n", tsName(e.Name), keptStream(d.Seat, e.Name))
|
|
fmt.Fprintf(&o.dts, "export declare const %sStream: %q;\n\n", tsName(e.Name), keptStream(d.Seat, e.Name))
|
|
}
|
|
if parts := e.Parts(); len(parts) > 0 {
|
|
tsPartsSubject(o, d.Seat, e, parts)
|
|
continue
|
|
}
|
|
tsSubject(o, tsName(e.Name), seatdef.EventSubject(d.Seat, e.Name), e.ByCaller)
|
|
}
|
|
for _, a := range d.Accepts {
|
|
tsSubject(o, tsName(acceptName(a)), acceptSubject(d.Seat, a), a.ByCaller)
|
|
}
|
|
if len(d.HolderEvents) > 0 {
|
|
tsNames(o, "holderEvents", holderNames(d.HolderEvents))
|
|
for _, e := range d.HolderEvents {
|
|
before, after, _ := strings.Cut(e.Subject, "<holder>")
|
|
fmt.Fprintf(&o.js, "export function %sHolderSubject(holder) {\n return %q + holder + %q;\n}\n\n", tsName(e.Name), before, after)
|
|
fmt.Fprintf(&o.dts, "export declare function %sHolderSubject(holder: string): string;\n\n", tsName(e.Name))
|
|
}
|
|
}
|
|
if len(d.States) > 0 {
|
|
if err := tsStates(o, d.States); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
for _, n := range sortedKeys(fromPart) {
|
|
fmt.Fprintf(&o.dts, "export type %s = %s.%s;\n", n, tsName(fromPart[n]), n)
|
|
}
|
|
if len(fromPart) > 0 {
|
|
o.dts.WriteString("\n")
|
|
}
|
|
tsTypes(&o.dts, tp)
|
|
out[dir+"/index.js"] = o.js.Bytes()
|
|
out[dir+"/index.d.ts"] = o.dts.Bytes()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// tsSubject writes the constant, or for a subject ending in the caller's name the function, of one subject.
|
|
func tsSubject(o *tsOut, name, subj string, byCaller bool) {
|
|
if byCaller {
|
|
fmt.Fprintf(&o.js, "export function %sSubject(caller) {\n return %q + caller;\n}\n\n", name, subj+".")
|
|
fmt.Fprintf(&o.dts, "export declare function %sSubject(caller: string): string;\n\n", name)
|
|
return
|
|
}
|
|
fmt.Fprintf(&o.js, "export const %sSubject = %q;\n\n", name, subj)
|
|
fmt.Fprintf(&o.dts, "export declare const %sSubject: %q;\n\n", name, subj)
|
|
}
|
|
|
|
func holderNames(es []seatdef.HolderEvent) []string {
|
|
out := make([]string, len(es))
|
|
for i, e := range es {
|
|
out[i] = e.Name
|
|
}
|
|
return out
|
|
}
|
|
|
|
func tsNames(o *tsOut, what string, names []string) {
|
|
if len(names) == 0 {
|
|
return
|
|
}
|
|
fmt.Fprintf(&o.js, "export const %s = {\n", what)
|
|
fmt.Fprintf(&o.dts, "export declare const %s: {\n", what)
|
|
for _, n := range names {
|
|
fmt.Fprintf(&o.js, " %s: %q,\n", tsName(n), n)
|
|
fmt.Fprintf(&o.dts, " readonly %s: %q;\n", tsName(n), n)
|
|
}
|
|
o.js.WriteString("};\n\n")
|
|
o.dts.WriteString("};\n\n")
|
|
}
|
|
|
|
func tsTypes(b *bytes.Buffer, tp *typer) {
|
|
for _, t := range tp.types {
|
|
if d := strings.TrimSpace(t.desc); d != "" {
|
|
fmt.Fprintf(b, "/** %s */\n", oneLine(d))
|
|
}
|
|
if t.t.kind != "struct" {
|
|
fmt.Fprintf(b, "export type %s = %s;\n\n", t.name, tsType(t.t))
|
|
continue
|
|
}
|
|
fmt.Fprintf(b, "export interface %s {\n", t.name)
|
|
for _, f := range t.t.fields {
|
|
if f.desc != "" {
|
|
fmt.Fprintf(b, " /** %s */\n", oneLine(f.desc))
|
|
}
|
|
opt := "?"
|
|
if f.required {
|
|
opt = ""
|
|
}
|
|
fmt.Fprintf(b, " %s%s: %s;\n", strconv.Quote(f.json), opt, tsType(f.t))
|
|
}
|
|
b.WriteString("}\n\n")
|
|
}
|
|
}
|
|
|
|
func tsType(t *typ) string {
|
|
if t.null {
|
|
n := *t
|
|
n.null = false
|
|
return tsType(&n) + " | null"
|
|
}
|
|
switch t.kind {
|
|
case "string":
|
|
if len(t.enum) > 0 {
|
|
parts := make([]string, len(t.enum))
|
|
for i, e := range t.enum {
|
|
parts[i] = strconv.Quote(e)
|
|
}
|
|
return strings.Join(parts, " | ")
|
|
}
|
|
return "string"
|
|
case "time":
|
|
return "string"
|
|
case "integer", "number":
|
|
return "number"
|
|
case "boolean":
|
|
return "boolean"
|
|
case "object":
|
|
return "Record<string, unknown>"
|
|
case "map":
|
|
return "Record<string, " + tsType(t.elem) + ">"
|
|
case "slice":
|
|
el := tsType(t.elem)
|
|
if strings.Contains(el, "|") {
|
|
el = "(" + el + ")"
|
|
}
|
|
return el + "[]"
|
|
case "ref":
|
|
return t.ref
|
|
}
|
|
return "unknown"
|
|
}
|
|
|
|
// tsPartsSubject writes the subject of an event whose subject carries parts, as goPartsSubject does.
|
|
func tsPartsSubject(o *tsOut, seat string, e seatdef.Message, parts []string) {
|
|
args := make([]string, len(parts))
|
|
names := make([]string, len(parts))
|
|
for i, p := range parts {
|
|
names[i] = goParam(p)
|
|
args[i] = names[i] + ": string"
|
|
}
|
|
all := "mesh.seat." + seat + ".event." + e.Local()
|
|
fmt.Fprintf(&o.js, "export function %sSubject(%s) {\n return %s;\n}\n\n", tsName(e.Name), strings.Join(names, ", "), strings.Join(subjectExpr(seat, e), " + "))
|
|
fmt.Fprintf(&o.js, "export const %sSubjectAll = %q;\n\n", tsName(e.Name), all)
|
|
fmt.Fprintf(&o.dts, "export declare function %sSubject(%s): string;\n\n", tsName(e.Name), strings.Join(args, ", "))
|
|
fmt.Fprintf(&o.dts, "export declare const %sSubjectAll: %q;\n\n", tsName(e.Name), all)
|
|
}
|