Builds: design 47 G A-128 (A-116) Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
88 lines
3.0 KiB
Go
88 lines
3.0 KiB
Go
package catalogue
|
|
|
|
import (
|
|
"sort"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/seatdef"
|
|
)
|
|
|
|
// SeatFromDefinition is a seat in this package's types from its definition (novox/hq ADR 0608 §1, ADR 0635 §1): what
|
|
// the seat list once held, now read from the definition the indexer keeps on the bus. The definition is resolved with
|
|
// its parts (seatdef.Resolve) as the indexer keeps it. A verb's input and answer have their named types in place, and a
|
|
// contribution kind given as data gets its checks (withShapeCode), which are code and never a definition's.
|
|
func SeatFromDefinition(d seatdef.Definition) Seat {
|
|
s := Seat{Name: d.Seat, Scope: d.Scope, Replicated: d.Replicated, Delivers: d.Delivers,
|
|
Accepts: seatdef.MessageNames(d.Accepts), Emits: localEvents(d.Events), PulledByKind: d.PulledByKind,
|
|
Yields: d.Yields, HeldByTheEngine: d.HeldByTheEngine, Decision: d.Decision}
|
|
if len(s.Accepts) == 0 {
|
|
s.Accepts = nil
|
|
}
|
|
if len(s.Emits) == 0 {
|
|
s.Emits = nil
|
|
}
|
|
for _, v := range d.Verbs {
|
|
s.Serves = append(s.Serves, Verb{Name: v.Name, Description: v.Description,
|
|
Input: seatdef.ResolveRefs(v.Input, d.Defs), Output: seatdef.ResolveRefs(v.Answer, d.Defs),
|
|
Optional: v.Optional, Replaces: v.Replaces})
|
|
}
|
|
for _, st := range d.States {
|
|
s.States = append(s.States, st.Name)
|
|
}
|
|
for _, r := range d.Receives {
|
|
rv := Receivable{Kind: r.Kind, Comment: r.Comment, Dirs: r.Dirs, Offered: r.Offered}
|
|
if r.Shape != nil {
|
|
sh := &Shape{Order: r.Shape.Order, Examples: examples(r.Shape.Examples), Text: r.Shape.Text}
|
|
for _, f := range r.Shape.Places {
|
|
sh.Places = append(sh.Places, PlaceField{Field: f.Field, Values: f.Values})
|
|
}
|
|
withShapeCode(d.Seat, r.Kind, sh)
|
|
rv.Shape = sh
|
|
}
|
|
s.Receives = append(s.Receives, rv)
|
|
}
|
|
for _, p := range d.Paths {
|
|
s.Paths = append(s.Paths, SeatPath{Path: p.Path, StepsBackFor: p.StepsBackFor})
|
|
}
|
|
return s
|
|
}
|
|
|
|
// SeatsFromDefinitions are the seats of a set of definitions, in reading order: the mesh's own, then a node's, each by
|
|
// name. A seat its holder declares itself (HolderDeclares) is not in the list, as the compiled list left it out.
|
|
func SeatsFromDefinitions(defs []seatdef.Definition) []Seat {
|
|
out := make([]Seat, 0, len(defs))
|
|
for _, d := range defs {
|
|
if d.HolderDeclares {
|
|
continue
|
|
}
|
|
out = append(out, SeatFromDefinition(d))
|
|
}
|
|
sort.SliceStable(out, func(i, j int) bool {
|
|
if (out[i].Scope == seatdef.ScopeMesh) != (out[j].Scope == seatdef.ScopeMesh) {
|
|
return out[i].Scope == seatdef.ScopeMesh
|
|
}
|
|
return out[i].Name < out[j].Name
|
|
})
|
|
return out
|
|
}
|
|
|
|
// localEvents are the events as a manifest names them: an event whose subject carries parts by its subject with `*`
|
|
// for each part (owed.*.*), so SeatSays matches a consumer's name against it.
|
|
func localEvents(es []seatdef.Message) []string {
|
|
out := make([]string, 0, len(es))
|
|
for _, e := range es {
|
|
out = append(out, e.Local())
|
|
}
|
|
return out
|
|
}
|
|
|
|
func examples(in []map[string]any) []map[string]any {
|
|
if in == nil {
|
|
return nil
|
|
}
|
|
out := make([]map[string]any, len(in))
|
|
for i, e := range in {
|
|
out[i], _ = seatdef.StringLists(e).(map[string]any)
|
|
}
|
|
return out
|
|
}
|