Skip to content

Instantly share code, notes, and snippets.

@ppaanngggg
Created August 12, 2022 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ppaanngggg/5049a1845247aae21a6eb22681d7fad6 to your computer and use it in GitHub Desktop.
Save ppaanngggg/5049a1845247aae21a6eb22681d7fad6 to your computer and use it in GitHub Desktop.
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