From b834f30026451641ee298627c132de441a599eb8 Mon Sep 17 00:00:00 2001 From: mikeee Date: Tue, 5 Dec 2023 15:24:13 +0000 Subject: [PATCH] feat: add data serialization to workflow calls Signed-off-by: mikeee --- client/workflow.go | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/client/workflow.go b/client/workflow.go index 28594db6..93974dfb 100644 --- a/client/workflow.go +++ b/client/workflow.go @@ -2,6 +2,7 @@ package client import ( "context" + "encoding/json" "errors" "fmt" "time" @@ -18,7 +19,7 @@ type StartWorkflowRequest struct { WorkflowName string Options map[string]string // Optional metadata Input []byte // Optional input - // TODO: support data serialization + SendRawInput bool // Set to True in order to disable serialization on the input } type StartWorkflowResponse struct { @@ -64,10 +65,12 @@ type RaiseEventWorkflowRequest struct { WorkflowComponent string EventName string EventData []byte // Optional data + SendRawData bool // Set to True in order to disable serialization on the data§1 } // StartWorkflowAlpha1 starts a workflow instance using the alpha1 spec. func (c *GRPCClient) StartWorkflowAlpha1(ctx context.Context, req *StartWorkflowRequest) (*StartWorkflowResponse, error) { + if req.InstanceID == "" { req.InstanceID = uuid.New().String() } @@ -78,12 +81,21 @@ func (c *GRPCClient) StartWorkflowAlpha1(ctx context.Context, req *StartWorkflow if req.WorkflowName == "" { return nil, errors.New("failed to start workflow: WorkflowName must be supplied") } + + var input = req.Input + var err error + if (!req.SendRawInput) && (input != nil) { + input, err = json.Marshal(input) + if err != nil { + return nil, fmt.Errorf("failed to marshal input: %v", err) + } + } resp, err := c.protoClient.StartWorkflowAlpha1(ctx, &pb.StartWorkflowRequest{ InstanceId: req.InstanceID, WorkflowComponent: req.WorkflowComponent, WorkflowName: req.WorkflowName, Options: req.Options, - Input: req.Input, + Input: input, }) if err != nil { return nil, fmt.Errorf("failed to start workflow instance: %v", err) @@ -208,7 +220,15 @@ func (c *GRPCClient) RaiseEventWorkflowAlpha1(ctx context.Context, req *RaiseEve if req.EventName == "" { return errors.New("failed to raise event on workflow: EventName must be supplied") } - _, err := c.protoClient.RaiseEventWorkflowAlpha1(ctx, &pb.RaiseEventWorkflowRequest{ + var input = req.EventData + var err error + if (!req.SendRawData) && (input != nil) { + input, err = json.Marshal(input) + if err != nil { + return fmt.Errorf("failed to marshal input: %v", err) + } + } + _, err = c.protoClient.RaiseEventWorkflowAlpha1(ctx, &pb.RaiseEventWorkflowRequest{ InstanceId: req.InstanceID, WorkflowComponent: req.WorkflowComponent, EventName: req.EventName, @@ -231,6 +251,15 @@ func (c *GRPCClient) StartWorkflowBeta1(ctx context.Context, req *StartWorkflowR if req.WorkflowName == "" { return nil, errors.New("failed to start workflow: WorkflowName must be supplied") } + + var input = req.Input + var err error + if (!req.SendRawInput) && (req.Input != nil) { + input, err = json.Marshal(input) + if err != nil { + return nil, fmt.Errorf("failed to marshal input: %v", err) + } + } resp, err := c.protoClient.StartWorkflowBeta1(ctx, &pb.StartWorkflowRequest{ InstanceId: req.InstanceID, WorkflowComponent: req.WorkflowComponent, @@ -360,7 +389,16 @@ func (c *GRPCClient) RaiseEventWorkflowBeta1(ctx context.Context, req *RaiseEven if req.EventName == "" { return errors.New("failed to raise event on workflow: EventName must be supplied") } - _, err := c.protoClient.RaiseEventWorkflowBeta1(ctx, &pb.RaiseEventWorkflowRequest{ + + var input = req.EventData + var err error + if (!req.SendRawData) && (input != nil) { + input, err = json.Marshal(input) + if err != nil { + return fmt.Errorf("failed to marshal input: %v", err) + } + } + _, err = c.protoClient.RaiseEventWorkflowBeta1(ctx, &pb.RaiseEventWorkflowRequest{ InstanceId: req.InstanceID, WorkflowComponent: req.WorkflowComponent, EventName: req.EventName,