package main import ( "encoding/json" "fmt" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" "sort" "mesh-delivery-view/internal/view" ) // catalogRead is the modules the view's tools answer, read from the catalog half per watched branch through // meshcatalogclient's branch follower (design 47 G §532, §614): every module a followed branch's view lists, each the // entry Branches.Entry reads, the first branch of the module's own registration (A-103), in the form of the value the // tools always answered. Until every watched branch has its first view the read says what it waits for; it never answers // no modules. func catalogRead(b *meshcatalogclient.Branches) view.StateRead { read := func() (map[string]json.RawMessage, error) { if err := b.Ready(); err != nil { return nil, fmt.Errorf("%w (%s)", err, meshcatalogclient.RevisionVerb) } values := map[string]json.RawMessage{} for _, branch := range b.Followed() { c, ok := b.Catalog(branch) if !ok { continue } half, _ := c.View() for module := range half.Entries { if _, done := values[module]; done { continue } e, ok, err := b.Entry(module) if err != nil { return nil, err } if !ok { continue } raw, _ := json.Marshal(e) v, err := view.ModuleValue(raw) if err != nil { return nil, fmt.Errorf("the catalog entry of %s: %w", module, err) } values[module] = v } } return values, nil } 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) { if err := b.Ready(); err != nil { return nil, fmt.Errorf("%w (%s)", err, meshcatalogclient.RevisionVerb) } e, ok, err := b.Entry(key) if err != nil || !ok { return nil, err } raw, _ := json.Marshal(e) return view.ModuleValue(raw) }, All: read, } }