seatlist.HolderNodesIn: each holder of a seat with its module and its node
Builds: design 47 H2 H-A-742
This commit is contained in:
@@ -85,3 +85,47 @@ func HoldersIn[V ~[]byte](values func() (map[string]V, error)) func(seat string)
|
||||
return out
|
||||
}
|
||||
}
|
||||
|
||||
// HolderNode is one holder of a seat: its module and the node it holds it on ("" for a seat held once for the mesh).
|
||||
type HolderNode struct {
|
||||
Module, Node string
|
||||
}
|
||||
|
||||
// HolderNodesIn is HoldersIn answering each holder of a seat with its module and its node, from the same seat-holders view: the
|
||||
// node is the suffix of the view's key `<seat>.<node>`, so no module reads the view's keys itself (design 47 H2 H-A-742). Sorted by
|
||||
// node then module; none while the view cannot be read or nobody holds the seat.
|
||||
func HolderNodesIn[V ~[]byte](values func() (map[string]V, error)) func(seat string) []HolderNode {
|
||||
return func(seat string) []HolderNode {
|
||||
keys, err := values()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
var out []HolderNode
|
||||
for key, raw := range keys {
|
||||
if key != seat && !strings.HasPrefix(key, seat+".") {
|
||||
continue
|
||||
}
|
||||
node := strings.TrimPrefix(strings.TrimPrefix(key, seat), ".")
|
||||
var held struct {
|
||||
Holders []struct {
|
||||
Module string `json:"module"`
|
||||
} `json:"holders"`
|
||||
}
|
||||
if json.Unmarshal([]byte(raw), &held) != nil {
|
||||
continue
|
||||
}
|
||||
for _, h := range held.Holders {
|
||||
if n := (HolderNode{Module: h.Module, Node: node}); h.Module != "" && !slices.Contains(out, n) {
|
||||
out = append(out, n)
|
||||
}
|
||||
}
|
||||
}
|
||||
slices.SortFunc(out, func(a, b HolderNode) int {
|
||||
if c := strings.Compare(a.Node, b.Node); c != 0 {
|
||||
return c
|
||||
}
|
||||
return strings.Compare(a.Module, b.Module)
|
||||
})
|
||||
return out
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package seatlist
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@@ -31,3 +32,28 @@ func TestHolderOf(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Design 47 H2 H-A-742: HolderNodesIn answers each holder of a seat with its module and its node, from the keys `<seat>.<node>`
|
||||
// of the seat-holders view; a seat held once has no node; another seat's key is no holder; HoldersIn still answers the modules.
|
||||
func TestHolderNodesInAnswersEachHolderWithItsNode(t *testing.T) {
|
||||
view := map[string][]byte{
|
||||
"go-package-registry.ace": []byte(`{"holders":[{"module":"registry"}]}`),
|
||||
"go-package-registry.shanks": []byte(`{"holders":[{"module":"registry"},{"module":"mirror"}]}`),
|
||||
"go-package-registry-extra.x": []byte(`{"holders":[{"module":"other"}]}`),
|
||||
"mesh-delivery": []byte(`{"holders":[{"module":"gate"}]}`),
|
||||
}
|
||||
values := func() (map[string][]byte, error) { return view, nil }
|
||||
want := []HolderNode{{"registry", "ace"}, {"mirror", "shanks"}, {"registry", "shanks"}}
|
||||
if got := HolderNodesIn(values)("go-package-registry"); !slices.Equal(got, want) {
|
||||
t.Errorf("holders with their nodes %v, want %v", got, want)
|
||||
}
|
||||
if got := HolderNodesIn(values)("mesh-delivery"); !slices.Equal(got, []HolderNode{{"gate", ""}}) {
|
||||
t.Errorf("a seat held once: %v", got)
|
||||
}
|
||||
if got := HoldersIn(values)("go-package-registry"); len(got) != 2 {
|
||||
t.Errorf("HoldersIn answers %v, want the two modules", got)
|
||||
}
|
||||
if got := HolderNodesIn(func() (map[string][]byte, error) { return nil, errors.New("unread") })("go-package-registry"); got != nil {
|
||||
t.Errorf("an unread view answers %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user