package viewstate import ( "bytes" "encoding/json" "fmt" "strings" "time" "git.novox.be/novox/mesh-sdk/go/keys" ) // WaitsState is the view's own state `waits`: every wait a part holds, as the parts say it (design 47 G §512, §376, A-242), one // key `...` each, written by the view's process when it hears `wait-held` and deleted when it hears // `wait-ended`, and read by view_tiers through the view's own read tool. const WaitsState = "waits" // Wait is the value of a waits key: the fields of the seat mesh-wants' wait (§498, §512, §629) as the part that holds it said // them. The view writes no words of its own: Words is the sayer's. type Wait struct { // Owner is the seat or module that says the wait. Owner string `json:"owner"` Step string `json:"step"` Node string `json:"node,omitempty"` Module string `json:"module,omitempty"` On string `json:"on,omitempty"` Bound string `json:"bound,omitempty"` Since time.Time `json:"since"` Words string `json:"words,omitempty"` Reason string `json:"reason,omitempty"` Part string `json:"part,omitempty"` Version string `json:"version,omitempty"` Fingerprint string `json:"fingerprint,omitempty"` Machines []string `json:"machines,omitempty"` } // WaitKey is the key of a wait: owner, step, node and module, each as a key part (an absent one is `-`). func WaitKey(owner, step, node, module string) string { part := func(s string) string { if s == "" { return "-" } return keys.EscapeName(s) } return strings.Join([]string{part(owner), part(step), part(node), part(module)}, ".") } // Key is the wait's key. func (w Wait) Key() string { return WaitKey(w.Owner, w.Step, w.Node, w.Module) } // ReadWait reads the body of a wait-held or wait-ended said by owner (the seat or module of its subject): the owner is the // body's own when it names one. A body with no step or a field the wait does not know is refused. func ReadWait(owner string, body []byte) (Wait, error) { var w Wait dec := json.NewDecoder(bytes.NewReader(body)) if err := dec.Decode(&w); err != nil { return Wait{}, fmt.Errorf("the wait does not read: %w", err) } if w.Owner == "" { w.Owner = owner } if w.Step == "" || w.Owner == "" { return Wait{}, fmt.Errorf("the wait names no step or no owner") } return w, nil }