Documentation ¶
Index ¶
- Variables
- type Action
- type ErrAlreadyInState
- type ErrInvalidTransition
- type ErrNotInState
- type Machine
- func (m *Machine[T]) Check() validation.ErrorGroup
- func (m *Machine[T]) Current() T
- func (*Machine[T]) Generate(rand *rand.Rand, size int) reflect.Value
- func (m *Machine[T]) In(s T) bool
- func (m *Machine[T]) MustBe(s T, a Action) error
- func (m *Machine[T]) String() string
- func (m *Machine[T]) To(s T, t Transition) error
- type Transition
Constants ¶
This section is empty.
Variables ¶
var ErrStatesMustBeSet = validation.NewError(
"machine_states_must_be_set",
"the machine states must be set and cannot be an empty",
)
ErrStatesMustBeSet is the validation check error returned when the machine states are empty.
var NoOp = func() error { return nil }
Functions ¶
This section is empty.
Types ¶
type ErrAlreadyInState ¶
ErrAlreadyInState returned when machine attempts to transition into its current state.
func (*ErrAlreadyInState[T]) Error ¶
func (e *ErrAlreadyInState[T]) Error() string
type ErrInvalidTransition ¶
ErrInvalidTransition returned when call requests a transition between states that isn't registered.
func (*ErrInvalidTransition[T]) Error ¶
func (e *ErrInvalidTransition[T]) Error() string
type ErrNotInState ¶
ErrNotInState returned when caller asserts a machine to be in a specific state and it isn't.
func (*ErrNotInState[T]) Error ¶
func (e *ErrNotInState[T]) Error() string
type Machine ¶
Machine represents thread-safe state machine.
func New ¶
func New[T state.State]( initial T, states []T, table transition.Table[T], ) (*Machine[T], validation.ErrorGroup)
New creates and validates a new Machine.
func (*Machine[T]) Check ¶
func (m *Machine[T]) Check() validation.ErrorGroup
Check runs default validation checks for the Machine.
func (*Machine[T]) Current ¶
func (m *Machine[T]) Current() T
Current returns the current state of the machine.
func (*Machine[T]) MustBe ¶
MustBe invokes the given action if the machine is in the expected state.
If not in the expected state, ErrNotInState is returned.
func (*Machine[T]) To ¶
func (m *Machine[T]) To(s T, t Transition) error
To transitions the state machine to the given state.
type Transition ¶
type Transition func() error