forked from compose/transporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopfilter.go
46 lines (39 loc) · 930 Bytes
/
opfilter.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
package opfilter
import (
"strings"
"github.com/compose/transporter/function"
"github.com/compose/transporter/message"
"github.com/compose/transporter/message/ops"
)
var (
_ function.Function = &opfilter{}
)
func init() {
function.Add(
"opfilter",
func() function.Function {
return &opfilter{}
},
)
}
// opfilter will skipped messages based on the defined filter.
type opfilter struct {
Whitelist []string `json:"whitelist"`
Blacklist []string `json:"blacklist"`
}
func (o *opfilter) Apply(msg message.Msg) (message.Msg, error) {
if len(o.Whitelist) > 0 && !isOpInList(msg.OP(), o.Whitelist) {
return nil, nil
} else if len(o.Blacklist) > 0 && isOpInList(msg.OP(), o.Blacklist) {
return nil, nil
}
return msg, nil
}
func isOpInList(op ops.Op, list []string) bool {
for _, listedOp := range list {
if ops.OpTypeFromString(strings.ToLower(listedOp)) == op {
return true
}
}
return false
}