source-moved is said on source-moved.<branch>.<source> with v, branch, source,
call, start and heads [{repository, commit}], kept 30 days and 64 MiB on its
own stream; its wildcard has two tokens (H-A-26). record-opened is said on
record-opened.<call>; it and record-ended carry branch, source and call, the
milestone <branch>.<source>, no forge-made fields (H-A-25). The source value
carries source and call, the verb source answers call; the state heads keeps
each untaken head {head, since} by <repository>.<branch>. The states merges,
asks and turns, the verb groups, and the events approval-carried, pinned,
freed, pins-stated and heads-replaced go (H-A-28, H-A-48), with their helpers
in meshdeliveryclient.
Builds: design 47 H §737, §740, §749, §764 (the gate's states, groups and switch); lead answers H-A-23, H-A-25, H-A-26, H-A-28, H-A-33, H-A-48
Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
134 lines
4.5 KiB
Go
134 lines
4.5 KiB
Go
package meshdeliveryclient
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"reflect"
|
|
|
|
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery"
|
|
"git.novox.be/novox/mesh-sdk/go/keys"
|
|
)
|
|
|
|
// The values of the seat's states `source` and `owed`, in the seat's types: each is stamped
|
|
// with Version when encoded, read strictly (an unknown field refused) and its version checked when decoded, and its rules
|
|
// held on both sides, so the gate that writes them and the readers (the forge bridge, the teller, the assigner, the view)
|
|
// agree. What the schema cannot say is written here once (ADR 0625 §4).
|
|
|
|
// Version is the layout version of the seat's values.
|
|
const Version = 1
|
|
|
|
// SourceKey is the key of the state source for a watched branch (design 47 *Watched branches and the catalog graph per
|
|
// branch*: source.<branch> in place of source.main).
|
|
func SourceKey(branch string) string { return seat.SourceByBranchKey(branch) }
|
|
|
|
func encodeValue(v any, check error, stamp func()) ([]byte, error) {
|
|
if check != nil {
|
|
return nil, check
|
|
}
|
|
stamp()
|
|
return json.Marshal(v)
|
|
}
|
|
|
|
// ----- source
|
|
|
|
// CheckSource refuses a source naming no repository, a repository with no name or an empty head.
|
|
func CheckSource(s seat.SourceByBranchValue) error {
|
|
if len(s.Heads) == 0 {
|
|
return fmt.Errorf("meshdeliveryclient: a source names no repository")
|
|
}
|
|
for repository, head := range s.Heads {
|
|
if repository == "" {
|
|
return fmt.Errorf("meshdeliveryclient: a source names a repository with no name")
|
|
}
|
|
if head == "" {
|
|
return fmt.Errorf("meshdeliveryclient: the source's head of %s is empty", repository)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EncodeSource is s as the value of the key main.
|
|
func EncodeSource(s seat.SourceByBranchValue) ([]byte, error) {
|
|
return encodeValue(&s, CheckSource(s), func() { s.V = Version })
|
|
}
|
|
|
|
// DecodeSource reads the value of the key main.
|
|
func DecodeSource(data []byte) (seat.SourceByBranchValue, error) {
|
|
if err := keys.CheckVersion(data); err != nil {
|
|
return seat.SourceByBranchValue{}, err
|
|
}
|
|
s, err := seat.DecodeSourceByBranchValue(data)
|
|
if err != nil {
|
|
return s, fmt.Errorf("meshdeliveryclient: a source does not fit its shape: %w", err)
|
|
}
|
|
return s, CheckSource(s)
|
|
}
|
|
|
|
// SameSource says whether two sources are the same.
|
|
func SameSource(a, b seat.SourceByBranchValue) bool { return reflect.DeepEqual(a, b) }
|
|
|
|
// ----- owed
|
|
|
|
// EncodeOwed is o as the value of an owed key, after CheckOwed.
|
|
func EncodeOwed(o seat.OwedByRepositoryNumberHeadActValue) ([]byte, error) {
|
|
return encodeValue(&o, CheckOwed(o), func() { o.V = Version })
|
|
}
|
|
|
|
// DecodeOwed reads the value of an owed key.
|
|
func DecodeOwed(data []byte) (seat.OwedByRepositoryNumberHeadActValue, error) {
|
|
if err := keys.CheckVersion(data); err != nil {
|
|
return seat.OwedByRepositoryNumberHeadActValue{}, err
|
|
}
|
|
o, err := seat.DecodeOwedByRepositoryNumberHeadActValue(data)
|
|
if err != nil {
|
|
return o, fmt.Errorf("meshdeliveryclient: an owed value does not fit its shape: %w", err)
|
|
}
|
|
return o, CheckOwed(o)
|
|
}
|
|
|
|
// SameOwed says whether two owed values are the same.
|
|
func SameOwed(a, b seat.OwedByRepositoryNumberHeadActValue) bool { return a == b }
|
|
|
|
// Answer is the operator's answer to a merge ask as the gate acts on it: the option chosen, the warrant that proves who
|
|
// answered, and the words said. It is the teller's state `answers` under the ask's key (the teller's own record; this is
|
|
// its layout for the gate that reads it).
|
|
type Answer struct {
|
|
V int `json:"v"`
|
|
Option string `json:"option"`
|
|
Warrant string `json:"warrant"`
|
|
Words string `json:"words"`
|
|
}
|
|
|
|
// CheckAnswer refuses an answer lacking its option, warrant or words.
|
|
func CheckAnswer(a Answer) error {
|
|
for _, f := range []struct{ name, value string }{{"option", a.Option}, {"warrant", a.Warrant}, {"words", a.Words}} {
|
|
if f.value == "" {
|
|
return fmt.Errorf("meshdeliveryclient: an answer has no %s", f.name)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EncodeAnswer is a as the value of an answer's key.
|
|
func EncodeAnswer(a Answer) ([]byte, error) {
|
|
return encodeValue(&a, CheckAnswer(a), func() { a.V = Version })
|
|
}
|
|
|
|
// DecodeAnswer reads the value of an answer's key.
|
|
func DecodeAnswer(data []byte) (Answer, error) {
|
|
if err := keys.CheckVersion(data); err != nil {
|
|
return Answer{}, err
|
|
}
|
|
var a Answer
|
|
dec := json.NewDecoder(bytes.NewReader(data))
|
|
dec.DisallowUnknownFields()
|
|
if err := dec.Decode(&a); err != nil {
|
|
return Answer{}, fmt.Errorf("meshdeliveryclient: an answer does not fit its shape: %w", err)
|
|
}
|
|
return a, CheckAnswer(a)
|
|
}
|
|
|
|
// SameAnswer says whether two answers are the same.
|
|
func SameAnswer(a, b Answer) bool { return a == b }
|