-
Notifications
You must be signed in to change notification settings - Fork 0
/
may_invoker.go
249 lines (207 loc) · 7.66 KB
/
may_invoker.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package fo
// MayInvoker is a helper instance that enables to invoke a
// call to a callback function and then filters out the error
// from the result and only returns the value. If the error
// is not nil, it will be collected to mayHandlers and then
// handled by handlers registered by Use(...)
type MayInvoker[T any] struct {
*mayHandlers
}
// NewMay creates a helper instance that enables to invoke a
// call to a callback function and then filters out the error
// from the result and only returns the value. If the error
// is not nil, it will be collected to mayHandlers and then
// handled by handlers registered by Use(...)
func NewMay[T any]() *MayInvoker[T] {
return &MayInvoker[T]{
mayHandlers: newMayHandlers(),
}
}
// NewMay1 is an alias of NewMay.
func NewMay1[T any]() *MayInvoker[T] {
return NewMay[T]()
}
// Use registers the handlers.
func (f *MayInvoker[T]) Use(handler ...MayHandler) *MayInvoker[T] {
f.mayHandlers.Use(handler...)
return f
}
// Invoke invokes the callback function and filters out the error
// from the result and only returns the value. If the error is not
// nil, it will be collected and handled by handlers registered by
// Use(...)
func (f *MayInvoker[T]) Invoke(t1 T, err any, messageArgs ...any) T {
if err == nil {
return t1
}
f.mayHandlers.handleError(err, messageArgs...)
return t1
}
// MayInvoker0 is a helper instance that behaves like MayInvoker
// , but it allows to invoke a callback function that returns no
// value.
type MayInvoker0 struct {
*mayHandlers
}
// NewMay0 creates a helper instance behaves like MayInvoker, but
// it allows to invoke a callback function that returns no value.
func NewMay0() *MayInvoker0 {
return &MayInvoker0{
mayHandlers: newMayHandlers(),
}
}
// Use registers the handlers.
func (f *MayInvoker0) Use(handler ...MayHandler) *MayInvoker0 {
f.mayHandlers.Use(handler...)
return f
}
// Invoke invokes the callback function and filters out the error
// from the result and only returns the value. If the error is not
// nil, it will be collected and handled by handlers registered by
// Use(...)
func (f *MayInvoker0) Invoke(anyErr any, messageArgs ...any) {
if anyErr == nil {
return
}
f.mayHandlers.handleError(anyErr, messageArgs...)
}
// MayInvoker2 is a helper instance behaves like MayInvoker, but
// it allows to invoke a callback function that returns 2 values.
type MayInvoker2[T1 any, T2 any] struct {
*mayHandlers
}
// NewMay2 creates a helper instance behaves like MayInvoker, but
// it allows to invoke a callback function that returns 2 values.
func NewMay2[T1 any, T2 any]() *MayInvoker2[T1, T2] {
return &MayInvoker2[T1, T2]{
mayHandlers: newMayHandlers(),
}
}
// Use registers the handlers.
func (f *MayInvoker2[T1, T2]) Use(handler ...MayHandler) *MayInvoker2[T1, T2] {
f.mayHandlers.Use(handler...)
return f
}
// Invoke invokes the callback function and filters out the error
// from the result and only returns the value. If the error is not
// nil, it will be collected and handled by handlers registered by
// Use(...)
func (f *MayInvoker2[T1, T2]) Invoke(t1 T1, t2 T2, err any, messageArgs ...any) (T1, T2) {
if err == nil {
return t1, t2
}
f.mayHandlers.handleError(err, messageArgs...)
return t1, t2
}
// MayInvoker3 is a helper instance behaves like MayInvoker, but
// it allows to invoke a callback function that returns 3 values.
type MayInvoker3[T1 any, T2 any, T3 any] struct {
*mayHandlers
}
// NewMay3 creates a helper instance behaves like MayInvoker, but
// it allows to invoke a callback function that returns 3 values.
func NewMay3[T1 any, T2 any, T3 any]() *MayInvoker3[T1, T2, T3] {
return &MayInvoker3[T1, T2, T3]{
mayHandlers: newMayHandlers(),
}
}
// Use registers the handlers.
func (f *MayInvoker3[T1, T2, T3]) Use(handler ...MayHandler) *MayInvoker3[T1, T2, T3] {
f.mayHandlers.Use(handler...)
return f
}
// Invoke invokes the callback function and filters out the error
// from the result and only returns the value. If the error is not
// nil, it will be collected and handled by handlers registered by
// Use(...)
func (f *MayInvoker3[T1, T2, T3]) Invoke(t1 T1, t2 T2, t3 T3, err any, messageArgs ...any) (T1, T2, T3) {
if err == nil {
return t1, t2, t3
}
f.mayHandlers.handleError(err, messageArgs...)
return t1, t2, t3
}
// MayInvoker4 is a helper instance behaves like MayInvoker, but
// it allows to invoke a callback function that returns 4 values.
type MayInvoker4[T1 any, T2 any, T3 any, T4 any] struct {
*mayHandlers
}
// NewMay4 creates a helper instance behaves like MayInvoker, but
// it allows to invoke a callback function that returns 4 values.
func NewMay4[T1 any, T2 any, T3 any, T4 any]() *MayInvoker4[T1, T2, T3, T4] {
return &MayInvoker4[T1, T2, T3, T4]{
mayHandlers: newMayHandlers(),
}
}
// Use registers the handlers.
func (f *MayInvoker4[T1, T2, T3, T4]) Use(handler ...MayHandler) *MayInvoker4[T1, T2, T3, T4] {
f.mayHandlers.Use(handler...)
return f
}
// Invoke invokes the callback function and filters out the error
// from the result and only returns the value. If the error is not
// nil, it will be collected and handled by handlers registered by
// Use(...)
func (f *MayInvoker4[T1, T2, T3, T4]) Invoke(t1 T1, t2 T2, t3 T3, t4 T4, err any, messageArgs ...any) (T1, T2, T3, T4) {
if err == nil {
return t1, t2, t3, t4
}
f.mayHandlers.handleError(err, messageArgs...)
return t1, t2, t3, t4
}
// MayInvoker5 is a helper instance behaves like MayInvoker, but
// it allows to invoke a callback function that returns 5 values.
type MayInvoker5[T1 any, T2 any, T3 any, T4 any, T5 any] struct {
*mayHandlers
}
// NewMay5 creates a helper instance behaves like MayInvoker, but
// it allows to invoke a callback function that returns 5 values.
func NewMay5[T1 any, T2 any, T3 any, T4 any, T5 any]() *MayInvoker5[T1, T2, T3, T4, T5] {
return &MayInvoker5[T1, T2, T3, T4, T5]{
mayHandlers: newMayHandlers(),
}
}
// Use registers the handlers.
func (f *MayInvoker5[T1, T2, T3, T4, T5]) Use(handler ...MayHandler) *MayInvoker5[T1, T2, T3, T4, T5] {
f.mayHandlers.Use(handler...)
return f
}
// Invoke invokes the callback function and filters out the error
// from the result and only returns the value. If the error is not
// nil, it will be collected and handled by handlers registered by
// Use(...)
func (f *MayInvoker5[T1, T2, T3, T4, T5]) Invoke(t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, err any, messageArgs ...any) (T1, T2, T3, T4, T5) {
if err == nil {
return t1, t2, t3, t4, t5
}
f.mayHandlers.handleError(err, messageArgs...)
return t1, t2, t3, t4, t5
}
// MayInvoker6 is a helper instance behaves like MayInvoker, but
// it allows to invoke a callback function that returns 6 values.
type MayInvoker6[T1 any, T2 any, T3 any, T4 any, T5 any, T6 any] struct {
*mayHandlers
}
// NewMay6 creates a helper instance behaves like MayInvoker, but
// it allows to invoke a callback function that returns 6 values.
func NewMay6[T1 any, T2 any, T3 any, T4 any, T5 any, T6 any]() *MayInvoker6[T1, T2, T3, T4, T5, T6] {
return &MayInvoker6[T1, T2, T3, T4, T5, T6]{
mayHandlers: newMayHandlers(),
}
}
// Use registers the handlers.
func (f *MayInvoker6[T1, T2, T3, T4, T5, T6]) Use(handler ...MayHandler) *MayInvoker6[T1, T2, T3, T4, T5, T6] {
f.mayHandlers.Use(handler...)
return f
}
// Invoke invokes the callback function and filters out the error
// from the result and only returns the value. If the error is not
// nil, it will be collected and handled by handlers registered by
// Use(...)
func (f *MayInvoker6[T1, T2, T3, T4, T5, T6]) Invoke(t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, t6 T6, err any, messageArgs ...any) (T1, T2, T3, T4, T5, T6) {
if err == nil {
return t1, t2, t3, t4, t5, t6
}
f.mayHandlers.handleError(err, messageArgs...)
return t1, t2, t3, t4, t5, t6
}