states

package
v0.0.0-...-fefb669 Latest Latest
Warning

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

Go to latest
Published: May 31, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package states implements the State behavior for the workflow states are the basic step in the workflow, they can be combined to form a complex workflow

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorLackOfRequiredField = errors.New("lack of required field")
	ErrorInvalidStateType    = errors.New("invalid state type")
	ErrorInvalidFiledContent = errors.New("field content is invalid")
	ErrorFiledDenied         = errors.New("field is dentied")
	ErrorFiledRequired       = errors.New("field is required")
	ErrorInvalidData         = errors.New("invalid data")
	ErrorInvalidField        = errors.New("invalid field")
)

ErrorLackOfRequiredField ...

View Source
var CommonFields = []string{
	StateFieldNames.Comment,
	StateFieldNames.InputPath,
	StateFieldNames.OutputPath,
	StateFieldNames.Parameters,
	StateFieldNames.ResultPath,
}

CommonFields common fields

View Source
var DefaultCatchNode = TaskCatchNode{
	ErrorEquals: []string{},
	Next:        "",
	ResultPath:  "$",
}

DefaultCatchNode default catch config for task

View Source
var DefaultRetryNode = TaskRetryNode{
	ErrorEquals:     []string{},
	IntervalSeconds: 1,
	MaxAttempts:     3,
	BackoffRate:     1.5,
}

DefaultRetryNode default retry config for task

View Source
var DefaultTaskBody = TaskBody{
	Resource: "",

	TimeoutSeconds: 0,

	HeartbeatSeconds: 0,

	Retry: []TaskRetryNode{},

	Catch: []TaskCatchNode{},
}

DefaultTaskBody ...

View Source
var FiledRequiredLevel = struct {
	Allowed  int
	Required int
	Deny     int
}{

	Allowed:  0,
	Required: 1,
	Deny:     2,
}

FiledRequiredLevel field required level

View Source
var HeaderFieldNames = struct {
	Version       string
	Type          string
	Comment       string
	QueryLanguage string
}{
	Version:       "Version",
	Type:          "Type",
	Comment:       "Comment",
	QueryLanguage: "QueryLanguage",
}

HeaderFieldNames ...

View Source
var QueryLanguages = struct {
	JSONPath QueryLanguageType
	JSONata  QueryLanguageType
}{
	JSONPath: "JSONPath",
	JSONata:  "JSONata",
}

QueryLanguages ...

View Source
var ResourceSeparator = ":"

ResourceSeparator ...

View Source
var ResourceType = struct {
	Builtin  string
	Activity string
}{
	Activity: "activity",
	Builtin:  "builtin",
}

ResourceType 资源类型

View Source
var StateFieldNames = struct {
	Type             string
	Comment          string
	InputPath        string
	OutputPath       string
	Parameters       string
	ResultPath       string
	Next             string
	End              string
	Resource         string
	Retry            string
	Catch            string
	TimeoutSeconds   string
	HeartbeatSeconds string
}{
	Type:             "Type",
	Comment:          "Comment",
	InputPath:        "InputPath",
	OutputPath:       "OutputPath",
	Parameters:       "Parameters",
	ResultPath:       "ResultPath",
	Next:             "Next",
	End:              "End",
	Resource:         "Resource",
	Retry:            "Retry",
	Catch:            "Catch",
	TimeoutSeconds:   "TimeoutSeconds",
	HeartbeatSeconds: "HeartbeatSeconds",
}

StateFieldNames common field names

