Skip to content

Commit

Permalink
fix: improve go-sdk options and fix fn name detection (#51)
Browse files Browse the repository at this point in the history
* fix: improve detection of fn name

* fix: support multiple events and crons

* feat: support single functions in go-sdk
  • Loading branch information
abelanger5 authored Jan 2, 2024
1 parent 373b9f4 commit 2e87128
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 50 deletions.
62 changes: 58 additions & 4 deletions pkg/worker/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ func (c Cron) ToWorkflowTriggers(wt *types.WorkflowTriggers) {
wt.Cron = append(wt.Cron, string(c))
}

type Crons []string

func (c Crons) ToWorkflowTriggers(wt *types.WorkflowTriggers) {
if wt.Cron == nil {
wt.Cron = []string{}
}

wt.Cron = append(wt.Cron, c...)
}

type Event string

func (e Event) ToWorkflowTriggers(wt *types.WorkflowTriggers) {
Expand All @@ -33,6 +43,16 @@ func (e Event) ToWorkflowTriggers(wt *types.WorkflowTriggers) {
wt.Events = append(wt.Events, string(e))
}

type Events []string

func (e Events) ToWorkflowTriggers(wt *types.WorkflowTriggers) {
if wt.Events == nil {
wt.Events = []string{}
}

wt.Events = append(wt.Events, e...)
}

type workflowConverter interface {
ToWorkflow(svcName string) types.Workflow
ToActionMap(svcName string) map[string]any
Expand All @@ -42,6 +62,29 @@ type Workflow struct {
Jobs []WorkflowJob
}

type workflowFn struct {
Function any
}

func Fn(f any) workflowFn {
return workflowFn{
Function: f,
}
}

func (w workflowFn) ToWorkflow(svcName string) types.Workflow {
workflowJob := &WorkflowJob{
Name: getFnName(w.Function),
Steps: []WorkflowStep{
{
Function: w.Function,
},
},
}

return workflowJob.ToWorkflow(svcName)
}

type WorkflowJob struct {
// The name of the job
Name string
Expand Down Expand Up @@ -178,7 +221,7 @@ func (s *WorkflowStep) ToWorkflowStep(prevStep *step, svcName string, index int)
}

func (s *WorkflowStep) GetStepId(index int) string {
stepId := s.getFnName()
stepId := getFnName(s.Function)

// this can happen if the function is anonymous
if stepId == "" {
Expand All @@ -194,8 +237,19 @@ func (s *WorkflowStep) GetActionId(svcName string, index int) string {
return fmt.Sprintf("%s:%s", svcName, stepId)
}

func (s *WorkflowStep) getFnName() string {
fnName := runtime.FuncForPC(reflect.ValueOf(s.Function).Pointer()).Name()
func getFnName(fn any) string {
fnInfo := runtime.FuncForPC(reflect.ValueOf(fn).Pointer())
fnName := fnInfo.Name()

// get after the last /
if strings.LastIndex(fnName, "/") != -1 {
fnName = fnName[strings.LastIndex(fnName, "/")+1:]
}

// get after the first .
if firstDotIndex := strings.Index(fnName, "."); firstDotIndex != -1 {
fnName = fnName[firstDotIndex+1:]
}

return strings.Split(fnName, ".")[1]
return strings.ReplaceAll(fnName, ".", "-")
}
120 changes: 74 additions & 46 deletions pkg/worker/workflow_test.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,76 @@
package worker

// import (
// "context"
// "testing"
// )

// // type actionInput struct {
// // Message string `json:"message"`
// // }

// // type stepOneOutput struct {
// // Message string `json:"message"`
// // }

// // type stepTwoOutput struct {
// // Message string `json:"message"`
// // }

// // func TestToWorkflowJob(t *testing.T) {
// // testJob := WorkflowJob{
// // Name: "test",
// // Description: "test",
// // Timeout: "1m",
// // Steps: []WorkflowStep{
// // {
// // ActionId: "test:test",
// // Function: func(ctx context.Context, input *actionInput) (result *stepOneOutput, err error) {
// // return nil, nil
// // },
// // },
// // {
// // ActionId: "test:test",
// // Function: func(ctx context.Context, input *stepOneOutput) (result *stepTwoOutput, err error) {
// // return nil, nil
// // },
// // },
// // },
// // }

// // job, err := testJob.ToWorkflowJob()

// // if err != nil {
// // t.Fatalf("could not convert workflow job: %v", err)
// // }

// // t.Fatalf("%v", job)
// // }
import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

func namedFunction() {}

func TestGetFnNameAnon(t *testing.T) {
fn := func() {}

name := getFnName(fn)

if name != "TestGetFnNameAnon-func1" {
t.Fatalf("expected function name to be TestGetFnNameAnon-func1, got %s", name)
}

name = getFnName(func() {})

if name != "TestGetFnNameAnon-func2" {
t.Fatalf("expected function name to be TestGetFnNameAnon-func2, got %s", name)
}

name = getFnName(namedFunction)

if name != "namedFunction" {
t.Fatalf("expected function name to be namedFunction, got %s", name)
}
}

type actionInput struct {
Message string `json:"message"`
}

type stepOneOutput struct {
Message string `json:"message"`
}

type stepTwoOutput struct {
Message string `json:"message"`
}

func TestToWorkflowJob(t *testing.T) {
testJob := WorkflowJob{
Name: "test",
Description: "test",
Timeout: "1m",
Steps: []WorkflowStep{
{
Function: func(ctx context.Context, input *actionInput) (result *stepOneOutput, err error) {
return nil, nil
},
},
{
Function: func(ctx context.Context, input *stepOneOutput) (result *stepTwoOutput, err error) {
return nil, nil
},
},
},
}

workflow := testJob.ToWorkflow("default")

assert.Equal(t, "test", workflow.Name)
}

func TestFnToWorkflow(t *testing.T) {
workflow := Fn(func(ctx context.Context, input *actionInput) (result *stepOneOutput, err error) {
return nil, nil
}).ToWorkflow("default")

assert.Equal(t, "TestFnToWorkflow-func1", workflow.Name)
}

0 comments on commit 2e87128

Please sign in to comment.