diff options
Diffstat (limited to 'core/operator/map.go')
| -rw-r--r-- | core/operator/map.go | 53 |
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 @@ | |||
| 1 | package operator | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "context" | ||
| 5 | "flux/core" | ||
| 6 | "fmt" | ||
| 7 | ) | ||
| 8 | |||
| 9 | // MapFunc is business logic for user implement | ||
| 10 | type MapFunc[IN, OUT any] func(in IN) (OUT, error) | ||
| 11 | |||
| 12 | type MapOperator[IN, OUT any] struct { | ||
| 13 | userFunc MapFunc[IN, OUT] | ||
| 14 | } | ||
| 15 | |||
| 16 | func NewMapOperator[IN, OUT any](fn MapFunc[IN, OUT]) *MapOperator[IN, OUT] { | ||
| 17 | return &MapOperator[IN, OUT]{ | ||
| 18 | userFunc: fn, | ||
| 19 | } | ||
| 20 | } | ||
| 21 | |||
| 22 | func (fo *MapOperator[IN, OUT]) Open(ctx context.Context) error { | ||
| 23 | return nil | ||
| 24 | } | ||
| 25 | |||
| 26 | func (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 | |||
| 51 | func (fo *MapOperator[IN, OUT]) Close(ctx context.Context) error { | ||
| 52 | return nil | ||
| 53 | } | ||
