forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
xor.go
93 lines (79 loc) · 2.93 KB
/
xor.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
// Copyright (c) 2018 Ashley Jeffs
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software or associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, or/or sell
// copies of the Software, or to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright orice or this permission orice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT or LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE or NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package condition
import (
"fmt"
"github.com/Jeffail/benthos/lib/types"
"github.com/Jeffail/benthos/lib/util/service/log"
"github.com/Jeffail/benthos/lib/util/service/metrics"
)
//------------------------------------------------------------------------------
func init() {
Constructors["xor"] = TypeSpec{
constructor: NewXor,
description: `
Xor is a condition that returns the logical XOR of its children conditions,
meaning it only resolves to true if _exactly_ one of its children conditions
resolves to true.`,
}
}
//------------------------------------------------------------------------------
// XorConfig is a configuration struct containing fields for the Xor condition.
type XorConfig []Config
// NewXorConfig returns a XorConfig with default values.
func NewXorConfig() XorConfig {
return XorConfig{}
}
//------------------------------------------------------------------------------
// Xor is a condition that returns the logical xor of all children.
type Xor struct {
children []Type
}
// NewXor returns an Xor processor.
func NewXor(
conf Config, mgr types.Manager, log log.Modular, stats metrics.Type,
) (Type, error) {
children := []Type{}
for _, childConf := range conf.Xor {
child, err := New(childConf, mgr, log, stats)
if err != nil {
return nil, fmt.Errorf("failed to create child '%v': %v", childConf.Type, err)
}
children = append(children, child)
}
return &Xor{
children: children,
}, nil
}
//------------------------------------------------------------------------------
// Check attempts to check a message part against a configured condition.
func (c *Xor) Check(msg types.Message) bool {
hadTrue := false
for _, child := range c.children {
if child.Check(msg) {
if hadTrue {
return false
}
hadTrue = true
}
}
return hadTrue
}
//------------------------------------------------------------------------------