An ease to use finit state machine golang implementation.Turn any struct to a fsm with graphviz visualization supported.

go-fsm

An ease to use finit state machine golang implementation.Turn any struct to a fsm with graphviz visualization supported.

usage

import github.com/fingerliu/go-fsm/fsm

const (
    OrderStatusCreated = "created"
    OrderStatusCancelled = "cancelled"
    OrderStatusPaid = "paid"
    OrderStatusCheckout = "checkout"
    OrderStatusDelivering = "delivering"
    OrderStatusDelivered = "delivered"
    OrderStatusFinished = "finished"
    
    // you can not cancel a virtual order once it is paid.
    
    // normal flow for a physical order maybe: created -> paid -> checkout -> delivering -> delivered -> finished
    OrderTypePhysical = "physical"
    
    // normal flow for a virtual order maybe: created -> paid -> finished
    OrderTypeVirtual = "virtual"
)

struct OrderService {
    Name string
    Type string
    Status string
    fsm fsm.FSM
}

func NewOrder() *OrderService {


    orderService := OrderService{}
    orderFsm := fsm.NewFSM().
    
        // add state to fsm
        AddStates(OrderStatusCreated,OrderStatusCancelled,OrderStatusPaid,OrderStatusCheckout,OrderStatusDelivering,OrderStatusDelivered,OrderStatusFinished).
    
        //add trasition from S to E with condition check C
        AddTransition(OrderStatusCreated, OrderStatusCancelled).
        
        // add trasition on a condition
        AddTransitionOn(OrderStatusPaid, OrderStatusCancelled, orderService.IsPhysical)
        
        // add hook for a specific state(enter/exit)
        AddStateEnterHook(OrderStatusCancelled, orderService.StopDeliver)
        
        // global hook is triggerred when state change(enter/exit) success.
        // here we use hook to save sate to order status field in database.
        AddGloalEnterHook(orderService.saveStatus)

    orderService.fsm = orderFsm
    
    return &orderService
}
Similar Resources

An easy-to-use Map Reduce Go parallel-computing framework inspired by 2021 6.824 lab1. It supports multiple workers on a single machine right now.

MapReduce This is an easy-to-use Map Reduce Go framework inspired by 2021 6.824 lab1. Feature Multiple workers on single machine right now. Easy to pa

Dec 5, 2022

Set of functions/methods that will ease GO code generation

Set of functions/methods that will ease GO code generation

Dec 1, 2021

Module to ease interaction with Pact's development server & ScalableBFT

go-pact Module to ease interaction with Pact's development server & ScalableBFT Install go install github.com/jfamousket/go-pact@latest Functions H

Dec 9, 2021

You could leverage Alfred and Google Sheets to track your time with ease.

You could leverage Alfred and Google Sheets to track your time with ease.

You could leverage Alfred and Google Sheets to track your time with ease. The goal is to track your time in a way that is easy to understand how much time you spend on.

Dec 25, 2022

Go library for creating state machines

Go library for creating state machines

Stateless Create state machines and lightweight state machine-based workflows directly in Go code: phoneCall := stateless.NewStateMachine(stateOffHook

Jan 6, 2023

Converts NFAs (and DFAs) to a regular expressions using the state removal method

nfa2regex: convert NFAs (and DFAs) to regular expressions An implementation of the state removal technique for converting an NFA to a regular expressi

Apr 29, 2022

Get notifications about unexpected system state from your local Gesundheitsdienst.

Get notifications about unexpected system state from your local Gesundheitsdienst.

Nov 1, 2022

Implementation of the Feynman algorithm to solve any problem!

Feynman Algorithm Allegedly coined in jest by Murray Gell-Mann to describe Richard Feynman's incredible problem solving ability, this simple algorithm

Mar 16, 2022

Align Golang struct tags

Formattag The tool is used to align golang struct's tags. eg.: Before // TestStruct this is a test struct type TestStruct struct { ID stri

Aug 1, 2022
Related tags
State observer - StateObserver used to synchronize the local(cached) state of the remote object with the real state

state observer StateObserver used to synchronize the local(cached) state of the

Jan 19, 2022
Plinko - a Fluent State Machine for Go
 Plinko - a Fluent State Machine for Go

Plinko - a Fluent State Machine for Go Build Status Create state machines and lightweight state machine-based workflows directly in golang code The pr

Jan 3, 2023
Finite State Machine for Go
Finite State Machine for Go

FSM for Go Finite State Machine for Go It is heavily inspired from looplab/fsm library but with more abstractions and optimizations License FSM is lic

Nov 30, 2021
Finite-state machine with processors

FSM Finite-state machine with processors. Builder Register state processors type state1Processor struct { // clients } func NewState1Processor(..

Jan 7, 2022
Visualize call graph of a Go program using Graphviz
Visualize call graph of a Go program using Graphviz

go-callvis go-callvis is a development tool to help visualize call graph of a Go program using interactive view. Introduction The purpose of this tool

Dec 31, 2022
Flesch-go - Go-based implementation of the Flesch reading ease readability formula module.

flesch-go Go-based implementation of the Flesch reading ease readability formula module. Thanks for the flesch-index project. Installation Run the fol

Nov 9, 2022
Build for all Go-supported platforms by default, disable those which you don't want.

bagop Build for all Go-supported platforms by default, disable those which you don't want. Overview bagop is a simple build tool for Go which tries to

Jul 29, 2022
A ocilloscope writen in GO. Supported serial input, portaudio input.

A ocilloscope writen in GO. Supported serial input, portaudio input.

Oct 23, 2021
A reimplementation of the TinyGo drivers package for communicating with multiples of the same (supported) devices on one individual I2C bus.

tinygo-multi-i2c A reimplementation of the TinyGo drivers package for communicating with multiples of the same (supported) devices on one individual I

Mar 10, 2022
FlameScope is a visualization tool for exploring different time ranges as Flame Graphs.
FlameScope is a visualization tool for exploring different time ranges as Flame Graphs.

FlameScope FlameScope is a visualization tool for exploring different time ranges as Flame Graphs, allowing quick analysis of performance issues such

Dec 27, 2022