machine

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2020 License: Apache-2.0 Imports: 13 Imported by: 5

README

Step Machine

machine is an implementation of the AWS State Machine specification. The primary goal of this implementation is to enable testing of state machines and code together.

Continuing Development

Step at the moment is still very beta, and its API will likely change more before it stabilizes. If you have ideas for improvements please reach out.

Some of the TODOs left for the library are:

  1. Support for Parallel States
  2. Better Validations e.g. making sure all states are reachable and executable
  3. Client side visualization of state machine and execution using GraphViz

Documentation

Overview

State Machine implementation

State Machine Parser

Index

Constants

This section is empty.

Variables

View Source
var EmptyStateMachine = `{
  "StartAt": "WIN",
  "States": { "WIN": {"Type": "Succeed"}}
}`

EmptyStateMachine is a small Valid StateMachine

Functions

func DefaultHandler

func DefaultHandler(_ context.Context, input interface{}) (interface{}, error)

func Validate

func Validate(sm_json *string) error

Global Methods

func ValidateNameAndType added in v1.0.3

func ValidateNameAndType(s State) error

Types

type Catcher added in v1.0.3

type Catcher struct {
	ErrorEquals []*string      `json:",omitempty"`
	ResultPath  *jsonpath.Path `json:",omitempty"`
	Next        *string        `json:",omitempty"`
}

type Choice added in v1.0.3

type Choice struct {
	ChoiceRule

	Next *string `json:",omitempty"`
}

type ChoiceRule added in v1.0.3

type ChoiceRule struct {
	Variable *jsonpath.Path `json:",omitempty"`

	StringEquals            *string `json:",omitempty"`
	StringLessThan          *string `json:",omitempty"`
	StringGreaterThan       *string `json:",omitempty"`
	StringLessThanEquals    *string `json:",omitempty"`
	StringGreaterThanEquals *string `json:",omitempty"`

	NumericEquals            *float64 `json:",omitempty"`
	NumericLessThan          *float64 `json:",omitempty"`
	NumericGreaterThan       *float64 `json:",omitempty"`
	NumericLessThanEquals    *float64 `json:",omitempty"`
	NumericGreaterThanEquals *float64 `json:",omitempty"`

	BooleanEquals *bool `json:",omitempty"`

	TimestampEquals            *time.Time `json:",omitempty"`
	TimestampLessThan          *time.Time `json:",omitempty"`
	TimestampGreaterThan       *time.Time `json:",omitempty"`
	TimestampLessThanEquals    *time.Time `json:",omitempty"`
	TimestampGreaterThanEquals *time.Time `json:",omitempty"`

	And []*ChoiceRule `json:",omitempty"`
	Or  []*ChoiceRule `json:",omitempty"`
	Not *ChoiceRule   `json:",omitempty"`
}

func (*ChoiceRule) String added in v1.0.3

func (cr *ChoiceRule) String() string

type ChoiceState added in v1.0.3

type ChoiceState struct {
	Type    *string
	Comment *string `json:",omitempty"`

	InputPath  *jsonpath.Path `json:",omitempty"`
	OutputPath *jsonpath.Path `json:",omitempty"`

	Default *string `json:",omitempty"` // Default State if no choices match

	Choices []*Choice `json:",omitempty"`
	// contains filtered or unexported fields
}

func (*ChoiceState) Execute added in v1.0.3

func (s *ChoiceState) Execute(ctx context.Context, input interface{}) (output interface{}, next *string, err error)

func (*ChoiceState) GetType added in v1.0.3

func (s *ChoiceState) GetType() *string

func (*ChoiceState) Name added in v1.0.3

func (s *ChoiceState) Name() *string

func (*ChoiceState) SetName added in v1.0.3

func (s *ChoiceState) SetName(name *string)

func (*ChoiceState) SetType added in v1.0.3

func (s *ChoiceState) SetType(t *string)

func (*ChoiceState) Validate added in v1.0.3

func (s *ChoiceState) Validate() error

type Execution

type Execution struct {
	Output     map[string]interface{}
	OutputJSON string
	Error      error

	LastOutput     map[string]interface{} // interim output
	LastOutputJSON string
	LastError      error // interim error

	ExecutionHistory []HistoryEvent
}

func (*Execution) EnteredEvent

func (sm *Execution) EnteredEvent(s State, input interface{})

func (*Execution) ExitedEvent

func (sm *Execution) ExitedEvent(s State, output interface{})

func (*Execution) Failed

func (sm *Execution) Failed()

func (*Execution) Path

func (sm *Execution) Path() []string

Path returns the Path of States, ignoreing TaskFn states

func (*Execution) SetLastOutput

func (sm *Execution) SetLastOutput(output interface{}, err error)

func (*Execution) SetOutput

func (sm *Execution) SetOutput(output interface{}, err error)

func (*Execution) Start

func (sm *Execution) Start()

func (*Execution) Succeeded

func (sm *Execution) Succeeded()

type ExecutionFn added in v1.0.3

type ExecutionFn func(context.Context, interface{}) (interface{}, *string, error)

