Files

94 lines
3.2 KiB
Go

package judgement
import (
"context"
"errors"
"fmt"
"github.com/samcharles93/archie-core/modules/connectors"
)
// Reversibility sets a ceiling from whether the action can be undone, as the
// connector declares it.
type Reversibility struct{}
// Name is the guard's name in verdicts.
func (Reversibility) Name() string { return "reversibility" }
// Advisory is false: the declaration is data, not a model's opinion.
func (Reversibility) Advisory() bool { return false }
// Judge maps the declared reversibility to a ceiling. An action that declares
// none lacks the evidence this guard needs.
func (Reversibility) Judge(_ context.Context, a Action) (Verdict, error) {
switch {
case a.Effect.ReadOnly:
return Verdict{Ceiling: ActQuietly}, nil
case a.Effect.Reversibility == connectors.UndoneAutomatically:
return Verdict{Ceiling: ActQuietly, Findings: []Finding{{Text: "undone automatically"}}}, nil
case a.Effect.Reversibility == connectors.Recoverable || a.Target.Disposable:
return Verdict{Ceiling: ActAndTell, Findings: []Finding{{Text: "recoverable, or the target is disposable"}}}, nil
case a.Effect.Reversibility == connectors.Irreversible:
return Verdict{Ceiling: ProposeAndWait, Findings: []Finding{{Text: "irreversible"}}}, nil
}
return Verdict{}, errors.New("the connector declares no reversibility for this action")
}
// ClassLevels says what level an action class stands at.
type ClassLevels interface {
Level(ctx context.Context, c Class, readOnly bool) (Level, error)
}
// Precedent sets a ceiling from the action class's current level. A class
// starts at its default (read-only classes act quietly, every other class
// proposes and waits) and stays wherever its history has put it.
type Precedent struct {
Classes ClassLevels
}
// Name is the guard's name in verdicts.
func (Precedent) Name() string { return "precedent" }
// Advisory is false.
func (Precedent) Advisory() bool { return false }
// Judge returns the class's level.
func (p Precedent) Judge(ctx context.Context, a Action) (Verdict, error) {
level, err := p.Classes.Level(ctx, a.Class(), a.Effect.ReadOnly)
if err != nil {
return Verdict{}, fmt.Errorf("read class level: %w", err)
}
return Verdict{Ceiling: level, Findings: []Finding{{Text: "this action class is at " + level.String()}}}, nil
}
// Freezes says whether the organisation has frozen its actions.
type Freezes interface {
Frozen(ctx context.Context) (bool, error)
}
// Freeze holds every action that changes something at propose and wait while
// the organisation is frozen. It never changes a class's level, so lifting the
// freeze puts every class back where it stood.
type Freeze struct {
Freezes Freezes
}
// Name is the guard's name in verdicts.
func (Freeze) Name() string { return "freeze" }
// Advisory is false.
func (Freeze) Advisory() bool { return false }
// Judge returns the freeze's ceiling.
func (f Freeze) Judge(ctx context.Context, a Action) (Verdict, error) {
frozen, err := f.Freezes.Frozen(ctx)
if err != nil {
return Verdict{}, fmt.Errorf("read freeze: %w", err)
}
v := Verdict{Ceiling: freezeCeiling(frozen, a.Effect.ReadOnly)}
if v.Ceiling != ActQuietly {
v.Findings = []Finding{{Text: "the organisation is frozen"}}
}
return v, nil
}