-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(join): joins two processes together where the output of one is t…
…he input of the other
- Loading branch information
1 parent
4863fba
commit 639c471
Showing
3 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package pipeline | ||
|
||
import "context" | ||
|
||
type join[A, B, C any] struct { | ||
a Processor[A, B] | ||
b Processor[B, C] | ||
} | ||
|
||
func (j *join[A, B, C]) Process(ctx context.Context, a A) (C, error) { | ||
var zero C | ||
if b, err := j.a.Process(ctx, a); err != nil { | ||
j.a.Cancel(a, err) | ||
return zero, err | ||
} else if c, err := j.b.Process(ctx, b); err != nil { | ||
j.b.Cancel(b, err) | ||
return zero, err | ||
} else { | ||
return c, nil | ||
} | ||
} | ||
|
||
func (j *join[A, B, C]) Cancel(_ A, _ error) {} | ||
|
||
// Join connects two processes where the output of the first is the input of the second | ||
func Join[A, B, C any](a Processor[A, B], b Processor[B, C]) Processor[A, C] { | ||
return &join[A, B, C]{a, b} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package pipeline | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strconv" | ||
"testing" | ||
) | ||
|
||
func TestJoin(t *testing.T) { | ||
// Emit 10 numbers | ||
want := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} | ||
ins := Emit(want...) | ||
// Join two steps, one that converts the number to a string, the other that converts it back to a number | ||
join := Join(NewProcessor(func(_ context.Context, i int) (string, error) { | ||
return strconv.Itoa(i), nil | ||
}, nil), NewProcessor(func(_ context.Context, i string) (int, error) { | ||
return strconv.Atoi(i) | ||
}, nil)) | ||
// Compare inputs and outputs | ||
var idx int | ||
for got := range Process(context.Background(), join, ins) { | ||
if want[idx] != got { | ||
t.Fatalf("[%d] = %d, want = %d", idx, got, want[idx]) | ||
} | ||
idx++ | ||
} | ||
} | ||
|
||
func JoinExample() { | ||
// Emit 10 numbers | ||
inputs := Emit(0, 1, 2, 3, 4, 5) | ||
// Join two steps, one that converts the number to a string, the other that converts it back to a number | ||
convertToStringThenBackToInt := Process(context.Background(), Join(NewProcessor(func(_ context.Context, i int) (string, error) { | ||
return strconv.Itoa(i), nil | ||
}, nil), NewProcessor(func(_ context.Context, i string) (int, error) { | ||
return strconv.Atoi(i) | ||
}, nil)), inputs) | ||
// Print the output | ||
for o := range convertToStringThenBackToInt { | ||
fmt.Println(o) | ||
} | ||
// Output | ||
// 0 | ||
// 1 | ||
// 2 | ||
// 3 | ||
// 4 | ||
// 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters