From 972d45ef4e7d5789c159993496981291d5d9dbe8 Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Fri, 12 Feb 2021 08:00:23 +0100 Subject: [PATCH] Add support for loop break & continue --- automation/service/workflow_converter.go | 34 ++++++++++++++++++++++++ automation/types/step.go | 2 ++ 2 files changed, 36 insertions(+) diff --git a/automation/service/workflow_converter.go b/automation/service/workflow_converter.go index e9b37e72c..3cd017837 100644 --- a/automation/service/workflow_converter.go +++ b/automation/service/workflow_converter.go @@ -149,6 +149,7 @@ func (svc workflowConverter) workflowStepDefConv(g *wfexec.Graph, s *types.Workf switch s.Kind { case types.WorkflowStepKindVisual: return nil, nil + case types.WorkflowStepKindDebug: return svc.convDebugStep(s) @@ -173,6 +174,11 @@ func (svc workflowConverter) workflowStepDefConv(g *wfexec.Graph, s *types.Workf case types.WorkflowStepKindErrHandler: return svc.convErrorHandlerStep(g, out) + case types.WorkflowStepKindBreak: + return svc.convBreakStep(out) + case types.WorkflowStepKindContinue: + return svc.convContinueStep(out) + default: return nil, errors.Internal("unsupported step kind %q", s.Kind) } @@ -438,6 +444,28 @@ func (svc workflowConverter) convPromptStep(s *types.WorkflowStep) (wfexec.Step, return types.PromptStep(s.Ref, types.ExpressionsStep(s.Arguments...)), nil } +func (svc workflowConverter) convBreakStep(out types.WorkflowPathSet) (wfexec.Step, error) { + if len(out) > 0 { + return nil, errors.Internal("break step must be last step in branch") + } + + return wfexec.NewGenericStep(func(ctx context.Context, r *wfexec.ExecRequest) (wfexec.ExecResponse, error) { + return wfexec.LoopBreak(), nil + }), nil + +} + +func (svc workflowConverter) convContinueStep(out types.WorkflowPathSet) (wfexec.Step, error) { + if len(out) > 0 { + return nil, errors.Internal("continue step must be last step in branch") + } + + return wfexec.NewGenericStep(func(ctx context.Context, r *wfexec.ExecRequest) (wfexec.ExecResponse, error) { + return wfexec.LoopContinue(), nil + }), nil + +} + func (svc workflowConverter) parseExpressions(ee ...*types.Expr) (err error) { for _, e := range ee { @@ -517,6 +545,12 @@ func verifyStep(step *types.WorkflowStep) types.WorkflowIssueSet { case types.WorkflowStepKindPrompt: checks = append(checks, noResults) + case types.WorkflowStepKindBreak: + checks = append(checks, noArgs, noResults) + + case types.WorkflowStepKindContinue: + checks = append(checks, noArgs, noResults) + default: return ii.Append(fmt.Errorf("unknown step kind"), nil) } diff --git a/automation/types/step.go b/automation/types/step.go index 63e301dcc..3544be2d7 100644 --- a/automation/types/step.go +++ b/automation/types/step.go @@ -67,6 +67,8 @@ const ( WorkflowStepKindErrHandler WorkflowStepKind = "error-handler" // no ref WorkflowStepKindVisual WorkflowStepKind = "visual" // ref = <*> WorkflowStepKindDebug WorkflowStepKind = "debug" // ref = <*> + WorkflowStepKindBreak WorkflowStepKind = "break" // ref = <*> + WorkflowStepKindContinue WorkflowStepKind = "continue" // ref = <*> ) // Scan on WorkflowStepSet gracefully handles conversion from NULL