-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwrap.go
113 lines (90 loc) · 2.5 KB
/
wrap.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package usecase
import (
"context"
"reflect"
)
// Middleware creates decorated use case interactor.
type Middleware interface {
Wrap(interactor Interactor) Interactor
}
// ErrorCatcher is a use case middleware that collects non empty errors.
type ErrorCatcher func(ctx context.Context, input interface{}, err error)
// Wrap implements Middleware.
func (e ErrorCatcher) Wrap(u Interactor) Interactor {
return &wrappedInteractor{
Interactor: Interact(func(ctx context.Context, input, output interface{}) error {
err := u.Interact(ctx, input, output)
if err != nil {
e(ctx, input, err)
}
return err
}),
wrapped: u,
}
}
// MiddlewareFunc makes Middleware from function.
type MiddlewareFunc func(next Interactor) Interactor
// Wrap decorates use case interactor.
func (mwf MiddlewareFunc) Wrap(interactor Interactor) Interactor {
return mwf(interactor)
}
// Wrap decorates Interactor with Middlewares.
//
// Having arguments i, mw1, mw2 the order of invocation is: mw1, mw2, i, mw2, mw1.
// Middleware mw1 can find behaviors of mw2 with As, but not vice versa.
func Wrap(interactor Interactor, mw ...Middleware) Interactor {
for i := len(mw) - 1; i >= 0; i-- {
w := mw[i].Wrap(interactor)
if w != nil {
interactor = &wrappedInteractor{
Interactor: w,
wrapped: interactor,
}
}
}
return interactor
}
// As finds the first Interactor in Interactor's chain that matches target, and if so, sets
// target to that Interactor value and returns true.
//
// An Interactor matches target if the Interactor's concrete value is assignable to the value
// pointed to by target.
//
// As will panic if target is not a non-nil pointer to either a type that implements
// Interactor, or to any interface type.
func As(interactor Interactor, target interface{}) bool {
if interactor == nil {
return false
}
if target == nil {
panic("target cannot be nil")
}
val := reflect.ValueOf(target)
typ := val.Type()
if typ.Kind() != reflect.Ptr || val.IsNil() {
panic("target must be a non-nil pointer")
}
if e := typ.Elem(); e.Kind() != reflect.Interface {
panic("*target must be interface")
}
targetType := typ.Elem()
for {
wrap, isWrap := interactor.(*wrappedInteractor)
if isWrap {
interactor = wrap.Interactor
}
if reflect.TypeOf(interactor).AssignableTo(targetType) {
val.Elem().Set(reflect.ValueOf(interactor))
return true
}
if !isWrap {
break
}
interactor = wrap.wrapped
}
return false
}
type wrappedInteractor struct {
Interactor
wrapped Interactor
}