-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfunc.go
More file actions
34 lines (27 loc) · 820 Bytes
/
func.go
File metadata and controls
34 lines (27 loc) · 820 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package runnable
import (
"context"
"reflect"
"runtime"
)
// RunnableFunc is a function that implements the Runnable contract.
type RunnableFunc func(context.Context) error
type funcRunnable struct {
name string
fn RunnableFunc
}
func (f *funcRunnable) runnableName() string { return f.name }
// Func returns a Runnable from a function. The name is derived from the function using reflection.
func Func(fn RunnableFunc) *funcRunnable {
name := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
return &funcRunnable{name, fn}
}
// Name sets the runnable name, used in log messages.
// Defaults to the function name derived via reflection.
func (f *funcRunnable) Name(name string) *funcRunnable {
f.name = name
return f
}
func (f *funcRunnable) Run(ctx context.Context) error {
return f.fn(ctx)
}