View Source
var StateFieldRequiredMap = map[StateType]StateFieldRequired{

	StateTypes.Task: {

		Type:    FiledRequiredLevel.Required,
		NextEnd: FiledRequiredLevel.Required,
	},
	StateTypes.Parallel: {

		Type:    FiledRequiredLevel.Required,
		NextEnd: FiledRequiredLevel.Required,
	},

	StateTypes.Map: {

		Type:    FiledRequiredLevel.Required,
		NextEnd: FiledRequiredLevel.Required,
	},
	StateTypes.Pass: {

		Type:    FiledRequiredLevel.Required,
		NextEnd: FiledRequiredLevel.Required,

		Retry: FiledRequiredLevel.Deny,
		Catch: FiledRequiredLevel.Deny,
	},
	StateTypes.Wait: {

		Type:       FiledRequiredLevel.Required,
		NextEnd:    FiledRequiredLevel.Required,
		ResultPath: FiledRequiredLevel.Deny,
		Parameters: FiledRequiredLevel.Deny,
		Retry:      FiledRequiredLevel.Deny,
		Catch:      FiledRequiredLevel.Deny,
	},
	StateTypes.Choice: {

		Type:       FiledRequiredLevel.Required,
		NextEnd:    FiledRequiredLevel.Deny,
		ResultPath: FiledRequiredLevel.Deny,
		Parameters: FiledRequiredLevel.Deny,
		Retry:      FiledRequiredLevel.Deny,
		Catch:      FiledRequiredLevel.Deny,
	},

	StateTypes.Succeed: {

		Type:       FiledRequiredLevel.Required,
		NextEnd:    FiledRequiredLevel.Deny,
		ResultPath: FiledRequiredLevel.Deny,
		Parameters: FiledRequiredLevel.Deny,
		Retry:      FiledRequiredLevel.Deny,
		Catch:      FiledRequiredLevel.Deny,
	},
	StateTypes.Fail: {

		Type:       FiledRequiredLevel.Required,
		InputPath:  FiledRequiredLevel.Deny,
		OutputPath: FiledRequiredLevel.Deny,
		NextEnd:    FiledRequiredLevel.Deny,
		ResultPath: FiledRequiredLevel.Deny,
		Parameters: FiledRequiredLevel.Deny,
		Retry:      FiledRequiredLevel.Deny,
		Catch:      FiledRequiredLevel.Deny,
	},

	StateTypes.Suspend: {

		Type:       FiledRequiredLevel.Required,
		InputPath:  FiledRequiredLevel.Deny,
		OutputPath: FiledRequiredLevel.Deny,
		NextEnd:    FiledRequiredLevel.Deny,
		ResultPath: FiledRequiredLevel.Deny,
		Parameters: FiledRequiredLevel.Deny,
		Retry:      FiledRequiredLevel.Deny,
		Catch:      FiledRequiredLevel.Deny,
	},
	StateTypes.StateGroup: {},
}

StateFieldRequiredMap field required map

View Source
var StateMachineFieldNames = struct {
	StartAt string
	States  string
}{
	StartAt: "StartAt",
	States:  "States",
}
View Source
var StateMachineType = "statemachine"

StateMachineType ...

View Source
var StateTypes = struct {
	Task     StateType
	Choice   StateType
	Fail     StateType
	Succeed  StateType
	Wait     StateType
	Pass     StateType
	Parallel StateType
	Map      StateType
	Suspend  StateType
	// 子流程类型, Parallel/Map 生成的中间步骤
	StateGroup StateType
}{
	Task:       "Task",
	Choice:     "Choice",
	Fail:       "Fail",
	Succeed:    "Succeed",
	Wait:       "Wait",
	Pass:       "Pass",
	Parallel:   "Parallel",
	Map:        "Map",
	Suspend:    "Suspend",
	StateGroup: "StateGroup",
}

StateTypes constant State type define here

View Source
var TimeFormatCommon = "2006-01-02 15:04:05"
View Source
var TimeFormatList = []string{TimeFormatCommon, time.RFC3339}
View Source
var VariablePrefix = "$."

VariablePrefix 变量字段的前缀

Functions

func HasIntersection

func HasIntersection(x []string, y []string) bool

HasIntersection return if x and y have common elements

func IsExecutableStateType

func IsExecutableStateType(stype string) bool

IsExecutableStateType 是否是可执行的步骤类型

func ParserStateMachine

func ParserStateMachine(definition string) error

ParserStateMachine ...

func RegisterResource

func RegisterResource(resource string)

RegisterResource ...

func ValidateStateFieldOptional

func ValidateStateFieldOptional(data map[string]any) error

ValidateStateFieldOptional validate state field optional

Types

type BaseBone

type BaseBone struct {
	Type    string   `json:"Type"`
	Name    string   `json:"Name"`
	Next    []string `json:"Next"`
	End     bool     `json:"End"`
	Comment string   `json:"Comment"`
}

BaseBone common state type bone

type BaseState

type BaseState struct {
	Name            string `json:"Name,omitempty"`
	Type            string `json:"Type,omitempty"`
	Comment         string `json:"Comment,omitempty"`
	InputPath       string `json:"InputPath,omitempty"`
	OutputPath      string `json:"OutputPath,omitempty"`
	ResultPath      string `json:"ResultPath,omitempty"`
	Parameters      any    `json:"Parameters,omitempty"`
	MaxExecuteTimes int    `json:"MaxExecuteTimes,omitempty"`
	End             bool   `json:"End,omitempty"`
	Next            string `json:"Next,omitempty"`
	Retry           any    `json:"Retry,omitempty"`
	Catch           any    `json:"Catch,omitempty"`
}

BaseState is a struct that defines the base state of a state machine, with default values

func (*BaseState) GenParameters

func (s *BaseState) GenParameters(input any) (any, error)

GenParameters calculate parameters by input data and parameters @input input state origin input return parameters

func (*BaseState) GetBone

func (s *BaseState) GetBone() StateBone

GetBone ...

func (*BaseState) GetName

func (s *BaseState) GetName() string

GetName ...

func (*BaseState) GetNextState

func (s *BaseState) GetNextState(input any, output any) (NextState, error)

