controllogic: add Graph.StateVars declarations (persisted via store JSON)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Martino Ferrari
2026-06-24 14:46:01 +02:00
parent e76865d132
commit 3ddffc14d7
2 changed files with 37 additions and 0 deletions
+4
View File
@@ -72,6 +72,10 @@ type Graph struct {
Nodes []Node `json:"nodes"`
Wires []Wire `json:"wires"`
Groups []NodeGroup `json:"groups,omitempty"`
// StateVars declares graph-local variables (scalar or array). Live values are
// instantiated in memory per generation from these declarations; only the
// declarations persist. Mirrors the panel-logic statevars feature.
StateVars []StateVar `json:"statevars,omitempty"`
}
func (n Node) param(key string) string {
+33
View File
@@ -0,0 +1,33 @@
package controllogic
import "testing"
func TestStoreRoundTripStateVars(t *testing.T) {
dir := t.TempDir()
st, err := NewStore(dir) // NewStore takes the storage DIRECTORY
if err != nil {
t.Fatal(err)
}
g := Graph{
ID: "g1",
Name: "with-vars",
StateVars: []StateVar{
{Name: "count", Type: "number", Initial: "0"},
{Name: "buf", Type: "array", Initial: "[1,2]", Elem: "number", Sizing: "capped", Capacity: 5},
},
}
if err := st.Save(g); err != nil { // Save returns only error
t.Fatal(err)
}
st2, err := NewStore(dir)
if err != nil {
t.Fatal(err)
}
got, err := st2.Get("g1") // Get returns (Graph, error); ErrNotFound if absent
if err != nil {
t.Fatal(err)
}
if len(got.StateVars) != 2 || got.StateVars[1].Name != "buf" || got.StateVars[1].Capacity != 5 {
t.Fatalf("statevars not round-tripped: %#v", got.StateVars)
}
}