-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
66 lines (51 loc) · 1.48 KB
/
filter.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package msgprocessor
import (
"errors"
ab "github.com/hyperledger/fabric-protos-go/common"
)
// ErrEmptyMessage is returned by the empty message filter on rejection.
var ErrEmptyMessage = errors.New("Message was empty")
// Rule defines a filter function which accepts, rejects, or forwards (to the next rule) an Envelope
type Rule interface {
// Apply applies the rule to the given Envelope, either successfully or returns error
Apply(message *ab.Envelope) error
}
// EmptyRejectRule rejects empty messages
var EmptyRejectRule = Rule(emptyRejectRule{})
type emptyRejectRule struct{}
func (a emptyRejectRule) Apply(message *ab.Envelope) error {
if message.Payload == nil {
return ErrEmptyMessage
}
return nil
}
// AcceptRule always returns Accept as a result for Apply
var AcceptRule = Rule(acceptRule{})
type acceptRule struct{}
func (a acceptRule) Apply(message *ab.Envelope) error {
return nil
}
// RuleSet is used to apply a collection of rules
type RuleSet struct {
rules []Rule
}
// NewRuleSet creates a new RuleSet with the given ordered list of Rules
func NewRuleSet(rules []Rule) *RuleSet {
return &RuleSet{
rules: rules,
}
}
// Apply applies the rules given for this set in order, returning nil on valid or err on invalid
func (rs *RuleSet) Apply(message *ab.Envelope) error {
for _, rule := range rs.rules {
err := rule.Apply(message)
if err != nil {
return err
}
}
return nil
}