-
Notifications
You must be signed in to change notification settings - Fork 2
/
context.go
53 lines (45 loc) · 1.58 KB
/
context.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
package kstreams
import (
"context"
"fmt"
)
type ProcessorContext[Kout any, Vout any] interface {
// Forward to all child nodes.
Forward(ctx context.Context, k Kout, v Vout)
// Forward to specific child node. Panics if child node is not found.
ForwardTo(ctx context.Context, k Kout, v Vout, childName string) // TBD: should forward return error ? or are errs...ignored?
// Get state store by name. Returns nil if not found.
GetStore(name string) Store
}
func NewInternalkProcessorContext[Kout any, Vout any](
outputs map[string]InputProcessor[Kout, Vout],
stores map[string]Store,
) *InternalProcessorContext[Kout, Vout] {
return &InternalProcessorContext[Kout, Vout]{}
}
type InternalProcessorContext[Kout any, Vout any] struct {
outputs map[string]InputProcessor[Kout, Vout]
stores map[string]Store
outputErrors []error
}
func (c *InternalProcessorContext[Kout, Vout]) drainErrors() []error {
res := c.outputErrors
return res
}
func (c *InternalProcessorContext[Kout, Vout]) Forward(ctx context.Context, k Kout, v Vout) {
for name, p := range c.outputs {
if err := p.Process(ctx, k, v); err != nil {
c.outputErrors = append(c.outputErrors, fmt.Errorf("failed to forward record to node %s: %w", name, err))
}
}
}
func (c *InternalProcessorContext[Kout, Vout]) ForwardTo(ctx context.Context, k Kout, v Vout, childName string) {
if p, ok := c.outputs[childName]; ok {
if err := p.Process(ctx, k, v); err != nil {
c.outputErrors = append(c.outputErrors, err)
}
}
}
func (c *InternalProcessorContext[Kout, Vout]) GetStore(name string) Store {
return c.stores[name]
}