type FailState added in v1.0.3

type FailState struct {
	Type    *string
	Comment *string `json:",omitempty"`

	Error *string `json:",omitempty"`
	Cause *string `json:",omitempty"`
	// contains filtered or unexported fields
}

func (*FailState) Execute added in v1.0.3

func (s *FailState) Execute(_ context.Context, input interface{}) (output interface{}, next *string, err error)

func (*FailState) GetType added in v1.0.3

func (s *FailState) GetType() *string

func (*FailState) Name added in v1.0.3

func (s *FailState) Name() *string

func (*FailState) SetName added in v1.0.3

func (s *FailState) SetName(name *string)

func (*FailState) SetType added in v1.0.3

func (s *FailState) SetType(t *string)

func (*FailState) Validate added in v1.0.3

func (s *FailState) Validate() error

type HistoryEvent

type HistoryEvent struct {
	sfn.HistoryEvent
}

type MapState added in v1.0.3

type MapState struct {
	Type    *string
	Comment *string `json:",omitempty"`

	Iterator   *StateMachine
	ItemsPath  *jsonpath.Path `json:",omitempty"`
	Parameters interface{}    `json:",omitempty"`

	MaxConcurrency *float64 `json:",omitempty"`

	InputPath  *jsonpath.Path `json:",omitempty"`
	OutputPath *jsonpath.Path `json:",omitempty"`
	ResultPath *jsonpath.Path `json:",omitempty"`

	Catch []*Catcher `json:",omitempty"`
	Retry []*Retrier `json:",omitempty"`

	Next *string `json:",omitempty"`
	End  *bool   `json:",omitempty"`
	// contains filtered or unexported fields
}

func (*MapState) Execute added in v1.0.3

func (s *MapState) Execute(ctx context.Context, input interface{}) (output interface{}, next *string, err error)

func (*MapState) GetType added in v1.0.3

func (s *MapState) GetType() *string

func (*MapState) Name added in v1.0.3

func (s *MapState) Name() *string

func (*MapState) SetName added in v1.0.3

func (s *MapState) SetName(name *string)

func (*MapState) SetType added in v1.0.3

func (s *MapState) SetType(t *string)

func (*MapState) Validate added in v1.0.3

func (s *MapState) Validate() error

type ParallelState added in v1.0.3

type ParallelState struct {
	Type    *string
	Comment *string `json:",omitempty"`
	// contains filtered or unexported fields
}

func (*ParallelState) Execute added in v1.0.3

func (s *ParallelState) Execute(_ context.Context, input interface{}) (output interface{}, next *string, err error)

func (*ParallelState) GetType added in v1.0.3

func (s *ParallelState) GetType() *string

func (*ParallelState) Name added in v1.0.3

func (s *ParallelState) Name() *string

func (*ParallelState) SetName added in v1.0.3

func (s *ParallelState) SetName(name *string)

func (*ParallelState) SetType added in v1.0.3

func (s *ParallelState) SetType(t *string)

func (*ParallelState) Validate added in v1.0.3

func (s *ParallelState) Validate() error

type PassState added in v1.0.3

type PassState struct {
	Type    *string
	Comment *string `json:",omitempty"`

	InputPath  *jsonpath.Path `json:",omitempty"`
	OutputPath *jsonpath.Path `json:",omitempty"`
	ResultPath *jsonpath.Path `json:",omitempty"`

	Result interface{} `json:",omitempty"`

	Next *string `json:",omitempty"`
	End  *bool   `json:",omitempty"`
	// contains filtered or unexported fields
}

func (*PassState) Execute added in v1.0.3

func (s *PassState) Execute(ctx context.Context, input interface{}) (output interface{}, next *string, err error)

func (*PassState) GetType added in v1.0.3

func (s *PassState) GetType() *string

func (*PassState) Name added in v1.0.3

func (s *PassState) Name() *string

func (*PassState) SetName added in v1.0.3

func (s *PassState) SetName(name *string)

func (*PassState) SetType added in v1.0.3

func (s *PassState) SetType(t *string)

func (*PassState) Validate added in v1.0.3

func (s *PassState) Validate() error

type Retrier added in v1.0.3

type Retrier struct {
	ErrorEquals     []*string `json:",omitempty"`
	IntervalSeconds *int      `json:",omitempty"`
	MaxAttempts     *int      `json:",omitempty"`
	BackoffRate     *float64  `json:",omitempty"`
	// contains filtered or unexported fields
}

type State added in v1.0.3

type State interface {
	Execute(context.Context, interface{}) (interface{}, *string, error)
	Validate() error

	SetName(*string)
	SetType(*string)

	Name() *string
	GetType() *string
}

type StateMachine

type StateMachine struct {
	Comment *string `json:",omitempty"`

	StartAt *string

	States States
}

StateMachine the core struct for the machine

func FromJSON

func FromJSON(raw []byte) (*StateMachine, error)

func ParseFile

func ParseFile(file string) (*StateMachine, error)

Takes a file, and a map of Task Function s

