-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsource_node.go
44 lines (34 loc) · 913 Bytes
/
source_node.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
package kstreams
import (
"context"
"github.com/twmb/franz-go/pkg/kgo"
)
type RecordProcessor interface {
Process(ctx context.Context, m *kgo.Record) error
}
// SourceNode[K,V] receives kgo records, and forward these to all downstream
// processors.
type SourceNode[K any, V any] struct {
KeyDeserializer Deserializer[K]
ValueDeserializer Deserializer[V]
DownstreamProcessors []InputProcessor[K, V]
}
func (n *SourceNode[K, V]) Process(ctx context.Context, m *kgo.Record) error {
key, err := n.KeyDeserializer(m.Key)
if err != nil {
return err
}
value, err := n.ValueDeserializer(m.Value)
if err != nil {
return err
}
for _, next := range n.DownstreamProcessors {
if err := next.Process(ctx, key, value); err != nil {
return err
}
}
return nil
}
func (n *SourceNode[K, V]) AddNext(next InputProcessor[K, V]) {
n.DownstreamProcessors = append(n.DownstreamProcessors, next)
}