-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstatement.go
32 lines (26 loc) · 864 Bytes
/
statement.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
package pipeline
import "context"
type (
// Statement is a structure that can be evaluated to yield a boolean result
Statement[T any] struct {
label string
fn func(context.Context, T) bool
}
)
// NewStatement creates a statement represented by the given name, that will evaluate to the given evaluation
func NewStatement[T any](name string, eval func(context.Context, T) bool) Statement[T] {
return Statement[T]{
label: name,
fn: eval,
}
}
// NewAnonymousStatement creates an anonymous statement with no representation, that will evaluate to the given evaluation
func NewAnonymousStatement[T any](eval func(context.Context, T) bool) Statement[T] {
return NewStatement("", eval)
}
func (s Statement[T]) Name() string {
return s.label
}
func (s Statement[T]) Evaluate(ctx context.Context, v T) bool {
return s.fn != nil && s.fn(ctx, v)
}