func (*StateMachine) DefaultLambdaContext

func (sm *StateMachine) DefaultLambdaContext(lambda_name string) context.Context

func (*StateMachine) Execute

func (sm *StateMachine) Execute(input interface{}) (*Execution, error)

func (*StateMachine) FindTask

func (sm *StateMachine) FindTask(name string) (*TaskState, error)

func (*StateMachine) SetDefaultHandler

func (sm *StateMachine) SetDefaultHandler()

func (*StateMachine) SetResource

func (sm *StateMachine) SetResource(lambda_arn *string)

func (*StateMachine) SetTaskFnHandlers

func (sm *StateMachine) SetTaskFnHandlers(tfs *handler.TaskHandlers) error

func (*StateMachine) SetTaskHandler

func (sm *StateMachine) SetTaskHandler(task_name string, resource_fn interface{}) error

func (*StateMachine) Tasks

func (sm *StateMachine) Tasks() map[string]*TaskState

func (*StateMachine) Validate

func (sm *StateMachine) Validate() error

type States

type States map[string]State

States is the collection of states

func (*States) UnmarshalJSON

func (sm *States) UnmarshalJSON(b []byte) error

type SucceedState added in v1.0.3

type SucceedState struct {
	Type    *string
	Comment *string `json:",omitempty"`

	InputPath  *jsonpath.Path `json:",omitempty"`
	OutputPath *jsonpath.Path `json:",omitempty"`
	// contains filtered or unexported fields
}

func (*SucceedState) Execute added in v1.0.3

func (s *SucceedState) Execute(ctx context.Context, input interface{}) (output interface{}, next *string, err error)

func (*SucceedState) GetType added in v1.0.3

func (s *SucceedState) GetType() *string

func (*SucceedState) Name added in v1.0.3

func (s *SucceedState) Name() *string

func (*SucceedState) SetName added in v1.0.3

func (s *SucceedState) SetName(name *string)

func (*SucceedState) SetType added in v1.0.3

func (s *SucceedState) SetType(t *string)

func (*SucceedState) Validate added in v1.0.3

func (s *SucceedState) Validate() error

type TaskState added in v1.0.3

type TaskState struct {
	Type    *string
	Comment *string `json:",omitempty"`

	InputPath  *jsonpath.Path `json:",omitempty"`
	OutputPath *jsonpath.Path `json:",omitempty"`
	ResultPath *jsonpath.Path `json:",omitempty"`
	Parameters interface{}    `json:",omitempty"`

	Resource *string `json:",omitempty"`

	Catch []*Catcher `json:",omitempty"`
	Retry []*Retrier `json:",omitempty"`

	// Maps a Lambda Handler Function
	TaskHandler interface{} `json:"-"`

	Next *string `json:",omitempty"`
	End  *bool   `json:",omitempty"`

	TimeoutSeconds   int `json:",omitempty"`
	HeartbeatSeconds int `json:",omitempty"`
	// contains filtered or unexported fields
}

func (*TaskState) Execute added in v1.0.3

func (s *TaskState) Execute(ctx context.Context, input interface{}) (output interface{}, next *string, err error)

Input must include the Task name in $.Task

func (*TaskState) GetType added in v1.0.3

func (s *TaskState) GetType() *string

func (*TaskState) Name added in v1.0.3

func (s *TaskState) Name() *string

func (*TaskState) SetName added in v1.0.3

func (s *TaskState) SetName(name *string)

func (*TaskState) SetTaskHandler added in v1.0.3

func (s *TaskState) SetTaskHandler(resourcefn interface{})

func (*TaskState) SetType added in v1.0.3

func (s *TaskState) SetType(t *string)

func (*TaskState) Validate added in v1.0.3

func (s *TaskState) Validate() error

type WaitState added in v1.0.3

type WaitState struct {
	Type    *string
	Comment *string `json:",omitempty"`

	InputPath  *jsonpath.Path `json:",omitempty"`
	OutputPath *jsonpath.Path `json:",omitempty"`

	Seconds     *float64       `json:",omitempty"`
	SecondsPath *jsonpath.Path `json:",omitempty"`

	Timestamp     *time.Time     `json:",omitempty"`
	TimestampPath *jsonpath.Path `json:",omitempty"`

	Next *string `json:",omitempty"`
	End  *bool   `json:",omitempty"`
	// contains filtered or unexported fields
}

func (*WaitState) Execute added in v1.0.3

func (s *WaitState) Execute(ctx context.Context, input interface{}) (output interface{}, next *string, err error)

func (*WaitState) GetType added in v1.0.3

func (s *WaitState) GetType() *string

func (*WaitState) Name added in v1.0.3

func (s *WaitState) Name() *string

func (*WaitState) SetName added in v1.0.3

func (s *WaitState) SetName(name *string)

func (*WaitState) SetType added in v1.0.3

func (s *WaitState) SetType(t *string)

func (*WaitState) Validate added in v1.0.3

func (s *WaitState) Validate() error

Jump to

Keyboard shortcuts

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