aboutsummaryrefslogtreecommitdiff
path: root/core/operator/map.go
diff options
context:
space:
mode:
Diffstat (limited to 'core/operator/map.go')
-rw-r--r--core/operator/map.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/core/operator/map.go b/core/operator/map.go
new file mode 100644
index 0000000..2165c25
--- /dev/null
+++ b/core/operator/map.go
@@ -0,0 +1,53 @@
1package operator
2
3import (
4 "context"
5 "flux/core"
6 "fmt"
7)
8
9// MapFunc is business logic for user implement
10type MapFunc[IN, OUT any] func(in IN) (OUT, error)
11
12type MapOperator[IN, OUT any] struct {
13 userFunc MapFunc[IN, OUT]
14}
15
16func NewMapOperator[IN, OUT any](fn MapFunc[IN, OUT]) *MapOperator[IN, OUT] {
17 return &MapOperator[IN, OUT]{
18 userFunc: fn,
19 }
20}
21
22func (fo *MapOperator[IN, OUT]) Open(ctx context.Context) error {
23 return nil
24}
25
26func (fo *MapOperator[IN, OUT]) Process(ctx context.Context, record core.StreamRecord[IN], out Emitter[OUT]) error {
27 if record.Type != core.RecordData {
28 // watermark and barrier
29 return out.Emit(ctx, core.StreamRecord[OUT]{
30 Type: record.Type,
31 Key: record.Key,
32 Timestamp: record.Timestamp,
33 BarrierID: record.BarrierID,
34 })
35 }
36
37 outValue, err := fo.userFunc(record.Value)
38
39 if err != nil {
40 return fmt.Errorf("cannot process value %v: %w", record.Value, err)
41 }
42
43 return out.Emit(ctx, core.StreamRecord[OUT]{
44 Type: core.RecordData,
45 Key: record.Key,
46 Timestamp: record.Timestamp,
47 Value: outValue,
48 })
49}
50
51func (fo *MapOperator[IN, OUT]) Close(ctx context.Context) error {
52 return nil
53}