Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add map error pipeline stage #8

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions etl/pipeline/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ func Map[T, V any](mapFunc func(context.Context, *PipelineState, T) V) stage[T,
}
}

func MapError[T, V any](mapFunc func(context.Context, *PipelineState, T) (V, error)) stage[T, V] {
return func(ctx context.Context, p *PipelineState, inChannel chan T) (chan V, error) {
// create channel with buffer for each element in inChannel
mapChan := make(chan V, len(inChannel))

// map values from inChannel
for inValue := range inChannel {
value, err := mapFunc(ctx, p, inValue)
if err != nil {
return nil, err
}
mapChan <- value
}
close(mapChan)

return mapChan, nil
}
}

func Filter[T any](filterFunc func(context.Context, *PipelineState, T) bool) stage[T, T] {
return func(ctx context.Context, p *PipelineState, inChannel chan T) (chan T, error) {
// optimistically allocate buffer for all elements in input channel
Expand Down