54 lines
1.5 KiB
Go
54 lines
1.5 KiB
Go
package meshmoduleassignerclient
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
|
|
seat "git.novox.be/novox/mesh-contracts/placement/go/seats/meshmoduleassigner"
|
|
)
|
|
|
|
// SeatHoldersKey is a seat's key in the state `seat-holders`: `<seat>` for a seat held once for the mesh (node empty),
|
|
// `<seat>.<node>` for a node seat on one node.
|
|
func SeatHoldersKey(name, node string) (string, error) {
|
|
if err := checkName("seat", name); err != nil {
|
|
return "", err
|
|
}
|
|
if node == "" {
|
|
return seat.SeatHoldersBySeatKey(name), nil
|
|
}
|
|
if err := checkName("node", node); err != nil {
|
|
return "", err
|
|
}
|
|
return seat.SeatHoldersBySeatNodeKey(name, node), nil
|
|
}
|
|
|
|
// HoldersOf reads who holds the seat named (on node, for a node seat) from the state's keys as a reader watched them: the
|
|
// node's key first, then the seat's. None when nobody holds it.
|
|
func HoldersOf(keys map[string][]byte, name, node string) ([]seat.SeatHoldersHoldersItem, error) {
|
|
for _, n := range []string{node, ""} {
|
|
key, err := SeatHoldersKey(name, n)
|
|
if err != nil {
|
|
if n == "" {
|
|
return nil, err
|
|
}
|
|
continue
|
|
}
|
|
raw, ok := keys[key]
|
|
if !ok {
|
|
continue
|
|
}
|
|
var sh seat.SeatHolders
|
|
if err := json.Unmarshal(raw, &sh); err != nil {
|
|
return nil, fmt.Errorf("meshmoduleassignerclient: the holders of %s are not readable: %w", key, err)
|
|
}
|
|
if sh.V != Version {
|
|
return nil, fmt.Errorf("meshmoduleassignerclient: the holders of %s are of version %d, and this reads %d", key, sh.V, Version)
|
|
}
|
|
return sh.Holders, nil
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
var _ = strings.Cut
|