The delivery repository's contracts and SDK live under modules/ as mesh-delivery-contracts and mesh-delivery-sdk, their Go module paths renamed to match the folders (the TypeScript names already did), and every importer in mesh-delivery, mesh-catalog, mesh-host, mesh-tools and mesh-sdk follows in the same group: a design as it should be, one path per module, checked by the group's check building every importer. Builds: design 47 G §427, §88 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
84 lines
2.3 KiB
Go
84 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
|
|
"sort"
|
|
"sync"
|
|
"time"
|
|
|
|
"mesh-delivery-view/internal/view"
|
|
)
|
|
|
|
// catalogWithin is how long a call waits for the seat mesh-catalog's first view before it says there is none.
|
|
var catalogWithin = 30 * time.Second
|
|
|
|
// catalogRead is the modules the view's tools answer, read from the catalog half through the seat mesh-catalog's verb
|
|
// `revision` (design 47, Two graphs): the trunk's newest revision, asked once per call and in pages, each entry in the
|
|
// form of the value the tools always answered. With no view the read says so; it never answers no modules.
|
|
func catalogRead(ask func(key string, body any) (json.RawMessage, error)) view.StateRead {
|
|
var (
|
|
once sync.Once
|
|
values map[string]json.RawMessage
|
|
rev uint64
|
|
failed error
|
|
)
|
|
load := func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), catalogWithin)
|
|
defer cancel()
|
|
c, err := meshcatalogclient.NewCatalog(ask, "main")
|
|
if err != nil {
|
|
failed = err
|
|
return
|
|
}
|
|
if err := c.Start(ctx); err != nil {
|
|
failed = fmt.Errorf("the catalog half has no first view yet (%s): %w", meshcatalogclient.RevisionVerb, err)
|
|
return
|
|
}
|
|
half, ok := c.View()
|
|
if !ok {
|
|
failed = fmt.Errorf("the catalog half has no first view yet (%s)", meshcatalogclient.RevisionVerb)
|
|
return
|
|
}
|
|
rev, values = uint64(half.Revision.Revision), map[string]json.RawMessage{}
|
|
for module, e := range half.Entries {
|
|
raw, _ := json.Marshal(e)
|
|
v, err := view.ModuleValue(raw)
|
|
if err != nil {
|
|
failed = fmt.Errorf("the catalog entry of %s: %w", module, err)
|
|
return
|
|
}
|
|
values[module] = v
|
|
}
|
|
}
|
|
read := func() (map[string]json.RawMessage, uint64, error) {
|
|
once.Do(load)
|
|
return values, rev, failed
|
|
}
|
|
return view.StateRead{
|
|
Keys: func() ([]string, error) {
|
|
v, _, err := read()
|
|
keys := make([]string, 0, len(v))
|
|
for k := range v {
|
|
keys = append(keys, k)
|
|
}
|
|
sort.Strings(keys)
|
|
return keys, err
|
|
},
|
|
Get: func(key string) (json.RawMessage, error) {
|
|
v, _, err := read()
|
|
return v[key], err
|
|
},
|
|
GetEntry: func(key string) (json.RawMessage, uint64, error) {
|
|
v, r, err := read()
|
|
return v[key], r, err
|
|
},
|
|
All: func() (map[string]json.RawMessage, error) {
|
|
v, _, err := read()
|
|
return v, err
|
|
},
|
|
}
|
|
}
|