Skip to content

Commit c8b6e9c

Browse files
mfateevmeiliang86
authored andcommitted
Added ability to call methodactivities by function pointer (#96)
1 parent 2e54e9f commit c8b6e9c

File tree

5 files changed

+18
-9
lines changed

5 files changed

+18
-9
lines changed

internal/internal_worker.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,16 @@ func getFunctionName(i interface{}) string {
10921092
}
10931093
fullName := runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
10941094
elements := strings.Split(fullName, ".")
1095-
return elements[len(elements)-1]
1095+
shortName := elements[len(elements)-1]
1096+
// This allows to call activities by method pointer
1097+
// Compiler adds -fm suffix to a function name which has a receiver
1098+
// Note that this works even if struct pointer used to get the function is nil
1099+
// It is possible because nil receivers are allowed.
1100+
// For example:
1101+
// var a *Activities
1102+
// ExecuteActivity(ctx, a.Foo)
1103+
// will call this function which is going to return "Foo"
1104+
return strings.TrimSuffix(shortName, "-fm")
10961105
}
10971106

10981107
func getActivityFunctionName(r *registry, i interface{}) string {

internal/internal_workflow_testsuite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2147,7 +2147,7 @@ func (s *WorkflowTestSuiteUnitTest) Test_Channel() {
21472147
}
21482148

21492149
// continue as new
2150-
return NewContinueAsNewError(ctx, "this-workflow-fn")
2150+
return NewContinueAsNewError(ctx, "this-workflow")
21512151
}
21522152

21532153
for i := range fanoutChs {

test/fixtures/activity.cancel.sm.repro.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"version": -24,
77
"workflowExecutionStartedEventAttributes": {
88
"workflowType": {
9-
"name": "ActivityCancelRepro-fm"
9+
"name": "ActivityCancelRepro"
1010
},
1111
"taskList": {
1212
"name": "tl-1"

test/integration_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,8 @@ func (ts *IntegrationTestSuite) TestBasic() {
165165
err := ts.executeWorkflow("test-basic", ts.workflows.Basic, &expected)
166166
ts.NoError(err)
167167
ts.EqualValues(expected, ts.activities.invoked())
168-
// See https://grokbase.com/p/gg/golang-nuts/153jjj8dgg/go-nuts-fm-suffix-in-function-name-what-does-it-mean
169-
// for explanation of -fm postfix.
170168
ts.Equal([]string{"ExecuteWorkflow begin", "ExecuteActivity", "ExecuteActivity", "ExecuteWorkflow end"},
171-
ts.tracer.GetTrace("Basic-fm"))
169+
ts.tracer.GetTrace("Basic"))
172170
}
173171

174172
func (ts *IntegrationTestSuite) TestActivityRetryOnError() {
@@ -374,7 +372,7 @@ func (ts *IntegrationTestSuite) TestChildWFWithMemoAndSearchAttributes() {
374372
ts.NoError(err)
375373
ts.EqualValues([]string{"getMemoAndSearchAttr"}, ts.activities.invoked())
376374
ts.Equal("memoVal, searchAttrVal", result)
377-
ts.Equal([]string{"ExecuteWorkflow begin", "ExecuteChildWorkflow", "ExecuteWorkflow end"}, ts.tracer.GetTrace("ChildWorkflowSuccess-fm"))
375+
ts.Equal([]string{"ExecuteWorkflow begin", "ExecuteChildWorkflow", "ExecuteWorkflow end"}, ts.tracer.GetTrace("ChildWorkflowSuccess"))
378376
}
379377

380378
func (ts *IntegrationTestSuite) TestChildWFWithParentClosePolicyTerminate() {

test/workflow_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,10 @@ func (w *Workflows) RetryTimeoutStableErrorWorkflow(ctx workflow.Context) ([]str
452452
},
453453
}
454454
ctx = workflow.WithActivityOptions(ctx, ao)
455-
456-
err := workflow.ExecuteActivity(ctx, "RetryTimeoutStableErrorActivity").Get(ctx, nil)
455+
// Test calling activity by method pointer
456+
// As Go allows nil receiver pointers it works fine
457+
var a *Activities
458+
err := workflow.ExecuteActivity(ctx, a.RetryTimeoutStableErrorActivity).Get(ctx, nil)
457459

458460
cerr, ok := err.(*cadence.CustomError)
459461
if !ok {

0 commit comments

Comments
 (0)