Skip to content

Commit

Permalink
feat: add serialization to workflows
Browse files Browse the repository at this point in the history
Signed-off-by: mikeee <[email protected]>
  • Loading branch information
mikeee committed Dec 5, 2023
1 parent 4172d0b commit 99c2747
Showing 1 changed file with 47 additions and 7 deletions.
54 changes: 47 additions & 7 deletions client/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
Expand All @@ -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 {
Expand Down Expand Up @@ -64,6 +65,7 @@ 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.
Expand All @@ -78,12 +80,22 @@ func (c *GRPCClient) StartWorkflowAlpha1(ctx context.Context, req *StartWorkflow
if req.WorkflowName == "" {
return nil, errors.New("failed to start workflow: WorkflowName must be supplied")
}

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)
}

Check warning on line 90 in client/workflow.go

View check run for this annotation

Codecov / codecov/patch

client/workflow.go#L87-L90

Added lines #L87 - L90 were not covered by tests
}

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)
Expand Down Expand Up @@ -208,11 +220,20 @@ 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{

eventData := req.EventData
var err error
if (!req.SendRawData) && (eventData != nil) {
eventData, err = json.Marshal(eventData)
if err != nil {
return fmt.Errorf("failed to marshal input: %v", err)
}

Check warning on line 230 in client/workflow.go

View check run for this annotation

Codecov / codecov/patch

client/workflow.go#L227-L230

Added lines #L227 - L230 were not covered by tests
}
_, err = c.protoClient.RaiseEventWorkflowAlpha1(ctx, &pb.RaiseEventWorkflowRequest{
InstanceId: req.InstanceID,
WorkflowComponent: req.WorkflowComponent,
EventName: req.EventName,
EventData: req.EventData,
EventData: eventData,
})
if err != nil {
return fmt.Errorf("failed to raise event on workflow: %v", err)
Expand All @@ -231,12 +252,22 @@ func (c *GRPCClient) StartWorkflowBeta1(ctx context.Context, req *StartWorkflowR
if req.WorkflowName == "" {
return nil, errors.New("failed to start workflow: WorkflowName must be supplied")
}

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)
}

Check warning on line 262 in client/workflow.go

View check run for this annotation

Codecov / codecov/patch

client/workflow.go#L259-L262

Added lines #L259 - L262 were not covered by tests
}

resp, err := c.protoClient.StartWorkflowBeta1(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)
Expand Down Expand Up @@ -360,11 +391,20 @@ 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{

eventData := req.EventData
var err error
if (!req.SendRawData) && (eventData != nil) {
eventData, err = json.Marshal(eventData)
if err != nil {
return fmt.Errorf("failed to marshal input: %v", err)
}

Check warning on line 401 in client/workflow.go

View check run for this annotation

Codecov / codecov/patch

client/workflow.go#L398-L401

Added lines #L398 - L401 were not covered by tests
}
_, err = c.protoClient.RaiseEventWorkflowBeta1(ctx, &pb.RaiseEventWorkflowRequest{
InstanceId: req.InstanceID,
WorkflowComponent: req.WorkflowComponent,
EventName: req.EventName,
EventData: req.EventData,
EventData: eventData,
})
if err != nil {
return fmt.Errorf("failed to raise event on workflow: %v", err)
Expand Down

0 comments on commit 99c2747

Please sign in to comment.