From 4004c225c121a2f6d236d23c18701ed72913b847 Mon Sep 17 00:00:00 2001 From: steebchen Date: Wed, 26 Jun 2024 21:10:14 +0100 Subject: [PATCH] fix(go-sdk): prefix action names with workflow name --- pkg/client/types/action.go | 29 +++++++++++++---------------- pkg/worker/service.go | 4 ++-- pkg/worker/worker.go | 12 ++++++++---- pkg/worker/workflow.go | 14 +++++++++----- 4 files changed, 32 insertions(+), 27 deletions(-) diff --git a/pkg/client/types/action.go b/pkg/client/types/action.go index d96d9b612..fad21fdd1 100644 --- a/pkg/client/types/action.go +++ b/pkg/client/types/action.go @@ -14,19 +14,15 @@ type Action struct { // Required. The verb to perform. Verb string - // Optional. A way to unique identify the step. - Subresource string + // The workflow name. Optional for compatibility reasons. + Workflow string } func (o Action) String() string { - if o.Subresource != "" { - return fmt.Sprintf("%s:%s:%s", o.Service, o.Verb, o.Subresource) + if o.Workflow != "" { + return fmt.Sprintf("%s:%s:%s", o.Workflow, o.Service, o.Verb) } - return o.IntegrationVerbString() -} - -func (o Action) IntegrationVerbString() string { return fmt.Sprintf("%s:%s", o.Service, o.Verb) } @@ -39,18 +35,19 @@ func ParseActionID(actionID string) (Action, error) { return Action{}, fmt.Errorf("invalid action id %s, must have at least 2 strings separated : (colon)", actionID) } - Service := firstToLower(parts[0]) - verb := strings.ToLower(parts[1]) - - var subresource string + var workflow string if numParts == 3 { - subresource = firstToLower(parts[2]) + workflow = firstToLower(parts[0]) + parts = parts[1:] } + Service := firstToLower(parts[0]) + verb := strings.ToLower(parts[1]) + return Action{ - Service: Service, - Verb: verb, - Subresource: subresource, + Service: Service, + Verb: verb, + Workflow: workflow, }, nil } diff --git a/pkg/worker/service.go b/pkg/worker/service.go index 5bf71c5f5..7346d004b 100644 --- a/pkg/worker/service.go +++ b/pkg/worker/service.go @@ -56,7 +56,7 @@ func (s *Service) On(t triggerConverter, workflow workflowConverter) error { } } - err = s.worker.registerAction(parsedAction.Service, parsedAction.Verb, fn) + err = s.worker.registerAction(apiWorkflow.Name, parsedAction.Service, parsedAction.Verb, fn) if err != nil { return err @@ -89,7 +89,7 @@ func (s *Service) RegisterAction(fn any, opts ...RegisterActionOpt) error { fnOpts.name = getFnName(fn) } - return s.worker.registerAction(s.Name, fnOpts.name, fn) + return s.worker.registerAction("none", s.Name, fnOpts.name, fn) } func (s *Service) Call(verb string) *WorkflowStep { diff --git a/pkg/worker/worker.go b/pkg/worker/worker.go index 6361d5f09..dbc518340 100644 --- a/pkg/worker/worker.go +++ b/pkg/worker/worker.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "reflect" + "strings" "sync" "time" @@ -197,7 +198,7 @@ func NewWorker(fs ...WorkerOpt) (*Worker, error) { for _, integrationAction := range actions { action := fmt.Sprintf("%s:%s", integrationId, integrationAction) - err := w.registerAction(integrationId, action, integration.ActionHandler(integrationAction)) + err := w.registerAction("integration", integrationId, action, integration.ActionHandler(integrationAction)) if err != nil { return nil, fmt.Errorf("could not register integration action %s: %w", action, err) @@ -266,11 +267,14 @@ func (w *Worker) RegisterAction(actionId string, method any) error { return fmt.Errorf("could not parse action id: %w", err) } - return w.registerAction(action.Service, action.Verb, method) + return w.registerAction("none", action.Service, action.Verb, method) } -func (w *Worker) registerAction(service, verb string, method any) error { - actionId := fmt.Sprintf("%s:%s", service, verb) +func (w *Worker) registerAction(wf, service, verb string, method any) error { + wf = strings.ToLower(wf) // TODO + wf = strings.ReplaceAll(wf, " ", "-") // TODO + + actionId := fmt.Sprintf("%s:%s:%s", wf, service, verb) // if the service is "concurrency", then this is a special action if service == "concurrency" { diff --git a/pkg/worker/workflow.go b/pkg/worker/workflow.go index 1fe03c082..fb00f4d14 100644 --- a/pkg/worker/workflow.go +++ b/pkg/worker/workflow.go @@ -245,7 +245,7 @@ func (j *WorkflowJob) ToActionMap(svcName string) map[string]any { res := map[string]any{} for i, step := range j.Steps { - actionId := step.GetActionId(svcName, i) + actionId := step.GetActionId(j.Name, svcName, i) res[actionId] = step.Function } @@ -348,7 +348,7 @@ func (w *WorkflowStep) ToActionMap(svcName string) map[string]any { step := *w return map[string]any{ - step.GetActionId(svcName, 0): w.Function, + step.GetActionId(w.Name, svcName, 0): w.Function, } } @@ -375,7 +375,7 @@ func (w *WorkflowStep) ToWorkflowStep(svcName string, index int, namespace strin Name: res.Id, ID: w.GetStepId(index), Timeout: w.Timeout, - ActionID: w.GetActionId(svcName, index), + ActionID: w.GetActionId(w.Name, svcName, index), Parents: []string{}, Retries: w.Retries, } @@ -443,10 +443,14 @@ func (w *WorkflowStep) GetStepId(index int) string { return stepId } -func (w *WorkflowStep) GetActionId(svcName string, index int) string { +func (w *WorkflowStep) GetActionId(wfName, svcName string, index int) string { stepId := w.GetStepId(index) - return fmt.Sprintf("%s:%s", svcName, stepId) + wf := wfName + wf = strings.ToLower(wf) + wf = strings.ReplaceAll(wf, " ", "-") + + return fmt.Sprintf("%s:%s:%s", wf, svcName, stepId) } func getFnName(fn any) string {