micromachine

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2019 License: MIT Imports: 1 Imported by: 0

README

🚗 Micromachine

Build Status Go Report Card GoDoc License MIT

A little stateless state machine.

Setup

Lint

make lint

Test

make test

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrSubjectCannotBeNil occurs when subject is nil.
	ErrSubjectCannotBeNil = errors.New(`subject cannot be nil`)

	// ErrInvalidGroup occurs when a group does not exist.
	ErrInvalidGroup = errors.New(`invalid group`)

	// ErrInvalidFromState occurs when a from state does not exist.
	ErrInvalidFromState = errors.New(`"from" state is invalid`)

	// ErrInvalidToState occurs when a to state does not exist.
	ErrInvalidToState = errors.New(`"to" state is invalid`)
)

Functions

This section is empty.

Types

type Condition

type Condition func() error

Condition must be checked before transitioning. Can also be used as a callback.

type Group

type Group int

Group is an int that represents a group.

const (
	// GroupPublic allows anyone to make the transitions in this group.
	GroupPublic Group = 0

	// GroupSuperAdmin bypasses all map checks but still runs conditions.
	GroupSuperAdmin Group = 99
)

type Paths

type Paths map[Group]map[State]map[State][]Condition

Paths maps the possible routes in the machine.

type State

type State int

State is an int that represents a state.

type StateMachine

type StateMachine struct {
	Paths   Paths
	Subject Stateful
}

StateMachine wraps the paths and has a reference to that which we are managing state for.

func (*StateMachine) Transition

func (sm *StateMachine) Transition(in Group, to State) error

Transition validates state transitions.

type Stateful

type Stateful interface {
	GetState() State
	SetState(Group, State) error
}

Stateful gives us a way to interact with our subject.

Directories

Path Synopsis
test

Jump to

Keyboard shortcuts

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