Files
mesh-catalog/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/values.go
T
jschoubben d786477a0d GATE7 (source part, §690): the gate's source answers a branch's source key, or the branches it keeps
The gate's source value is the contract's SourceByBranchValue and its key SourceKey(branch); the verb takes an optional
branch and answers branches alone when given none. The whole-module package does not compile on the join for GATE11
(settle) and GATE17 (ledger fill and remove), so the unit suite was run with those five sites stubbed in a scratch edit,
never committed: green apart from the stubbed removal case.

Builds: design 47 G §690, §614, §15

Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
2026-11-11 21:35:26 +01:00

139 lines
4.5 KiB
Go

package main
import (
"encoding/json"
"reflect"
mdc "git.novox.be/novox/mesh-delivery/contracts/go/clients/meshdeliveryclient"
seat "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshdelivery"
)
// The holders the gate reads or hears, by the names their modules go by (the gate's own constants, one per reader: the
// manifest's reads and consumes name them).
const (
tellerModule = "mesh-delivery-teller"
viewModule = "mesh-delivery-view"
forgeBridgeModule = "mesh-delivery-forge-bridge"
)
// The values the gate keeps in the seat mesh-delivery's states. The seat's types are the layout and meshdeliveryclient
// holds their rules and codecs; Source, Owed and Answer are used as they are, and the three whose seat form differs from the
// form the gate works in (a pull request that holds its head, the router's ask as bytes, a turn's times as plain times)
// are converted at the edge, so a stored value decodes unchanged (novox/hq ADR 0634 §2).
type (
// Source is the state source's value.
Source = seat.SourceByBranchValue
// Owed is the state owed's value: what the forge bridge performs.
Owed = seat.OwedByRepositoryNumberHeadActValue
// Answer is the operator's answer as the gate acts on it.
Answer = mdc.Answer
)
// Merge is the state merges' value: a group's merge.
type Merge struct {
V int
Group string
Ask string
Approved map[string]string
Pulls []MergePull
}
// MergePull is one pull request of a merge: its expected head, its state, the merge commit once merged, the forge's words
// when refused.
type MergePull struct {
mdc.Head
State mdc.MergeState
Commit string
Words string
}
func (m Merge) stored() seat.MergesByGroupValue {
out := seat.MergesByGroupValue{Approved: m.Approved, Ask: m.Ask, Group: m.Group, V: int64(m.V)}
if m.Pulls != nil {
out.Pulls = make([]seat.MergesByGroupValuePullsItem, len(m.Pulls))
}
for i, p := range m.Pulls {
out.Pulls[i] = seat.MergesByGroupValuePullsItem{Commit: p.Commit, Head: p.Head.Head, Number: int64(p.Number),
Repository: p.Repository, State: string(p.State), Words: p.Words}
}
return out
}
func mergeFromSeat(s seat.MergesByGroupValue) Merge {
out := Merge{V: int(s.V), Group: s.Group, Ask: s.Ask, Approved: s.Approved}
for _, p := range s.Pulls {
out.Pulls = append(out.Pulls, MergePull{Head: mdc.Head{Repository: p.Repository, Number: int(p.Number), Head: p.Head},
State: mdc.MergeState(p.State), Commit: p.Commit, Words: p.Words})
}
return out
}
// EncodeMerge is m as the value of a merges key.
func EncodeMerge(m Merge) ([]byte, error) { return mdc.EncodeMerge(m.stored()) }
// DecodeMerge reads the value of a merges key.
func DecodeMerge(data []byte) (Merge, error) {
s, err := mdc.DecodeMerge(data)
if err != nil {
return Merge{}, err
}
return mergeFromSeat(s), nil
}
// SameMerge says whether two merges are the same value.
func SameMerge(a, b Merge) bool { return mdc.SameMerge(a.stored(), b.stored()) }
// Ask is the state asks' value: the router's ask for a group's merge at its heads.
type Ask struct {
V int
Group string
Heads []mdc.Head
Ask json.RawMessage
// Asking counts the times the gate wrote this group's ask at these heads (0 or 1 for the first).
Asking int
}
func (a Ask) stored() seat.AsksByMergeGroupHeadsValue {
out := seat.AsksByMergeGroupHeadsValue{Asking: int64(a.Asking), Group: a.Group, V: int64(a.V)}
if len(a.Ask) > 0 {
out.Ask = a.Ask
}
if a.Heads != nil {
out.Heads = make([]seat.AsksByMergeGroupHeadsValueHeadsItem, len(a.Heads))
}
for i, h := range a.Heads {
out.Heads[i] = seat.AsksByMergeGroupHeadsValueHeadsItem{Head: h.Head, Number: int64(h.Number), Repository: h.Repository}
}
return out
}
func askFromSeat(s seat.AsksByMergeGroupHeadsValue) (Ask, error) {
raw, err := json.Marshal(s.Ask)
if err != nil {
return Ask{}, err
}
return Ask{V: int(s.V), Group: s.Group, Heads: mdc.HeadsOf(s.Heads), Ask: raw, Asking: int(s.Asking)}, nil
}
// EncodeAsk is a as the value of an asks key.
func EncodeAsk(a Ask) ([]byte, error) { return mdc.EncodeAsk(a.stored()) }
// DecodeAsk reads the value of an asks key.
func DecodeAsk(data []byte) (Ask, error) {
s, err := mdc.DecodeAsk(data)
if err != nil {
return Ask{}, err
}
return askFromSeat(s)
}
// SameAsk says whether two asks are the same value, the router's ask by its content.
func SameAsk(a, b Ask) bool {
var x, y any
if json.Unmarshal(a.Ask, &x) != nil || json.Unmarshal(b.Ask, &y) != nil {
return false
}
a.Ask, b.Ask = nil, nil
return reflect.DeepEqual(a, b) && reflect.DeepEqual(x, y)
}