forked from benthosdev/benthos
/
static.go
56 lines (45 loc) · 1.32 KB
/
static.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
package condition
import (
"github.com/Jeffail/benthos/v3/lib/log"
"github.com/Jeffail/benthos/v3/lib/metrics"
"github.com/Jeffail/benthos/v3/lib/types"
)
//------------------------------------------------------------------------------
func init() {
Constructors[TypeStatic] = TypeSpec{
constructor: NewStatic,
Summary: `
Always resolves to the same static boolean value.`,
}
}
//------------------------------------------------------------------------------
// Static is a condition that always returns a static boolean value.
type Static struct {
value bool
mCount metrics.StatCounter
mTrue metrics.StatCounter
mFalse metrics.StatCounter
}
// NewStatic returns a Static condition.
func NewStatic(
conf Config, mgr types.Manager, log log.Modular, stats metrics.Type,
) (Type, error) {
return &Static{
value: conf.Static,
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 (s *Static) Check(msg types.Message) bool {
s.mCount.Incr(1)
if s.value {
s.mTrue.Incr(1)
} else {
s.mFalse.Incr(1)
}
return s.value
}
//------------------------------------------------------------------------------