Files
mesh-sdk/go/busstate/busstate.go
T
jschoubben c9fec158cb A3 sdk: a bundle uses its own object store through the runtime (ADR 0595 §2, §9, design 25 § 12.5)
stdio.BusState asks the runtime one buscalls method on the bundle's own bus state; package busstate makes the object
store's put, get, list, delete and fill by the store's local name (A3's request 1, distribution's incoming). Its test
takes the place of one merged into its neighbour, so mesh-sdk's unit count does not grow.

Claude-Session: https://claude.ai/code/session_01JG73KJNrH5EgFXMKKYSrig
2026-11-04 12:06:55 +01:00

60 lines
2.3 KiB
Go

// Package busstate is how a bundle uses its own bus state through the runtime (novox/hq ADR 0595 §9, design 25 §
// 12.5): the object store's put, get, list, delete and fill, each naming an entry by the local name its module asks for
// it by in `requires`. The runtime serves the calls from the module's own bindings, its own entries only. The shapes
// are package buscalls's; this package makes the calls.
package busstate
import (
"encoding/json"
"fmt"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/buscalls"
)
// Objects is one object store the module asks for in `requires` (the template objects, ADR 0595 §2), by its local name.
type Objects struct{ local string }
// ObjectsOf is the object store of that local name.
func ObjectsOf(local string) Objects { return Objects{local: local} }
func call[T any](method string, params any) (T, error) {
var out T
raw, err := stdio.BusState(method, params)
if err != nil {
return out, err
}
if err := json.Unmarshal(raw, &out); err != nil {
return out, fmt.Errorf("the runtime's answer to %s: %w", method, err)
}
return out, nil
}
// Put stores one object whole under its name, replacing one of that name, and answers it as the store holds it.
func (o Objects) Put(name string, data []byte, description string) (buscalls.ObjectInfo, error) {
return call[buscalls.ObjectInfo](buscalls.ObjectsPut, buscalls.ObjectsPutRequest{Objects: o.local, Name: name,
Data: data, Description: description})
}
// Get is one object, its info and its bytes.
func (o Objects) Get(name string) (buscalls.ObjectData, error) {
return call[buscalls.ObjectData](buscalls.ObjectsGet, buscalls.ObjectsNameRequest{Objects: o.local, Name: name})
}
// List is every object the store holds.
func (o Objects) List() ([]buscalls.ObjectInfo, error) {
l, err := call[buscalls.ObjectsListed](buscalls.ObjectsList, buscalls.ObjectsRequest{Objects: o.local})
return l.Objects, err
}
// Delete removes one object.
func (o Objects) Delete(name string) error {
_, err := call[buscalls.Done](buscalls.ObjectsDelete, buscalls.ObjectsNameRequest{Objects: o.local, Name: name})
return err
}
// Fill is how much the store holds against its cap.
func (o Objects) Fill() (buscalls.Fill, error) {
return call[buscalls.Fill](buscalls.ObjectsInfo, buscalls.ObjectsRequest{Objects: o.local})
}