Skip to content

Instantly share code, notes, and snippets.

@ppaanngggg
Created Aug 12, 2022
Embed
What would you like to do?
package main
import (
"context"
"github.com/cortezaproject/corteza-server/automation/service"
"github.com/cortezaproject/corteza-server/automation/types"
"github.com/cortezaproject/corteza-server/pkg/expr"
)
type workflowArgs struct {
Workflow uint64
Input map[string]expr.TypedValue
}
func WorkflowHandler() *types.Function {
return &types.Function{
Ref: "workflow",
Kind: "function",
Labels: map[string]string(nil),
Meta: &types.FunctionMeta{
Short: "Call workflow",
},
Parameters: []*types.Param{
{
Name: "workflow",
Types: []string{"UnsignedInteger"},
Required: true,
Meta: &types.ParamMeta{Label: "workflow"},
},
{
Name: "input",
Types: []string{"Vars"},
},
},
Results: []*types.Param{
{
Name: "result",
Types: []string{"Vars"},
},
},
Handler: func(ctx context.Context, in *expr.Vars) (*expr.Vars, error) {
args := &workflowArgs{}
if err := in.Decode(args); err != nil {
return nil, err
}
vars, err := expr.NewVars(args.Input)
if err != nil {
return nil, err
}
wp := types.WorkflowExecParams{
Trace: true, Async: false, Wait: true, Input: vars,
}
result, _, err := service.DefaultWorkflow.Exec(ctx, args.Workflow, wp)
if err != nil {
return nil, err
}
out := &expr.Vars{}
tval, err := service.Registry().Type("Vars").Cast(result)
if err != nil {
return nil, err
}
if err := expr.Assign(out, "result", tval); err != nil {
return nil, err
}
return out, nil
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment