transition

package
v0.0.0-...-045f52c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2015 License: MIT Imports: 4 Imported by: 0

README

Introduction

transition is a Golang state machine implementation. rely on github.com/jinzhu/gorm

GO Compatibility ?

Installation

go get github.com/theplant/qor3/transition

Usage

1. Add transition to object struct
type Order struct {
    ID        uint
    ...

    // Add transition to Order
    // type Transition struct {
    //   State           string
    //   StateChangeLogs []StateChangeLog `sql:"-"`
    // }
    transition.Transition
}
2. Define states and events
var OrderStateMachine transition.New(&Order{})

func init() {
	// Define initial state
	OrderStateMachine.Initial("draft")

    // Define what to do when enter a state and exit a state. See Callbacks for detail.
	OrderStateMachine.State("paying").Enter(func(order interface{}, tx *gorm.DB) (err error) {
        // To get order object use 'order.(*Order)'
		// business logic here
		return
	}).Exit(func(order interface{}, tx *gorm.DB) (err error) {
        // business logic here
        return
    })

    // Define event and what to do before perform transition and after transition. See Callbacks for detail.
    OrderStateMachine.Event("checkout").To("paying").From("draft").Before(func(order interface{}, tx *gorm.DB) (err error) {
        // business logic here
        return
    }).After(func(order interface{}, tx *gorm.DB) (err error) {
        // business logic here
        return
    })

    // Different state transition for one event
    cancellEvent := OrderStateMachine.Event("cancel")
    cancellEvent.To("canceled").From("draft", "paying")  // NOTE: Must define `To` before `From`
    cancellEvent.To("canceled_after_paid").From("paid", "processed")
}
3. Transfer state
// func (sm *StateMachine) Trigger(name string, value Stater, tx *gorm.DB) error {
OrderStatemachine.Trigger("checkout", *order, db)

Callbacks

There are 2 callbacks for state.

Enter

This will be performed when entering a state. Object is not persisted and object state is not changed.

OrderStateMachine.State("paying").Enter(func(order interface{}, tx *gorm.DB) (err error) {
    // To get order object use 'order.(*Order)'
    // business logic here
    return
})
Exit

This will be performed when exiting a state. Object is not persisted and object state is not changed.

OrderStateMachine.State("paying").Exit(func(order interface{}, tx *gorm.DB) (err error) {
    // business logic here
    return
})

And 2 callbacks for event.

Before

This will be performed before the event performed. Object is not persisted and object state is not changed.

OrderStateMachine.Event("checkout").From("draft").To("paying").Before(func(order interface{}, tx *gorm.DB) (err error) {
    // business logic here
    return
})
After

This will be performed after the event performed. Object is persisted.

OrderStateMachine.Event("checkout").From("draft").To("paying").Before(func(order interface{}, tx *gorm.DB) (err error) {
    // business logic here
    return
})

Features

State change log

not implemented

Copyright © The Plant

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event struct {
	Name string
	// contains filtered or unexported fields
}

func (*Event) To

func (event *Event) To(name string) *EventTransition

type EventTransition

type EventTransition struct {
	// contains filtered or unexported fields
}

func (*EventTransition) After

func (transition *EventTransition) After(fc func(value interface{}, tx *gorm.DB) error) *EventTransition

func (*EventTransition) Before

func (transition *EventTransition) Before(fc func(value interface{}, tx *gorm.DB) error) *EventTransition

func (*EventTransition) From

func (transition *EventTransition) From(states ...string) *EventTransition

type State

type State struct {
	Name string
	// contains filtered or unexported fields
}

func (*State) Enter

func (state *State) Enter(fc func(value interface{}, tx *gorm.DB) error) *State

func (*State) Exit

func (state *State) Exit(fc func(value interface{}, tx *gorm.DB) error) *State

type StateChangeLog

type StateChangeLog struct {
	Id         uint64
	ReferTable string
	ReferId    string
	From       string
	To         string
	Note       string
	CreatedAt  time.Time
	UpdatedAt  time.Time
	DeletedAt  time.Time
}

type StateMachine

type StateMachine struct {
	// contains filtered or unexported fields
}

func New

func New(value interface{}) *StateMachine

func (*StateMachine) Event

func (sm *StateMachine) Event(name string) *Event

func (*StateMachine) Initial

func (sm *StateMachine) Initial(name string) *StateMachine

func (*StateMachine) State

func (sm *StateMachine) State(name string) *State

func (*StateMachine) Trigger

func (sm *StateMachine) Trigger(name string, value Stater, tx *gorm.DB) error

type Stater

type Stater interface {
	SetState(name string)
	GetState() string
}

type Transition

type Transition struct {
	State           string
	StateChangeLogs []StateChangeLog `sql:"-"`
}

func (Transition) GetState

func (transition Transition) GetState() string

func (*Transition) SetState

func (transition *Transition) SetState(name string)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL