fsm

package module
v0.0.0-...-be34c72 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

README

FSM - Finite State Machine Generator for Go

A type-safe finite state machine generator for Go that creates code from YAML definitions.

Installation

go install github.com/egoodhall/fsm/cmd/fsmgen@latest

Features

  • Type-safe state machine definitions
  • Parallel processing with configurable worker counts
  • In-memory or persistent state storage
  • Automatic code generation from YAML definitions

Usage

  1. Define your state machine in YAML:
# create_workspace.yaml
name: CreateWorkspace
states:
- 
  name: CreateRecord
  entrypoint: true
  inputs:
  - WorkspaceContext
  transitions:
  - CloneRepo
  - Error
- 
  name: CloneRepo
  workers: 5
  inputs:
  - WorkspaceContext
  - WorkspaceID
  transitions:
  - Done
  - Error
- 
  name: Done
  terminal: true
  inputs:
  - WorkspaceContext
  - WorkspaceID
- 
  name: Error
  terminal: true
  inputs:
  - WorkspaceContext
  1. Define your custom types:
// example.go
package example

//go:generate go run github.com/egoodhall/fsm/cmd/fsmgen -out . -pkg example create_workspace.yaml

type WorkspaceID int64

type WorkspaceContext struct {
    RepositoryURL string
    BranchName    string
}
  1. Generate FSM code:
fsmgen -out ./generated -pkg example create_workspace.yaml
# or use go:generate
go generate ./...
  1. Use the generated FSM:
fsm, err := example.NewCreateWorkspaceFSMBuilder().
    CreateRecordState(func(ctx context.Context, transitions example.CreateRecordTransitions, c example.WorkspaceContext) error {
        return transitions.ToCloneRepo(ctx, c, example.WorkspaceID(1))
    }).
    CloneRepoState(func(ctx context.Context, transitions example.CloneRepoTransitions, c example.WorkspaceContext, i example.WorkspaceID) error {
        return transitions.ToDone(ctx, c, i)
    }).
    DoneState(func(ctx context.Context, c example.WorkspaceContext, i example.WorkspaceID) error {
        // Terminal state handler
        return nil
    }).
    ErrorState(func(ctx context.Context, c example.WorkspaceContext) error {
        // Error state handler
        return nil
    }).
    BuildAndStart(context.Background())

// Submit a task to the FSM
id, err := fsm.Submit(context.Background(), example.WorkspaceContext{
    RepositoryURL: "https://github.com/example/repo",
    BranchName: "main",
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(pkg string, model *FsmModel) *jen.File

func GetAttempt

func GetAttempt(ctx context.Context) int

func InMemory

func InMemory() func() (Store, error)

func Logger

func Logger(ctx context.Context) *slog.Logger

func OnDisk

func OnDisk(path string) func() (Store, error)

func PutAttempt

func PutAttempt(ctx context.Context, attempt int) context.Context

func PutLogger

func PutLogger(ctx context.Context, logger *slog.Logger) context.Context

func PutState

func PutState(ctx context.Context, id State) context.Context

func PutTaskID

func PutTaskID(ctx context.Context, id TaskID) context.Context

Types

type Backoff

type Backoff func(attempt int) time.Duration

func ExponentialBackoff

func ExponentialBackoff(base, max time.Duration) Backoff

func LinearBackoff

func LinearBackoff(increment, max time.Duration) Backoff

type CompletionListener

type CompletionListener func(ctx context.Context, id TaskID, state State)

type FsmModel

type FsmModel struct {
	Name   string               `yaml:"name"`
	Types  map[string]TypeModel `yaml:"types"`
	States []StateModel         `yaml:"states"`
}

func ParseModel

func ParseModel(p *yaml.Decoder) (*FsmModel, error)

func (*FsmModel) FsmBuilderConstructorName

func (s *FsmModel) FsmBuilderConstructorName() string

func (*FsmModel) FsmBuilderFinalStageName

func (s *FsmModel) FsmBuilderFinalStageName() string

func (*FsmModel) FsmBuilderName

func (s *FsmModel) FsmBuilderName() string

func (*FsmModel) FsmBuilderStageMethodName

func (s *FsmModel) FsmBuilderStageMethodName(state StateModel) string

func (*FsmModel) FsmBuilderStageName

func (s *FsmModel) FsmBuilderStageName(state StateModel) string

func (*FsmModel) FsmInternalName

func (s *FsmModel) FsmInternalName() string

func (*FsmModel) FsmName

func (s *FsmModel) FsmName() string

func (*FsmModel) FsmStateInternalName

func (s *FsmModel) FsmStateInternalName(state StateModel) string

func (*FsmModel) FsmStateMessageName

func (s *FsmModel) FsmStateMessageName(state StateModel) string

func (*FsmModel) FsmStateProcessorName

func (s *FsmModel) FsmStateProcessorName(state StateModel) string

func (*FsmModel) FsmStateQueueInternalName

func (s *FsmModel) FsmStateQueueInternalName(state StateModel) string

func (*FsmModel) GetState

func (s *FsmModel) GetState(name State) StateModel

func (*FsmModel) InitialState

func (s *FsmModel) InitialState() StateModel

func (*FsmModel) RenderType

func (s *FsmModel) RenderType(name string) jen.Code

func (*FsmModel) StateName

func (s *FsmModel) StateName(state StateModel) string

func (*FsmModel) StateTypeName

func (s *FsmModel) StateTypeName() string

func (*FsmModel) TransitionToName

func (s *FsmModel) TransitionToName(to State) string

func (*FsmModel) TransitionsParamTypeName

func (s *FsmModel) TransitionsParamTypeName(state StateModel) string

type GeneratorOptions

type GeneratorOptions struct {
	Out string
	Pkg string
}

func ParseFlags

func ParseFlags() (input string, opts GeneratorOptions, err error)

type Option

type Option func(SupportsOptions) error

func WithBackoff

func WithBackoff(backoff Backoff) Option

func WithCompletionListener

func WithCompletionListener(listener CompletionListener) Option

func WithLogger

func WithLogger(logger *slog.Logger) Option

func WithStore

func WithStore(provider func() (Store, error)) Option

func WithTransitionListener

func WithTransitionListener(listener TransitionListener) Option

type Q

type Q interface {
	sqlc.Querier
}

type State

type State string
const StateError State = "__error__"

StateError is used to indicate an error during a state transition.

func GetState

func GetState(ctx context.Context) State

type StateModel

type StateModel struct {
	Name        State    `yaml:"name"`
	Entrypoint  bool     `yaml:"entrypoint"`
	Terminal    bool     `yaml:"terminal"`
	Workers     int      `yaml:"workers"`
	Queue       int      `yaml:"queue"`
	Inputs      []string `yaml:"inputs"`
	Transitions []State  `yaml:"transitions"`
}

type Store

type Store interface {
	DB() *sql.DB
	Q() Q
}

type SupportsOptions

type SupportsOptions interface {
	WithContext(update func(ctx context.Context) context.Context)
	WithStore(store Store)
	WithBackoff(backoff Backoff)
	WithTransitionListener(listener TransitionListener)
	WithCompletionListener(listener CompletionListener)
}

type TaskID

type TaskID int64

func GetTaskID

func GetTaskID(ctx context.Context) TaskID

type TransitionListener

type TransitionListener func(ctx context.Context, id TaskID, from State, to State)

type TypeModel

type TypeModel struct {
	Type    string `yaml:"type"`
	Package string `yaml:"package,omitempty"`
}

Directories

Path Synopsis
cmd
fsmgen command
Generated by fsmgen.
Generated by fsmgen.
gen

Jump to

Keyboard shortcuts

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