-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathfilter.go
More file actions
30 lines (24 loc) · 846 Bytes
/
filter.go
File metadata and controls
30 lines (24 loc) · 846 Bytes
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
package filter
import "context"
// Filter is a generic middleware type
type Filter interface {
Do(ctx context.Context, request interface{}, service Service) (response interface{}, err error)
}
// Service is a generic client/server type
type Service interface {
Do(ctx context.Context, request interface{}) (response interface{}, err error)
}
// ServiceWithFilters applies the filters to a service in a standard way.
func ServiceWithFilters(service Service, filters ...Filter) Service {
for i := len(filters) - 1; i >= 0; i-- {
service = &filteredService{filter: filters[i], service: service}
}
return service
}
type filteredService struct {
filter Filter
service Service
}
func (fs *filteredService) Do(ctx context.Context, request interface{}) (response interface{}, err error) {
return fs.filter.Do(ctx, request, fs.service)
}