169 lines
7.1 KiB
Go
169 lines
7.1 KiB
Go
package meshcatalogclient
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
|
|
seat "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshcatalog"
|
|
"git.novox.be/novox/mesh-sdk/go/keys"
|
|
)
|
|
|
|
// The states of a verdict on a manifest (novox/hq ADR 0578 §1): the indexer judges every manifest it takes in once and
|
|
// every other part takes the verdict from the seat's state `verdicts`.
|
|
const (
|
|
// VerdictAccepted is the state of a manifest the judge found no problem in.
|
|
VerdictAccepted = "accepted"
|
|
// VerdictRefused is the state of a manifest the judge found a problem in; the verdict's problems say each.
|
|
VerdictRefused = "refused"
|
|
// VerdictManifestWaits is the state of a manifest that carries a field the judge does not know: it is not refused,
|
|
// because a newer judge may know the field; the verdict's unknown names each. A reader holds the module as a refused
|
|
// one and keeps what runs. In a `modules` value it is written as a refusal naming the fields.
|
|
VerdictManifestWaits = "waits"
|
|
)
|
|
|
|
// VerdictsKey is the key of a `verdicts` value: the digest of the manifest it judges.
|
|
func VerdictsKey(digest string) (string, error) {
|
|
if !keys.IsHex64(digest) {
|
|
return "", fmt.Errorf("meshcatalogclient: %q is not a manifest digest, 64 lowercase hex digits", digest)
|
|
}
|
|
return seat.VerdictsByDigestKey(digest), nil
|
|
}
|
|
|
|
// VerdictRefuses says whether v refuses its manifest.
|
|
func VerdictRefuses(v seat.VerdictsByDigestValue) bool { return v.State == VerdictRefused }
|
|
|
|
// VerdictWaits says whether v says the manifest waits for a judge that knows its fields.
|
|
func VerdictWaits(v seat.VerdictsByDigestValue) bool { return v.State == VerdictManifestWaits }
|
|
|
|
// VerdictHeld says whether a reader holds the module as refused: the manifest is refused or waits.
|
|
func VerdictHeld(v seat.VerdictsByDigestValue) bool { return VerdictRefuses(v) || VerdictWaits(v) }
|
|
|
|
// WaitsOn says, in words, what a waiting manifest waits on.
|
|
func WaitsOn(v seat.VerdictsByDigestValue) string {
|
|
return "it carries " + strings.Join(v.Unknown, ", ") + ", which the indexer's judge does not know yet"
|
|
}
|
|
|
|
// TakeVerdict is how a reader takes the verdict on a manifest (ADR 0578 §3): held, with its problems, when the indexer
|
|
// wrote a refusal, or, for a manifest that waits, with the words that say which fields it waits on. A missing verdict (nil)
|
|
// is taken as accepted: every manifest the indexer holds before it has judged it was taken in under the judges that ran
|
|
// until then. A reader holds a refused module and names it with VerdictWhy, and reads the rest.
|
|
func TakeVerdict(v *seat.VerdictsByDigestValue) (refused bool, problems []string) {
|
|
if v == nil || !VerdictHeld(*v) {
|
|
return false, nil
|
|
}
|
|
if VerdictWaits(*v) {
|
|
return true, []string{WaitsOn(*v)}
|
|
}
|
|
return true, v.Problems
|
|
}
|
|
|
|
// VerdictWhy is a refusal in a reader's words: the module, that the indexer's judge refused its manifest, and each problem.
|
|
func VerdictWhy(v seat.VerdictsByDigestValue) string {
|
|
if !VerdictHeld(v) {
|
|
return ""
|
|
}
|
|
judge := "the indexer's judge"
|
|
if v.Judge != "" {
|
|
judge += " (" + v.Judge + ")"
|
|
}
|
|
if VerdictWaits(v) {
|
|
return fmt.Sprintf("%s holds the manifest of %s: %s", judge, v.Module, WaitsOn(v))
|
|
}
|
|
why := fmt.Sprintf("%s refused the manifest of %s:", judge, v.Module)
|
|
for _, p := range v.Problems {
|
|
why += " " + p + ";"
|
|
}
|
|
return why[:len(why)-1]
|
|
}
|
|
|
|
// VerdictAsRefused is v as a reader that does not know the waiting state takes it: a waiting verdict is a refusal naming what
|
|
// it waits on, any other verdict is v.
|
|
func VerdictAsRefused(v seat.VerdictsByDigestValue) seat.VerdictsByDigestValue {
|
|
if !VerdictWaits(v) {
|
|
return v
|
|
}
|
|
v.State, v.Problems, v.Unknown = VerdictRefused, []string{WaitsOn(v)}, nil
|
|
return v
|
|
}
|
|
|
|
// SameVerdict says whether v and o say the same of a manifest: the module, the state, the problems, the unknown fields and
|
|
// the warnings, whoever judged and when. The indexer writes a verdict only when it is not the same as the one it holds.
|
|
func SameVerdict(v, o seat.VerdictsByDigestValue) bool {
|
|
return v.Module == o.Module && v.State == o.State && slices.Equal(v.Problems, o.Problems) && slices.Equal(v.Unknown, o.Unknown) && slices.Equal(v.Warnings, o.Warnings)
|
|
}
|
|
|
|
func checkVerdict(v seat.VerdictsByDigestValue) error {
|
|
if err := keys.CheckName("module", v.Module); err != nil {
|
|
return err
|
|
}
|
|
if v.State != VerdictManifestWaits && len(v.Unknown) > 0 {
|
|
return fmt.Errorf("meshcatalogclient: the verdict on %s names unknown fields and does not wait", v.Module)
|
|
}
|
|
switch v.State {
|
|
case VerdictAccepted:
|
|
if len(v.Problems) > 0 {
|
|
return fmt.Errorf("meshcatalogclient: the verdict on %s accepts its manifest and names problems", v.Module)
|
|
}
|
|
case VerdictRefused:
|
|
if len(v.Problems) == 0 {
|
|
return fmt.Errorf("meshcatalogclient: the verdict on %s refuses its manifest and names no problem", v.Module)
|
|
}
|
|
if len(v.Warnings) > 0 {
|
|
return fmt.Errorf("meshcatalogclient: the verdict on %s refuses its manifest and warns: a refusal names problems", v.Module)
|
|
}
|
|
case VerdictManifestWaits:
|
|
if len(v.Unknown) == 0 || len(v.Problems) > 0 || len(v.Warnings) > 0 {
|
|
return fmt.Errorf("meshcatalogclient: the verdict on %s waits and names no unknown field, or names problems or warnings: a wait names the fields", v.Module)
|
|
}
|
|
default:
|
|
return fmt.Errorf("meshcatalogclient: the verdict on %s is %q, none of %s, %s and %s", v.Module, v.State, VerdictAccepted, VerdictRefused, VerdictManifestWaits)
|
|
}
|
|
if v.Judge == "" {
|
|
return fmt.Errorf("meshcatalogclient: the verdict on %s names no judge", v.Module)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EncodeVerdict is v as JSON at the kept values' version; it refuses a verdict without a module or a judge, a state other
|
|
// than accepted, refused or waits, a refusal naming no problem and an acceptance naming one.
|
|
func EncodeVerdict(v seat.VerdictsByDigestValue) ([]byte, error) {
|
|
if err := checkVerdict(v); err != nil {
|
|
return nil, err
|
|
}
|
|
v.V = keys.Version
|
|
return json.Marshal(v)
|
|
}
|
|
|
|
// DecodeVerdict reads a `verdicts` value; it refuses an unknown version (keys.ErrUnknownVersion) and what EncodeVerdict
|
|
// refuses.
|
|
func DecodeVerdict(data []byte) (seat.VerdictsByDigestValue, error) {
|
|
if err := keys.CheckVersion(data); err != nil {
|
|
return seat.VerdictsByDigestValue{}, err
|
|
}
|
|
var v seat.VerdictsByDigestValue
|
|
if err := json.Unmarshal(data, &v); err != nil {
|
|
return seat.VerdictsByDigestValue{}, fmt.Errorf("meshcatalogclient: value does not fit its shape: %w", err)
|
|
}
|
|
if err := checkVerdict(v); err != nil {
|
|
return seat.VerdictsByDigestValue{}, err
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
// ModuleVerdict is the verdict a `modules` value carries, or nil when it carries none (taken as accepted, TakeVerdict). A
|
|
// refusal naming unknown fields is the wait it was written as.
|
|
func ModuleVerdict(module string, m seat.ModulesByModuleValue) *seat.VerdictsByDigestValue {
|
|
if m.Verdict == "" && len(m.Warnings) == 0 {
|
|
return nil
|
|
}
|
|
if m.Verdict == "" {
|
|
return &seat.VerdictsByDigestValue{Module: module, State: VerdictAccepted, Warnings: m.Warnings}
|
|
}
|
|
if m.Verdict == VerdictRefused && len(m.Unknown) > 0 {
|
|
return &seat.VerdictsByDigestValue{Module: module, State: VerdictManifestWaits, Unknown: m.Unknown}
|
|
}
|
|
return &seat.VerdictsByDigestValue{Module: module, State: m.Verdict, Problems: m.Problems}
|
|
}
|