GetNextState get next state info generate final output using inputpath parameters resultpath outputpath @input state origin input @output state produce data return next state info

func (*BaseState) GetOutput

func (s *BaseState) GetOutput(input any, output any) (any, error)

GetOutput calculate task output cautions: 1. return nil if outputpath is empty 2. return output is not a copy of input, it will reference data from input and output, which is defined in resultpath and outputpath @input input state origin input @output output state produce data return output state produce data

func (*BaseState) GetOutputWithPath

func (s *BaseState) GetOutputWithPath(input any, output any, resultpath string, outputpath string) (any, error)

GetOutputWithPath calculate output with resultpath and outputpath cautions: 1. return nil if outputpath is empty 2. return output is not a copy of input, it will reference data from input and output, which is defined in resultpath and outputpath @input input state origin input @output output state produce data @resultpath resultpath @outputpath outputpath

func (*BaseState) GetParametersInput

func (s *BaseState) GetParametersInput(input any) (any, error)

GetParametersInput get state parameters input input , origin input, output state input using inputpath , parameters

func (*BaseState) GetType

func (s *BaseState) GetType() string

GetType ...

func (*BaseState) Init

func (s *BaseState) Init() error

Init ...

func (*BaseState) RenderParameters

func (s *BaseState) RenderParameters(input any, parameters any) (any, error)

RenderParameters render parameters @input input state origin input @parameters parameters field parameters return parameters

func (*BaseState) SetName

func (s *BaseState) SetName(name string)

SetName ...

func (*BaseState) Validate

func (s *BaseState) Validate() error

Validate ...

type Choice

type Choice struct {
	*BaseState
	*ChoiceBody
}

Choice ...

func (*Choice) ChoiceNextState

func (choice *Choice) ChoiceNextState(input any) string

ChoiceNextState

func (*Choice) GetBone

func (choice *Choice) GetBone() StateBone

GetBone get choice bone

func (*Choice) GetNextState

func (choice *Choice) GetNextState(input any) (NextState, error)

GetNextState Get Next State input state origin input

func (*Choice) IsEnd

func (choice *Choice) IsEnd() bool

IsEnd check if choice is end, return false always, choice is not end state

type ChoiceBody

type ChoiceBody struct {
	Choices []ChoiceBranch `mapstructure:"Choices"`
	Default string         `mapstructure:"Default" validate:"gte=0"`
}

ChoiceBody ...

type ChoiceBranch

type ChoiceBranch struct {
	Condition expression.BooleanExpression
	Next      string `mapstructure:"Next" validate:"required,gt=0"`
}

type FieldError

type FieldError struct {
	// The error that occurred
	RawError error
	Line     int64
	Column   int64
	Offset   int64
	Paths    []string
}

FieldError is an error that occurred in a field at a specific path or line number and column number or offset in the file. FieldError is final state error for FieldPathError.

func NewFieldPathError

func NewFieldPathError(err error, paths ...string) *FieldError

NewFieldPathError ...

func (*FieldError) Error

func (e *FieldError) Error() string

Error ...

type FiledPathError

type FiledPathError struct {
	// The error that occurred
	RawError error
	Paths    []string
}

FiledPathError FieldPathError is an error that occurred in a field at a specific path.

func (*FiledPathError) Error

func (e *FiledPathError) Error() string

Error ...

type NextState

type NextState struct {
	Name       string        // Next State Name
	Output     interface{}   // State Output as the input of the next state
	Delay      time.Duration // Delay delay duration from current state to next state
	Retry      bool          // Whether to trigger retry
	RetryIndex int           // Which Indexed Retry strategy hit
}

NextState next state info

type Pass

type Pass struct {
	*BaseState
	*PassBody
}

Pass ...

func (*Pass) GetNextState

func (p *Pass) GetNextState(input any) (NextState, error)

GetNextState Get Next State

func (*Pass) GetResult

func (p *Pass) GetResult(input any) (any, error)

GetResult render result with input and parameters

type PassBody

type PassBody struct {
	Result map[string]any `mapstructure:"Result" validate:"required"`
}

PassBody ...

func (*PassBody) GetOutput

func (p *PassBody) GetOutput(input any) (any, error)

type QueryLanguageType

type QueryLanguageType string

QueryLanguageType 查询语言类型

type ResourceURI

type ResourceURI struct {
	Resource     string
	ResourceType string
	Function     string
}

ResourceURI resource uri defintion

func ParseResource

func ParseResource(resource string) (uri *ResourceURI, err error)

ParseResource parser path to ResourceURI

type State

type State interface {
	Init() error
	Validate() error
	GetName() string
	SetName(name string)
	GetType() string
	GetBone() StateBone
}

State ...

type StateBone

type StateBone struct {
	BaseBone
	*StateMachineBone
	Branches []StateBone `json:"Branches"`
}

