diff --git a/automation/types/step.go b/automation/types/step.go new file mode 100644 index 000000000..63e301dcc --- /dev/null +++ b/automation/types/step.go @@ -0,0 +1,119 @@ +package types + +import ( + "context" + "database/sql/driver" + "encoding/json" + "fmt" + "github.com/cortezaproject/corteza-server/pkg/expr" +) + +type ( + // WorkflowStep describes one workflow step + WorkflowStep struct { + ID uint64 `json:"stepID,string"` + Kind WorkflowStepKind `json:"kind"` + + // reference to function or subprocess (workflow) + Ref string `json:"ref"` + + // set of expressions to evaluate, test or pass to function + // invalid for for kind=~gateway:* + Arguments []*Expr `json:"arguments"` + + // only valid when kind=function + Results []*Expr `json:"results"` + + Meta WorkflowStepMeta `json:"meta,omitempty"` + + Labels map[string]string `json:"labels,omitempty"` + } + + WorkflowStepMeta struct { + Name string `json:"name"` + Description string `json:"description"` + Visual map[string]interface{} `json:"visual"` + } + + // WorkflowPath defines connection between two workflow steps + WorkflowPath struct { + // Expression to evaluate over the input variables; results will be set to scope under variable Name + Expr string `json:"expr,omitempty"` + + eval expr.Evaluable + + ParentID uint64 `json:"parentID,string"` + ChildID uint64 `json:"childID,string"` + Meta WorkflowPathMeta `json:"meta,omitempty"` + } + + WorkflowPathMeta struct { + Name string `json:"name"` + Description string `json:"description"` + Visual map[string]interface{} `json:"visual"` + } + + WorkflowStepKind string +) + +const ( + WorkflowStepKindExpressions WorkflowStepKind = "expressions" // ref + WorkflowStepKindGateway WorkflowStepKind = "gateway" // ref = join|fork|excl|incl + WorkflowStepKindFunction WorkflowStepKind = "function" // ref = + WorkflowStepKindIterator WorkflowStepKind = "iterator" // ref = + WorkflowStepKindError WorkflowStepKind = "error" // no ref + WorkflowStepKindTermination WorkflowStepKind = "termination" // no ref + WorkflowStepKindPrompt WorkflowStepKind = "prompt" // ref = + WorkflowStepKindErrHandler WorkflowStepKind = "error-handler" // no ref + WorkflowStepKindVisual WorkflowStepKind = "visual" // ref = <*> + WorkflowStepKindDebug WorkflowStepKind = "debug" // ref = <*> +) + +// Scan on WorkflowStepSet gracefully handles conversion from NULL +func (vv WorkflowStepSet) Value() (driver.Value, error) { + return json.Marshal(vv) +} + +func (vv *WorkflowStepSet) Scan(value interface{}) error { + //lint:ignore S1034 This typecast is intentional, we need to get []byte out of a []uint8 + switch value.(type) { + case nil: + *vv = WorkflowStepSet{} + case []uint8: + b := value.([]byte) + if err := json.Unmarshal(b, vv); err != nil { + return fmt.Errorf("can not scan '%v' into WorkflowStepSet: %w", string(b), err) + } + } + + return nil +} + +func (t WorkflowPath) GetExpr() string { return t.Expr } +func (t *WorkflowPath) SetEval(eval expr.Evaluable) { t.eval = eval } +func (t WorkflowPath) Eval(ctx context.Context, scope *expr.Vars) (interface{}, error) { + return t.eval.Eval(ctx, scope) +} +func (t WorkflowPath) Test(ctx context.Context, scope *expr.Vars) (bool, error) { + return t.eval.Test(ctx, scope) +} + +func (vv *WorkflowPathSet) Scan(value interface{}) error { + //lint:ignore S1034 This typecast is intentional, we need to get []byte out of a []uint8 + switch value.(type) { + case nil: + *vv = WorkflowPathSet{} + case []uint8: + b := value.([]byte) + if err := json.Unmarshal(b, vv); err != nil { + return fmt.Errorf("can not scan '%v' into WorkflowPathSet: %w", string(b), err) + } + } + + return nil +} + +// Scan on WorkflowPathSet gracefully handles conversion from NULL +func (vv WorkflowPathSet) Value() (driver.Value, error) { + return json.Marshal(vv) +} diff --git a/automation/types/workflow.go b/automation/types/workflow.go index db8f44128..6562b0926 100644 --- a/automation/types/workflow.go +++ b/automation/types/workflow.go @@ -1,7 +1,6 @@ package types import ( - "context" "database/sql/driver" "encoding/json" "fmt" @@ -78,65 +77,6 @@ type ( Culprit map[string]int `json:"culprit"` Description string `json:"description"` } - - // WorkflowStep describes one workflow step - WorkflowStep struct { - ID uint64 `json:"stepID,string"` - Kind WorkflowStepKind `json:"kind"` - - // reference to function or subprocess (workflow) - Ref string `json:"ref"` - - // set of expressions to evaluate, test or pass to function - // invalid for for kind=~gateway:* - Arguments []*Expr `json:"arguments"` - - // only valid when kind=function - Results []*Expr `json:"results"` - - Meta WorkflowStepMeta `json:"meta,omitempty"` - - Labels map[string]string `json:"labels,omitempty"` - } - - WorkflowStepMeta struct { - Name string `json:"name"` - Description string `json:"description"` - Visual map[string]interface{} `json:"visual"` - } - - // WorkflowPath defines connection between two workflow steps - WorkflowPath struct { - // Expression to evaluate over the input variables; results will be set to scope under variable Name - Expr string `json:"expr,omitempty"` - - eval expr.Evaluable - - ParentID uint64 `json:"parentID,string"` - ChildID uint64 `json:"childID,string"` - Meta WorkflowPathMeta `json:"meta,omitempty"` - } - - WorkflowPathMeta struct { - Name string `json:"name"` - Description string `json:"description"` - Visual map[string]interface{} `json:"visual"` - } - - WorkflowStepKind string -) - -const ( - WorkflowStepKindExpressions WorkflowStepKind = "expressions" // ref - WorkflowStepKindGateway WorkflowStepKind = "gateway" // ref = join|fork|excl|incl - WorkflowStepKindFunction WorkflowStepKind = "function" // ref = - WorkflowStepKindIterator WorkflowStepKind = "iterator" // ref = - WorkflowStepKindError WorkflowStepKind = "error" // no ref - WorkflowStepKindTermination WorkflowStepKind = "termination" // no ref - WorkflowStepKindPrompt WorkflowStepKind = "prompt" // ref = - WorkflowStepKindErrHandler WorkflowStepKind = "error-handler" // no ref - WorkflowStepKindVisual WorkflowStepKind = "visual" // ref = <*> - WorkflowStepKindDebug WorkflowStepKind = "debug" // ref = <*> ) // Resource returns a resource ID for this type @@ -217,52 +157,3 @@ func (set WorkflowIssueSet) SetCulprit(name string, pos int) WorkflowIssueSet { return set } - -func (t WorkflowPath) GetExpr() string { return t.Expr } -func (t *WorkflowPath) SetEval(eval expr.Evaluable) { t.eval = eval } -func (t WorkflowPath) Eval(ctx context.Context, scope *expr.Vars) (interface{}, error) { - return t.eval.Eval(ctx, scope) -} -func (t WorkflowPath) Test(ctx context.Context, scope *expr.Vars) (bool, error) { - return t.eval.Test(ctx, scope) -} - -func (vv *WorkflowStepSet) Scan(value interface{}) error { - //lint:ignore S1034 This typecast is intentional, we need to get []byte out of a []uint8 - switch value.(type) { - case nil: - *vv = WorkflowStepSet{} - case []uint8: - b := value.([]byte) - if err := json.Unmarshal(b, vv); err != nil { - return fmt.Errorf("can not scan '%v' into WorkflowStepSet: %w", string(b), err) - } - } - - return nil -} - -// Scan on WorkflowStepSet gracefully handles conversion from NULL -func (vv WorkflowStepSet) Value() (driver.Value, error) { - return json.Marshal(vv) -} - -func (vv *WorkflowPathSet) Scan(value interface{}) error { - //lint:ignore S1034 This typecast is intentional, we need to get []byte out of a []uint8 - switch value.(type) { - case nil: - *vv = WorkflowPathSet{} - case []uint8: - b := value.([]byte) - if err := json.Unmarshal(b, vv); err != nil { - return fmt.Errorf("can not scan '%v' into WorkflowPathSet: %w", string(b), err) - } - } - - return nil -} - -// Scan on WorkflowPathSet gracefully handles conversion from NULL -func (vv WorkflowPathSet) Value() (driver.Value, error) { - return json.Marshal(vv) -}