Documentation
¶
Overview ¶
Package fsm provides a generic finite state machine (FSM) implementation with support for transitions, guards, and enter/exit callbacks. It is built with types and utilities from the github.com/enetx/g library. The base FSM is NOT concurrent-safe. For concurrent use, wrap it using the .Sync() method.
Index ¶
- type Callback
- type Context
- type ErrAmbiguousTransition
- type ErrCallback
- type ErrInvalidTransition
- type ErrUnknownState
- type Event
- type FSM
- func (f *FSM) CallEnter(state State) error
- func (f *FSM) Clone() *FSM
- func (f *FSM) Context() *Context
- func (f *FSM) Current() State
- func (f *FSM) History() g.Slice[State]
- func (f *FSM) MarshalJSON() ([]byte, error)
- func (f *FSM) OnEnter(state State, cb Callback) *FSM
- func (f *FSM) OnExit(state State, cb Callback) *FSM
- func (f *FSM) OnTransition(hook TransitionHook) *FSM
- func (f *FSM) Reset()
- func (f *FSM) SetState(s State)
- func (f *FSM) States() g.Slice[State]
- func (f *FSM) Sync() *SyncFSM
- func (f *FSM) ToDOT() g.String
- func (f *FSM) Transition(from State, event Event, to State) *FSM
- func (f *FSM) TransitionWhen(from State, event Event, to State, guard GuardFunc) *FSM
- func (f *FSM) Trigger(event Event, input ...any) error
- func (f *FSM) UnmarshalJSON(data []byte) error
- type FSMState
- type GuardFunc
- type State
- type StateMachine
- type SyncFSM
- func (sf *SyncFSM) CallEnter(state State) error
- func (sf *SyncFSM) Context() *Context
- func (sf *SyncFSM) Current() State
- func (sf *SyncFSM) History() g.Slice[State]
- func (sf *SyncFSM) MarshalJSON() ([]byte, error)
- func (sf *SyncFSM) Reset()
- func (sf *SyncFSM) SetState(s State)
- func (sf *SyncFSM) States() g.Slice[State]
- func (sf *SyncFSM) ToDOT() g.String
- func (sf *SyncFSM) Trigger(event Event, input ...any) error
- func (sf *SyncFSM) UnmarshalJSON(data []byte) error
- type TransitionHook
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context struct {
State State
Input any
Data *g.MapSafe[g.String, any]
Meta *g.MapSafe[g.String, any]
}
Context holds FSM state, input, persistent and temporary data. Data is for long-lived values (e.g. user ID, settings) and is serialized. Meta is for ephemeral metadata (e.g. timestamps, counters) and is also serialized. Input holds data specific to the current trigger event and is NOT serialized. State holds the state for which a callback is being executed.
type ErrAmbiguousTransition ¶
ErrAmbiguousTransition is returned when a trigger event results in more than one valid transition. This typically happens due to a configuration error where multiple guards for the same event from the same state return true. The FSM's behavior is ambiguous, so the transition is aborted to prevent non-deterministic behavior.
func (*ErrAmbiguousTransition) Error ¶
func (e *ErrAmbiguousTransition) Error() string
type ErrCallback ¶
type ErrCallback struct {
// HookType is the type of callback or hook where the error occurred (e.g., "OnEnter", "OnTransition").
HookType string
// State is the state associated with the callback. It may be empty for global hooks.
State State
// Err is the original error returned by the callback or the error created after recovering from a panic.
Err error
}
ErrCallback is returned when a callback (OnEnter, OnExit) or a hook (OnTransition) returns an error or panics. It wraps the original error, allowing it to be inspected using functions like errors.Is and errors.As.
func (*ErrCallback) Error ¶
func (e *ErrCallback) Error() string
func (*ErrCallback) Unwrap ¶
func (e *ErrCallback) Unwrap() error
Unwrap provides compatibility with the standard library's errors package, allowing the use of errors.Is and errors.As to inspect the wrapped error.
type ErrInvalidTransition ¶
ErrInvalidTransition is returned when no matching transition is found for the given event from the current state.
func (*ErrInvalidTransition) Error ¶
func (e *ErrInvalidTransition) Error() string
type ErrUnknownState ¶
type ErrUnknownState struct {
State State
}
ErrUnknownState is returned when attempting to unmarshal a state that has not been defined in the FSM's configuration. This prevents the FSM from entering an invalid, undeclared state.
func (*ErrUnknownState) Error ¶
func (e *ErrUnknownState) Error() string
type FSM ¶
type FSM struct {
// contains filtered or unexported fields
}
FSM is the main state machine struct.
func (*FSM) CallEnter ¶
CallEnter manually invokes all OnEnter callbacks for a state without a transition.
func (*FSM) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (*FSM) OnTransition ¶
func (f *FSM) OnTransition(hook TransitionHook) *FSM
OnTransition registers a global transition hook.
func (*FSM) Reset ¶
func (f *FSM) Reset()
Reset resets the FSM to its initial state and clears all context data.
func (*FSM) SetState ¶
SetState manually sets the current state, without triggering any callbacks or guards. WARNING: This is a low-level method that bypasses all FSM logic (OnExit, OnEnter callbacks, transition hooks, and guards). It does not update the state history. It should only be used for specific scenarios like restoring the FSM from storage or for manual administrative intervention. For all standard operations, use Trigger.
func (*FSM) ToDOT ¶
ToDOT generates a DOT language string representation of the FSM for visualization.
func (*FSM) Transition ¶
Transition adds a basic transition (without a guard) from -> event -> to.
func (*FSM) TransitionWhen ¶
TransitionWhen adds a guarded transition from -> event -> to.
func (*FSM) Trigger ¶
Trigger attempts to transition using the given event. It accepts an optional single 'input' argument to pass data to guards and callbacks. This input is only valid for the duration of this specific trigger cycle.
func (*FSM) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.
type FSMState ¶
type FSMState struct {
Current State `json:"current"`
History g.Slice[State] `json:"history"`
Data g.Map[g.String, any] `json:"data"`
Meta g.Map[g.String, any] `json:"meta"`
}
FSMState is a serializable representation of the FSM's state. It uses standard map types for robust JSON handling.
type StateMachine ¶
type SyncFSM ¶
type SyncFSM struct {
// contains filtered or unexported fields
}
SyncFSM is a thread-safe wrapper around an FSM. It protects all state-mutating and state-reading operations with a sync.RWMutex, making it safe for use across multiple goroutines. All methods on SyncFSM are the thread-safe counterparts to the methods on the base FSM.
func (*SyncFSM) CallEnter ¶
CallEnter is the thread-safe version of FSM.CallEnter. It manually invokes the OnEnter callbacks for a given state without a transition.
func (*SyncFSM) Context ¶
Context is the thread-safe version of FSM.Context. It returns a pointer to the FSM's context.
func (*SyncFSM) Current ¶
Current is the thread-safe version of FSM.Current. It returns the FSM's current state.
func (*SyncFSM) History ¶
History is the thread-safe version of FSM.History. It returns a copy of the state transition history.
func (*SyncFSM) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface for thread-safe serialization of the FSM's state to JSON.
func (*SyncFSM) Reset ¶
func (sf *SyncFSM) Reset()
Reset is the thread-safe version of FSM.Reset. It resets the FSM to its initial state and clears its context.
func (*SyncFSM) SetState ¶
SetState is the thread-safe version of FSM.SetState. It forcefully sets the current state, bypassing all callbacks and guards. WARNING: This is a low-level method intended for specific use cases like state restoration. For all standard operations, use Trigger.
func (*SyncFSM) States ¶
States is the thread-safe version of FSM.States. It returns a slice of all unique states defined in the FSM.
func (*SyncFSM) ToDOT ¶
ToDOT is the thread-safe version of FSM.ToDOT. It generates a DOT language string representation of the FSM for visualization.
func (*SyncFSM) Trigger ¶
Trigger is the thread-safe version of FSM.Trigger. It atomically executes a state transition in response to an event.
func (*SyncFSM) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface for thread-safe deserialization of the FSM's state from JSON.