-
Notifications
You must be signed in to change notification settings - Fork 4
/
runnable.go
59 lines (47 loc) · 1.06 KB
/
runnable.go
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package runnable
import (
"context"
"reflect"
"runtime"
"strings"
)
// Runnable is the contract for anything that runs with a Go context, respects the concellation contract,
// and expects the caller to handle errors.
type Runnable interface {
Run(context.Context) error
}
func findName(t interface{}) string {
var parts []string
for t != nil {
part := findNameFromOne(t)
if part != "" {
parts = append(parts, part)
}
if r, ok := t.(interface{ RunnableUnwrap() any }); ok {
t = r.RunnableUnwrap()
continue
}
break
}
return strings.Join(parts, "/")
}
func findNameFromOne(t any) string {
if r, ok := t.(interface{ RunnableName() string }); ok {
return r.RunnableName()
}
valueOf := reflect.ValueOf(t)
if valueOf.Kind() == reflect.Func {
return runtime.FuncForPC(valueOf.Pointer()).Name() + "()"
}
return reflect.Indirect(valueOf).Type().Name()
}
type baseWrapper struct {
name string
wrapped any
}
func (w *baseWrapper) RunnableName() string {
return w.name
}
func (w *baseWrapper) RunnableUnwrap() any {
return w.wrapped
}