forked from benthosdev/benthos
/
all.go
136 lines (114 loc) · 3.25 KB
/
all.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package condition
import (
"encoding/json"
"github.com/dafanshu/benthos/v3/lib/log"
"github.com/dafanshu/benthos/v3/lib/message"
"github.com/dafanshu/benthos/v3/lib/metrics"
"github.com/dafanshu/benthos/v3/lib/types"
)
//------------------------------------------------------------------------------
func init() {
Constructors[TypeAll] = TypeSpec{
constructor: NewAll,
Summary: `
A condition that tests a child condition against each message of a batch
individually. If all messages pass the child condition then this condition also
passes.`,
Description: `
For example, if we wanted to check that all messages of a batch contain the word
'foo' we could use this config:
` + "``` yaml" + `
all:
text:
operator: contains
arg: foo
` + "```" + ``,
}
}
//------------------------------------------------------------------------------
// AllConfig is a configuration struct containing fields for the All condition.
type AllConfig struct {
*Config `yaml:",inline" json:",inline"`
}
// NewAllConfig returns a AllConfig with default values.
func NewAllConfig() AllConfig {
return AllConfig{
Config: nil,
}
}
// MarshalJSON prints an empty object instead of nil.
func (m AllConfig) MarshalJSON() ([]byte, error) {
if m.Config != nil {
return json.Marshal(m.Config)
}
return json.Marshal(struct{}{})
}
// MarshalYAML prints an empty object instead of nil.
func (m AllConfig) MarshalYAML() (interface{}, error) {
if m.Config != nil {
return *m.Config, nil
}
return struct{}{}, nil
}
// UnmarshalJSON ensures that when parsing child config it is initialised.
func (m *AllConfig) UnmarshalJSON(bytes []byte) error {
if m.Config == nil {
nConf := NewConfig()
m.Config = &nConf
}
return json.Unmarshal(bytes, m.Config)
}
// UnmarshalYAML ensures that when parsing child config it is initialised.
func (m *AllConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
if m.Config == nil {
nConf := NewConfig()
m.Config = &nConf
}
return unmarshal(m.Config)
}
//------------------------------------------------------------------------------
// All is a condition that returns the logical or of all children.
type All struct {
child Type
mCount metrics.StatCounter
mTrue metrics.StatCounter
mFalse metrics.StatCounter
}
// NewAll returns an All condition.
func NewAll(
conf Config, mgr types.Manager, log log.Modular, stats metrics.Type,
) (Type, error) {
childConf := conf.All.Config
if childConf == nil {
newConf := NewConfig()
childConf = &newConf
}
child, err := New(*childConf, mgr, log.NewModule(".all"), metrics.Namespaced(stats, "all"))
if err != nil {
return nil, err
}
return &All{
child: child,
mCount: stats.GetCounter("count"),
mTrue: stats.GetCounter("true"),
mFalse: stats.GetCounter("false"),
}, nil
}
//------------------------------------------------------------------------------
// Check attempts to check a message part against a configured condition.
func (c *All) Check(msg types.Message) bool {
c.mCount.Incr(1)
if msg.Len() > 0 {
for i := 0; i < msg.Len(); i++ {
if !c.child.Check(message.Lock(msg, i)) {
c.mFalse.Incr(1)
return false
}
}
c.mTrue.Incr(1)
return true
}
c.mFalse.Incr(1)
return false
}
//------------------------------------------------------------------------------