Documentation ¶
Index ¶
- Variables
- func NewStateMachine() *stateMachine
- type Condition
- type PostTransition
- type State
- type StateDoc
- type StateDocJSON
- type StateJSON
- type StateMachine
- type StateMachineDocumentation
- type StateMachineJSON
- type StateSwitch
- type States
- type Transition
- type TransitionArgs
- type TransitionRule
- type TransitionRuleDoc
- type TransitionRuleJSON
- type TransitionRules
- type TransitionType
- type TransitionTypeDoc
- type TransitionTypeJSON
Constants ¶
This section is empty.
Variables ¶
var ( NoConditionPassedToRunTransaction = errors.New("no condition found to run transition") NoMatchForTransitionType = errors.New("no match for transition type") )
Functions ¶
Types ¶
type Condition ¶
type Condition func(stateSwitch StateSwitch, args TransitionArgs) (bool, error)
Condition for the transition, transition will be executed only if this function return true Can be nil, in this case it's considered as return true, nil Not mandatory
type PostTransition ¶
type PostTransition func(stateSwitch StateSwitch, args TransitionArgs) error
PostTransition will be called if condition and transition are successful. Not mandatory
type StateDocJSON ¶
type StateMachine ¶
type StateMachine interface { // AddTransitionRule adds a new transition rule to the state machine AddTransitionRule(rule TransitionRule) // AddTransition is a deprecated method, use AddTransitionRule instead AddTransition(rule TransitionRule) // Run transition by type Run(transitionType TransitionType, stateSwitch StateSwitch, args TransitionArgs) error StateMachineDocumentation }
type StateMachineDocumentation ¶
type StateMachineDocumentation interface { // DescribeState lets you optionally add documentation for a particular // [State]. This description will be included in the JSON generated by // AsJSON DescribeState(state State, stateDocumentation StateDoc) // DescribeTransitionType lets you optionally add documentation for a // particular [TransitionType]. This description will be included in the // JSON generated by AsJSON DescribeTransitionType(transitionType TransitionType, transitionTypeDocumentation TransitionTypeDoc) // Generates a machine-readable JSON representation of the state machine // states and transitions. See StateMachineJSON for the format. Such JSON // can be used to generate documentation or to generate a state machine // diagram AsJSON() ([]byte, error) }
type StateMachineJSON ¶
type StateMachineJSON struct { TransitionRules []TransitionRuleJSON `json:"transition_rules"` States map[string]StateJSON `json:"states"` TransitionTypes map[string]TransitionTypeJSON `json:"transition_types"` }
type StateSwitch ¶
type StateSwitch interface { // State return current state State() State // SetState set a new state SetState(state State) error }
StateSwitch interface used by state machine
type Transition ¶
type Transition func(stateSwitch StateSwitch, args TransitionArgs) error
Transition is users business logic, should not set the state or return next state If condition return true this function will be executed Not mandatory
type TransitionArgs ¶
type TransitionArgs interface{}
type TransitionRule ¶
type TransitionRule struct { TransitionType TransitionType SourceStates States DestinationState State Condition Condition Transition Transition PostTransition PostTransition // Documentation for the transition rule, can be left empty Documentation TransitionRuleDoc }
TransitionRule is a rule that defines the required source states and conditions needed to move to a particular destination state when a particular transition type happens
func (TransitionRule) IsAllowedToRun ¶
func (tr TransitionRule) IsAllowedToRun(stateSwitch StateSwitch, args TransitionArgs) (bool, error)
IsAllowedToRun validate if current state supported, after then check the condition, if it pass then transition is a allowed. Nil condition is automatic approval.
type TransitionRuleDoc ¶
type TransitionRuleJSON ¶
type TransitionRuleJSON struct { TransitionType TransitionType `json:"transition_type"` SourceStates []string `json:"source_states"` DestinationState string `json:"destination_state"` Name string `json:"name"` Description string `json:"description"` }
type TransitionRules ¶
type TransitionRules []TransitionRule
func (TransitionRules) Find ¶
func (tr TransitionRules) Find(transitionType TransitionType) TransitionRules
Find search for all matching transitions by transition type
type TransitionType ¶
type TransitionType string
TransitionType reprents an event that can cause a state transition