-
Notifications
You must be signed in to change notification settings - Fork 1
/
pipe.go
78 lines (62 loc) · 1.29 KB
/
pipe.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package pipe
import "errors"
type actionFunc[T any] func(input *T) error
type action[T any] struct {
fn actionFunc[T]
cfg runCfg
}
type Pipe[T any] struct {
initFns []func(*T)
actions []action[T]
onErr func(error) error
}
func New[T any](initFns ...func(*T)) *Pipe[T] {
return &Pipe[T]{
initFns: initFns,
onErr: func(e error) error {
/* do nothing */
return nil
},
}
}
func (p *Pipe[T]) OnErr(handler func(error) error) *Pipe[T] {
p.onErr = handler
return p
}
type runCfg struct {
permittedErrs []error
}
func (p *Pipe[T]) Next(fn actionFunc[T], opts ...func(opts *runCfg)) *Pipe[T] {
cfg := runCfg{}
for _, opt := range opts {
opt(&cfg)
}
p.actions = append(p.actions, action[T]{fn: fn, cfg: cfg})
return p
}
func (p *Pipe[T]) Do() (T, error) {
var t T
for _, initFn := range p.initFns {
initFn(&t)
}
outer:
for _, action := range p.actions {
if err := action.fn(&t); err != nil {
for _, permittedErr := range action.cfg.permittedErrs {
if errors.Is(err, permittedErr) {
continue outer
}
}
// stop the chain prematurely
err = p.onErr(err)
return t, err
}
}
p.onErr(nil)
return t, nil
}
func PermitErrors(errs ...error) func(opts *runCfg) {
return func(opts *runCfg) {
opts.permittedErrs = append(opts.permittedErrs, errs...)
}
}