forked from hashicorp/go-eventlogger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
53 lines (43 loc) · 1.27 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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package eventlogger
import (
"context"
)
// Predicate is a func that returns true if we want to keep the Event.
type Predicate func(e *Event) (bool, error)
// Filter is a Node that's used for filtering out events from the Pipeline.
type Filter struct {
// Predicate is a func that returns true if we want to keep the Event.
Predicate Predicate
name string
}
var _ Node = &Filter{}
// Process will call the Filter's Predicate func to determine whether to return
// the Event or filter it out of the Pipeline (Filtered Events return nil, nil,
// which is a successful response).
func (f *Filter) Process(ctx context.Context, e *Event) (*Event, error) {
// Use the predicate to see if we want to keep the event.
keep, err := f.Predicate(e)
if err != nil {
return nil, err
}
if !keep {
// Return nil to signal that the event should be discarded.
return nil, nil
}
// return the event
return e, nil
}
// Reopen is a no op for Filters.
func (f *Filter) Reopen() error {
return nil
}
// Type describes the type of the node as a Filter.
func (f *Filter) Type() NodeType {
return NodeTypeFilter
}
// Name returns a representation of the Filter's name
func (f *Filter) Name() string {
return f.name
}