-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
62 lines (52 loc) · 1 KB
/
main.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
60
61
62
package main
import (
"context"
"time"
pp "github.com/sonalys/pipego"
"github.com/sonalys/pipego/retry"
)
type api struct{}
func (a api) fetch(ctx context.Context, id string) (int, error) {
return 4, nil
}
type pipeline struct {
API interface {
fetch(ctx context.Context, id string) (int, error)
}
input int
sum int
square int
}
func (p *pipeline) fetchInput(id string) pp.Step {
return func(ctx context.Context) (err error) {
p.input, err = p.API.fetch(ctx, id)
return
}
}
func (p *pipeline) sumInput(ctx context.Context) (err error) {
p.sum = p.input + p.input
return
}
func (p *pipeline) sqrInput(ctx context.Context) (err error) {
p.square = p.input * p.input
return
}
func main() {
ctx := context.Background()
p := pipeline{
API: api{},
}
err := pp.Run(ctx,
retry.Constant(3, time.Second,
p.fetchInput("id"),
),
pp.Parallel(2,
p.sumInput,
p.sqrInput,
),
)
if err != nil {
println(err.Error())
}
// main.pipeline{API:main.api{}, input:4, sum:8, square:16}
}