Documentation ¶
Overview ¶
Package fsm provides UserStateMachine for a Questionnaire.
Index ¶
- Constants
- func Forward[T shared.N](ctx context.Context, fsm *FiniteStateMachine, opt option.Option[T]) error
- type Callback
- type FiniteStateMachine
- func (fsm *FiniteStateMachine) AddToJournal(e event.Event)
- func (fsm *FiniteStateMachine) Backward() *FiniteStateMachine
- func (fsm *FiniteStateMachine) Done() *FiniteStateMachine
- func (fsm *FiniteStateMachine) Dump() event.Event
- func (fsm *FiniteStateMachine) Emit(prevQst, currentQst question.Question) event.Event
- func (fsm *FiniteStateMachine) GetJournal() []event.Event
- func (fsm *FiniteStateMachine) GetLogger() sypl.ISypl
- func (fsm *FiniteStateMachine) GetState() status.Status
- func (fsm *FiniteStateMachine) Jump(id string) *FiniteStateMachine
- func (fsm *FiniteStateMachine) SetCallback(cb Callback)
- func (fsm *FiniteStateMachine) Start() *FiniteStateMachine
Constants ¶
const ( DefaultMetricCounterLabel = "counter" Name = "fsm" Type = "FiniteStateMachine" )
Type is the type of the entity regarding the framework. It is used to for example, to identify the entity in the logs, metrics, and for tracing.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Callback ¶ added in v0.0.3
Callback is a function that is called every time the state of the questionnaire changes.
type FiniteStateMachine ¶
type FiniteStateMachine struct { // CurrentQuestion is the current question. CurrentQuestion question.Question `json:"currentQuestion" bson:"currentQuestion"` // CurrentQuestionID is the ID of the current question. CurrentQuestionID string `json:"currentQuestionID" bson:"currentQuestionID"` // CurrentQuestionIndex is the current question index. CurrentQuestionIndex int `json:"currentQuestionIndex" bson:"currentQuestionIndex"` // PreviousQuestionID is the ID of the previous question. PreviousQuestionID string `json:"previousQuestionID" bson:"previousQuestionID"` // TotalAnswers is the total number of answers. TotalAnswers int `json:"totalAnswers" bson:"totalAnswers"` // TotalQuestions is the total number of questions. TotalQuestions int `json:"totalQuestions" bson:"totalQuestions"` // Journal is the list of events. Journal []event.Event `json:"journal" bson:"journal"` // Logger. Logger sypl.ISypl `json:"-" bson:"-" validate:"required"` // Answers is the list of answers. Answers *safeorderedmap.SafeOrderedMap[answer.Answer] `json:"answers" bson:"answers"` // Questionnaire is the questionnaire. Questionnaire questionnaire.Questionnaire `json:"Questionnaire" bson:"Questionnaire" validate:"required"` // PreviousQuestion is the previous question. PreviousQuestion question.Question `json:"previousQuestion" bson:"previousQuestion"` // CurrentAnswer is the current answer. CurrentAnswer answer.Answer `json:"currentAnswer" bson:"currentAnswer"` // State is the current state of the questionnaire. State status.Status `json:"state" validate:"required" bson:"state"` // UserID is the ID of the user. UserID string `json:"userID" validate:"required" bson:"userID"` // contains filtered or unexported fields }
FiniteStateMachine is the Finite State Machine for the Questionnaire.
func Load ¶ added in v0.0.3
func Load(ctx context.Context, fsm *FiniteStateMachine, e event.Event) *FiniteStateMachine
Load the FSM up to state of the event.
func New ¶
func New( ctx context.Context, userID string, q questionnaire.Questionnaire, cb Callback, ) (*FiniteStateMachine, error)
New creates a new machine status.
TODO: Also supports Channel.
func (*FiniteStateMachine) AddToJournal ¶
func (fsm *FiniteStateMachine) AddToJournal(e event.Event)
AddToJournal adds an entry to the journal.
func (*FiniteStateMachine) Backward ¶
func (fsm *FiniteStateMachine) Backward() *FiniteStateMachine
Backward goes back to the previous question.
func (*FiniteStateMachine) Done ¶
func (fsm *FiniteStateMachine) Done() *FiniteStateMachine
Done the FSM setting the state to `Done`.
func (*FiniteStateMachine) Dump ¶
func (fsm *FiniteStateMachine) Dump() event.Event
Dump returns the current state of the machine.
func (*FiniteStateMachine) Emit ¶
func (fsm *FiniteStateMachine) Emit(prevQst, currentQst question.Question) event.Event
Emit emits the state of the machine, also returning it. Do whatever you want with it :) (e.g. persist it). It includes the a dump of the journal, so you can use it to restore the state of the machine.
func (*FiniteStateMachine) GetJournal ¶
func (fsm *FiniteStateMachine) GetJournal() []event.Event
GetJournal returns the journal.
func (*FiniteStateMachine) GetLogger ¶
func (fsm *FiniteStateMachine) GetLogger() sypl.ISypl
GetLogger returns the logger.
func (*FiniteStateMachine) GetState ¶
func (fsm *FiniteStateMachine) GetState() status.Status
GetState returns the current state of the questionnaire.
func (*FiniteStateMachine) Jump ¶
func (fsm *FiniteStateMachine) Jump(id string) *FiniteStateMachine
Jump to the specified question.
func (*FiniteStateMachine) SetCallback ¶ added in v0.0.3
func (fsm *FiniteStateMachine) SetCallback(cb Callback)
SetCallback sets the callback to be called when the state of the machine changes.
func (*FiniteStateMachine) Start ¶
func (fsm *FiniteStateMachine) Start() *FiniteStateMachine
Start the state machine.