StateBone for map /parallel state bone

type StateFieldRequired

type StateFieldRequired struct {
	Comment    int
	Type       int
	InputPath  int
	OutputPath int
	Parameters int
	ResultPath int
	NextEnd    int
	Retry      int
	Catch      int
}

StateFieldRequired state field required

type StateMachine

type StateMachine struct {
	*StateMachineHeader
	*StateMachineBody
}

StateMachine ...

func (*StateMachine) Validate

func (s *StateMachine) Validate() string

Validate ...

type StateMachineBody

type StateMachineBody struct {
	StartAt string
	States  map[string]State
}

StateMachineBody ...

func (*StateMachineBody) AddState

func (s *StateMachineBody) AddState(state State)

AddState ...

func (*StateMachineBody) GetBone

func (s *StateMachineBody) GetBone() StateMachineBone

func (*StateMachineBody) SetStartAt

func (s *StateMachineBody) SetStartAt(name string)

func (*StateMachineBody) Validate

func (s *StateMachineBody) Validate() error

Validate ...

type StateMachineBone

type StateMachineBone struct {
	StartAt string               `json:"StartAt"`
	States  map[string]StateBone `json:"States"`
}

StateMachineBone StateMachineBone

type StateMachineHeader

type StateMachineHeader struct {
	Version       string
	Type          string
	Comment       string
	QueryLanguage string
}

StateMachineHeader ...

func (*StateMachineHeader) Init

func (header *StateMachineHeader) Init() error

Init ...

type StateType

type StateType string

StateType state type

type Task

type Task struct {
	*BaseState
	*TaskBody
}

Task ...

func (*Task) GetBone

func (t *Task) GetBone() StateBone

GetBone get bone

func (*Task) GetNextState

func (t *Task) GetNextState(input interface{}, taskdata TaskSendData) (NextState, error)

GetNextState get task next state @input state input data @taskdata task send data return next state

func (*Task) GetTaskTimeout

func (t *Task) GetTaskTimeout() (TaskTimeout, error)

GetTaskTimeout return task timeout

func (*Task) Init

func (t *Task) Init() error

Init init task

func (*Task) Validate

func (t *Task) Validate() error

Validate ...

type TaskBody

type TaskBody struct {
	Resource         string `validate:"required,gt=0"`
	TimeoutSeconds   uint   `validate:"gte=0"`
	HeartbeatSeconds uint   `validate:"gte=0"`
	// Retry for decode
	Retry []TaskRetryNode `mapstructure:"Retry"`
	// Catch for decode
	Catch []TaskCatchNode `mapstructure:"Catch"`
}

TaskBody ...

func (*TaskBody) Init

func (body *TaskBody) Init() error

Init ...

func (*TaskBody) Validate

func (body *TaskBody) Validate() error

Validate ...

type TaskCatchNode

type TaskCatchNode struct {
	ErrorEquals []string `mapstructure:"ErrorEquals"`
	Next        string   `mapstructure:"Next"`
	ResultPath  string   `mapstructure:"ResultPath"`
}

TaskCatchNode struct for catch

type TaskRetryNode

type TaskRetryNode struct {
	ErrorEquals     []string `mapstructure:"ErrorEquals"`
	IntervalSeconds uint     `mapstructure:"IntervalSeconds"`
	MaxAttempts     uint     `mapstructure:"MaxAttempts"`
	BackoffRate     float64  `mapstructure:"BackoffRate"`
}

TaskRetryNode struct for retry

type TaskSendData

type TaskSendData struct {
	Success bool     // is task submit success
	Retry   []int    // current retry times, index is retry times for each Retry branch
	Errors  []string // send error list
	Output  any      // task state execute output
}

TaskSendData task state execute and send data

type TaskTimeout

type TaskTimeout struct {
	TaskTimeout      time.Duration
	HeartBeatTimeout time.Duration
}

TaskTimeout Describe Task Timeout demand

type Wait

type Wait struct {
	*BaseState
	*WaitBody
}

Wait ...

func (*Wait) GetNextState

func (w *Wait) GetNextState(input interface{}) (NextState, error)

GetNextState Get Next State

func (*Wait) GetWakeupTime

func (w *Wait) GetWakeupTime(input any) (time.Time, error)

GetWakeupTime Get Wait State Wake up time

func (*Wait) Init

func (w *Wait) Init() error

type WaitBody

type WaitBody struct {
	Seconds       uint   `mapstructure:"Seconds" validate:"gte=0"`
	Timestamp     string `mapstructure:"Timestamp" validate:"gte=0"`
	SecondsPath   string `mapstructure:"SecondsPath" validate:"gte=0"`
	TimestampPath string `mapstructure:"TimestampPath" validate:"gte=0"`
}

WaitBody ...

Jump to

Keyboard shortcuts

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