1
1
/*
2
- Copyright IBM Corp. 2016 All Rights Reserved.
2
+ Copyright IBM Corp. All Rights Reserved.
3
3
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- http://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
4
+ SPDX-License-Identifier: Apache-2.0
15
5
*/
16
6
17
7
package filter
18
8
19
9
import (
20
- "fmt "
10
+ "errors "
21
11
22
12
ab "github.com/hyperledger/fabric/protos/common"
23
13
)
24
14
25
- // Action is used to express the output of a rule
26
- type Action int
27
-
28
- const (
29
- // Accept indicates that the message should be processed
30
- Accept = iota
31
- // Reject indicates that the message should not be processed
32
- Reject
33
- // Forward indicates that the rule could not determine the correct course of action
34
- Forward
35
- )
15
+ // ErrEmptyMessage is returned by the empty message filter on rejection.
16
+ var ErrEmptyMessage = errors .New ("Message was empty" )
36
17
37
18
// Rule defines a filter function which accepts, rejects, or forwards (to the next rule) an Envelope
38
19
type Rule interface {
39
- // Apply applies the rule to the given Envelope, replying with the Action to take for the message
40
- Apply (message * ab.Envelope ) Action
20
+ // Apply applies the rule to the given Envelope, either successfully or returns error
21
+ Apply (message * ab.Envelope ) error
41
22
}
42
23
43
24
// EmptyRejectRule rejects empty messages
44
25
var EmptyRejectRule = Rule (emptyRejectRule {})
45
26
46
27
type emptyRejectRule struct {}
47
28
48
- func (a emptyRejectRule ) Apply (message * ab.Envelope ) Action {
29
+ func (a emptyRejectRule ) Apply (message * ab.Envelope ) error {
49
30
if message .Payload == nil {
50
- return Reject
31
+ return ErrEmptyMessage
51
32
}
52
- return Forward
33
+ return nil
53
34
}
54
35
55
36
// AcceptRule always returns Accept as a result for Apply
56
37
var AcceptRule = Rule (acceptRule {})
57
38
58
39
type acceptRule struct {}
59
40
60
- func (a acceptRule ) Apply (message * ab.Envelope ) Action {
61
- return Accept
41
+ func (a acceptRule ) Apply (message * ab.Envelope ) error {
42
+ return nil
62
43
}
63
44
64
45
// RuleSet is used to apply a collection of rules
@@ -76,14 +57,10 @@ func NewRuleSet(rules []Rule) *RuleSet {
76
57
// Apply applies the rules given for this set in order, returning nil on valid or err on invalid
77
58
func (rs * RuleSet ) Apply (message * ab.Envelope ) error {
78
59
for _ , rule := range rs .rules {
79
- action := rule .Apply (message )
80
- switch action {
81
- case Accept :
82
- return nil
83
- case Reject :
84
- return fmt .Errorf ("Rejected by rule: %T" , rule )
85
- default :
60
+ err := rule .Apply (message )
61
+ if err != nil {
62
+ return err
86
63
}
87
64
}
88
- return fmt . Errorf ( "No matching filter found" )
65
+ return nil
89
66
}
0 commit comments