fsm

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2022 License: Apache-2.0 Imports: 20 Imported by: 0

README

fsm

CI License GitHub release codecov Made by Alexey Burmistrov

fsm is a command line tool that generates finite state machine for Go struct.

Usage

FSM generation
Usage: fsm gen -p ./examples/transitions -s Order -f State -o order_fsm.go -t ./examples/transitions/transitions.json
   --package, -p      package where struct is located (default: default is current dir(.))
   --struct, -s       struct name
   --field, -f        state field of struct
   --output, -o       output file name (default: default srcdir/<struct>_fsm.go)
   --transitions, -t  path to file with transitions
   --noGenerate, -g   don't put //go:generate instruction to the generated code (default: false)
   --graph-output, -a value  path to transition graph file in dot format

This will generate finite state machine for struct Order with transitions defined in ./examples/transitions/transitions.json file. Transition graph will be generated from transitions file and output will be in dot format

Action generation
Usage: fsm actions gen --tpl examples/transitions/action.go.tpl -o examples/transitions/actions -t examples/transitions/transitions.json
   --template value, --tpl value  template for action
   --output_dir value, -o value   output dir
   --transitions value, -t value  path to file with transitions

This will generate actions stubs from ./examples/transitions/action.go.tpl file.

Action doc generation
Usage: fsm actions doc -o examples/transitions/actions/README.md -t examples/transitions/transitions.json
   --output value, -o value       output file name
   --transitions value, -t value  path to file with transitions

This will generate actions doc README.md from transitions file. Further runs of the command will add only new actions and update the Transitions where action appears section.

Install

Using go
go get -u github.com/MrEhbr/fsm/cmd/fsm
Download releases

https://github.com/MrEhbr/fsm/releases

License

© 2020 [Alexey Burmistrov]

Licensed under the Apache License, Version 2.0 (LICENSE). See the COPYRIGHT file for more details.

SPDX-License-Identifier: Apache-2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InStrings

func InStrings(slice []string, str string) bool

func ToCamelCase

func ToCamelCase(s string, upper bool) string

ToCamelCase transform string to upper or lower camel case

func ToSnackCase

func ToSnackCase(s string, upper bool) string

Types

type Colors

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

func NewColors

func NewColors() *Colors

func (*Colors) Pick

func (c *Colors) Pick() string

type Generator

type Generator struct {
	Options
	// contains filtered or unexported fields
}

Generator generates finite state machine

func NewGenerator

func NewGenerator(opt Options) (*Generator, error)

NewGenerator returns a pointer to Generator

func (*Generator) Generate

func (g *Generator) Generate(w io.Writer) error

Generate generates code using template

func (*Generator) GenerateTransitionGraph

func (g *Generator) GenerateTransitionGraph(w io.Writer) error

type Options

type Options struct {
	// InputPackage is an import path or a relative path of the package that contains the source struct
	InputPackage string
	// OutputFile name for output file
	OutputFile string
	// Struct is name of struct
	Struct string
	// StateField is name of struct field that indicate state
	StateField string
	// TransitionsFile is path to file which hold supported transitions
	TransitionsFile string
	// DisableGoGenerate don't put go generate
	DisableGoGenerate bool
	// ActionGraphOutputFile path to action graph file
	ActionGraphOutputFile string
}

Options for NewGenerator constructor

type StateValue

type StateValue struct {
	Name string
	Val  string
}

StateValue represents state field values

type Struct

type Struct struct {
	Name        string
	StateField  string
	StateType   string
	StateValues []StateValue
	Transitions Transitions
}

Struct represent struct data

func (Struct) FindValue

func (s Struct) FindValue(str string) string

FindValue find state value by string representation of value or variable name if not found return empty string

func (Struct) Validate

func (s Struct) Validate() error

Validate validate Struct for using in generation

type Transition

type Transition struct {
	From          stringArray `json:"from" yaml:"from"`
	To            string      `json:"to" yaml:"to"`
	Event         string      `json:"event" yaml:"event"`
	BeforeActions []string    `json:"before_actions" yaml:"before_actions"`
	Actions       []string    `json:"actions" yaml:"actions"`
}

Transition represent fsm state transition

func (Transition) String

func (t Transition) String() string

type Transitions

type Transitions []Transition

Transitions slice of Transition

func (Transitions) ActionTransitions

func (trs Transitions) ActionTransitions(action string) []Transition

func (Transitions) Actions

func (trs Transitions) Actions() []string

Actions return unique slice of actions

func (Transitions) Events

func (trs Transitions) Events() []string

func (Transitions) Graph

func (trs Transitions) Graph() *graphman.Graph

Directories

Path Synopsis
cmd
fsm
examples

Jump to

Keyboard shortcuts

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