-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathfunc.go
More file actions
63 lines (56 loc) · 2.08 KB
/
func.go
File metadata and controls
63 lines (56 loc) · 2.08 KB
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
60
61
62
63
package flow
import (
"context"
)
// Func adapts a `func(ctx) error` into a Step (no input, no output) named
// `name`. Convenient for one-off inline steps that don't deserve their own
// type.
//
// w.Add(flow.Step(flow.Func("greet", func(ctx context.Context) error {
// fmt.Println("hello")
// return nil
// })))
func Func(name string, do func(context.Context) error) *Function[struct{}, struct{}] {
return FuncIO(name, func(ctx context.Context, _ struct{}) (struct{}, error) {
return struct{}{}, do(ctx)
})
}
// FuncIO adapts a `func(ctx, In) (Out, error)` into a Step. The Input field
// is fed in (so you can populate it via Step(...).Input(...)) and the
// returned Out is stored on the Output field (so you can read it via
// Step(...).Output(...)).
func FuncIO[I, O any](name string, do func(context.Context, I) (O, error)) *Function[I, O] {
f := &Function[I, O]{Name: name, DoFunc: do}
return f
}
// FuncI is FuncIO for an input-only function (no output).
func FuncI[I any](name string, do func(context.Context, I) error) *Function[I, struct{}] {
return FuncIO(name, func(ctx context.Context, i I) (struct{}, error) {
return struct{}{}, do(ctx, i)
})
}
// FuncO is FuncIO for an output-only function (no input).
func FuncO[O any](name string, do func(context.Context) (O, error)) *Function[struct{}, O] {
return FuncIO(name, func(ctx context.Context, _ struct{}) (O, error) {
return do(ctx)
})
}
// Function is the Step implementation produced by Func / FuncIO / FuncI /
// FuncO. Input is supplied by the caller (typically via Step(f).Input(...)),
// passed into DoFunc on each attempt, and the return value is stashed in
// Output (so Step(f).Output(...) can pick it up). String() returns Name, so
// Function shows up nicely in logs and ErrWorkflow messages.
type Function[I, O any] struct {
Name string
Input I
Output O
DoFunc func(context.Context, I) (O, error)
}
func (f *Function[I, O]) String() string { return f.Name }
func (f *Function[I, O]) Do(ctx context.Context) error {
var err error
if f.DoFunc != nil {
f.Output, err = f.DoFunc(ctx, f.Input)
}
return err
}