• Stars
    star
    425
  • Rank 102,094 (Top 3 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Transition is a Golang state machine implementation

Transition

Transition is a Golang state machine implementation.

it can be used standalone, but it integrates nicely with GORM models. When integrated with GORM, it will also store state change logs in the database automatically.

GoDoc Build Status

Usage

Enable Transition for your struct

Embed transition.Transition into your struct, it will enable the state machine feature for the struct:

import "github.com/qor/transition"

type Order struct {
  ID uint
  transition.Transition
}

Define States and Events

var OrderStateMachine = transition.New(&Order{})

// Define initial state
OrderStateMachine.Initial("draft")

// Define a State
OrderStateMachine.State("checkout")

// Define another State and what to do when entering and exiting that state.
OrderStateMachine.State("paid").Enter(func(order interface{}, tx *gorm.DB) error {
  // To get order object use 'order.(*Order)'
  // business logic here
  return
}).Exit(func(order interface{}, tx *gorm.DB) error {
  // business logic here
  return
})

// Define more States
OrderStateMachine.State("cancelled")
OrderStateMachine.State("paid_cancelled")


// Define an Event
OrderStateMachine.Event("checkout").To("checkout").From("draft")

// Define another event and what to do before and after performing the transition.
OrderStateMachine.Event("paid").To("paid").From("checkout").Before(func(order interface{}, tx *gorm.DB) error {
  // business logic here
  return nil
}).After(func(order interface{}, tx *gorm.DB) error {
  // business logic here
  return nil
})

// Different state transitions for one event
cancellEvent := OrderStateMachine.Event("cancel")
cancellEvent.To("cancelled").From("draft", "checkout")
cancellEvent.To("paid_cancelled").From("paid").After(func(order interface{}, tx *gorm.DB) error {
  // Refund
}})

Trigger an Event

// func (*StateMachine) Trigger(name string, value Stater, tx *gorm.DB, notes ...string) error
OrderStatemachine.Trigger("paid", &order, db, "charged offline by jinzhu")
// notes will be used to generate state change logs when works with GORM

// When using without GORM, just pass nil to the db, like
OrderStatemachine.Trigger("cancel", &order, nil)

OrderStatemachine.Trigger("cancel", &order, db)
// order's state will be changed to cancelled if current state is "draft"
// order's state will be changed to paid_cancelled if current state is "paid"

Get/Set State

var order Order

// Get Current State
order.GetState()

// Set State
order.SetState("finished") // this will only update order's state, won't save it into database

State change logs

When working with GORM, Transition will store all state change logs in the database. Use GetStateChangeLogs to get those logs.

// create the table used to store logs first
db.AutoMigrate(&transition.StateChangeLog{})

// get order's state change logs
var stateChangeLogs = transition.GetStateChangeLogs(&order, db)

// type StateChangeLog struct {
//   From       string  // from state
//   To         string  // to state
//   Note       string  // notes
// }

License

Released under the MIT License.

More Repositories

1

qor

QOR is a set of libraries written in Go that abstracts common features needed for business applications, CMSs, and E-commerce systems.
Go
5,198
star
2

qor-example

An example application showcasing the QOR SDK
Go
1,241
star
3

admin

Qor Admin - Instantly create a beautiful, cross platform, configurable Admin Interface and API for managing your data in minutes.
JavaScript
898
star
4

auth

Golang Authentication solution
Go
677
star
5

roles

Roles is an authorization library for Golang
Go
140
star
6

validations

Validations is a GORM extension, used to validate models when creating, updating
Go
128
star
7

i18n

I18n is a golang implementation, provides internationalization support for your application, with different backends support
Go
105
star
8

worker

Worker run jobs in background at scheduled time
Go
61
star
9

media

Media add uploading files to cloud or other destinations with support for image cropping and resizing features to any structs
JavaScript
60
star
10

oss

QOR OSS provides common interface to operate files in cloud storage/filesystem
Go
57
star
11

amazon-pay-sdk-go

Amazon Pay Go SDK
Go
31
star
12

mailer

Mail solution
Go
23
star
13

audited

Audited is used to log last UpdatedBy and CreatedBy for your models
Go
21
star
14

auth_themes

Auth Themes
Go
20
star
15

media_library

Abandoned, use https://github.com/qor/media instead
Go
20
star
16

bindatafs

Compile QOR templates into binary with go-bindata
Go
19
star
17

l10n

L10n make your resources(models) be able to localize into different locales
Go
18
star
18

exchange

QOR exchange provides conversion (import/export) functionality for any Qor.Resource to CSV, Excel file
Go
17
star
19

gomerchant

Stripe, Paygent, and Amazon Pay Adaptors
Go
17
star
20

render

Render Templates
Go
16
star
21

seo

SEO module for QOR3
Go
14
star
22

sorting

Sorting: adds sorting and reordering abilities to your models.
JavaScript
12
star
23

publish2

Version Control with Schedule
Go
11
star
24

widget

Qor Widget - Define some customizable, shareable HTML widgets for different pages
Go
11
star
25

session

Session management
Go
8
star
26

filebox

Filebox could be used to provide access permission control for files, directories
Go
8
star
27

doc

QOR3 documentation
CSS
6
star
28

publish

Publish allow user update a resource but do not show the change in website until it is get "published" for GORM-backend models
Go
6
star
29

activity

Qor Activity: add Comment and Track data/state changes to any Qor Resource support to admin interface
Go
6
star
30

notification

QOR Notification
Go
6
star
31

slug

Slug is an extension for qor.
JavaScript
6
star
32

responder

Responder: Respond differently according to request's accepted mime type
Go
6
star
33

location

Qor Location - Make your struct support pick up location from google map in Qor Admin
JavaScript
5
star
34

assetfs

AssetFileSystem
Go
5
star
35

middlewares

Middlewares Management
Go
5
star
36

serializable_meta

Serializable meta
Go
4
star
37

action_bar

Action Bar in QOR3
Go
4
star
38

cache

Cache Store
Go
4
star
39

redirect_back

A Golang HTTP Handler that redirect back to last URL saved in session
Go
4
star
40

page_builder

Page Builder
JavaScript
3
star
41

metas

Meta Types for Admin
JavaScript
3
star
42

log

Qor Logger
Go
3
star
43

help

Help for QOR ADMIN
JavaScript
3
star
44

banner_editor

Banner Editor in QOR3
JavaScript
2
star
45

application

Application
Go
2
star
46

qor-example-cases

Learn QOR3 by examples
Go
2
star
47

wildcard_router

WildcardRouter handles dynamic routes
Go
2
star
48

variations

Variations
JavaScript
2
star
49

app

App Generator - WIP
Swift
1
star