The seat generator gives every value, event and answer the wire field form (left out while 1) and a tolerant decoder answering the form it read; each contract carries its forms and a digest of its fields, and the compare fails a definition whose fields changed while its form did not. Every delivery seat is regenerated, and the readers take the form. Builds: design 47 H2 §780; H-A-352
134 lines
4.6 KiB
Go
134 lines
4.6 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 }
|