63 lines
2.3 KiB
Go
63 lines
2.3 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
|
|
}
|