-
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.
Merge pull request #8 from deliveryhero/NCR-14625-apply-for-multitoken
NCR-14625 add Apply
- Loading branch information
Showing
23 changed files
with
280 additions
and
44 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
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 |
---|---|---|
@@ -1 +1 @@ | ||
* mark.salpeter@deliveryhero.com | ||
* mark.salpeter@deliveryhero.com |
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
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,40 @@ | ||
package pipeline | ||
|
||
import "context" | ||
|
||
type apply[A, B, C any] struct { | ||
a Processor[A, []B] | ||
b Processor[B, C] | ||
} | ||
|
||
func (j *apply[A, B, C]) Process(ctx context.Context, a A) ([]C, error) { | ||
bs, err := j.a.Process(ctx, a) | ||
if err != nil { | ||
j.a.Cancel(a, err) | ||
return []C{}, err | ||
} | ||
|
||
cs := make([]C, 0, len(bs)) | ||
|
||
for i := range bs { | ||
c, err := j.b.Process(ctx, bs[i]) | ||
if err != nil { | ||
j.b.Cancel(bs[i], err) | ||
return cs, err | ||
} | ||
|
||
cs = append(cs, c) | ||
} | ||
|
||
return cs, nil | ||
} | ||
|
||
func (j *apply[A, B, C]) Cancel(_ A, _ error) {} | ||
|
||
// Apply connects two processes, applying the second to each item of the first output | ||
func Apply[A, B, C any]( | ||
a Processor[A, []B], | ||
b Processor[B, C], | ||
) Processor[A, []C] { | ||
return &apply[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,47 @@ | ||
package pipeline_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/deliveryhero/pipeline/v2" | ||
) | ||
|
||
func ExampleApply() { | ||
transform := pipeline.NewProcessor(func(_ context.Context, s string) ([]string, error) { | ||
return strings.Split(s, ","), nil | ||
}, nil) | ||
|
||
double := pipeline.NewProcessor(func(_ context.Context, s string) (string, error) { | ||
return s + s, nil | ||
}, nil) | ||
|
||
addLeadingZero := pipeline.NewProcessor(func(_ context.Context, s string) (string, error) { | ||
return "0" + s, nil | ||
}, nil) | ||
|
||
apply := pipeline.Apply( | ||
transform, | ||
pipeline.Sequence( | ||
double, | ||
addLeadingZero, | ||
double, | ||
), | ||
) | ||
|
||
input := "1,2,3,4,5" | ||
|
||
for out := range pipeline.Process(context.Background(), apply, pipeline.Emit(input)) { | ||
for j := range out { | ||
fmt.Printf("process: %s\n", out[j]) | ||
} | ||
} | ||
|
||
// Output: | ||
// process: 011011 | ||
// process: 022022 | ||
// process: 033033 | ||
// process: 044044 | ||
// process: 055055 | ||
} |
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,59 @@ | ||
package pipeline | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestLoopApply(t *testing.T) { | ||
t.Parallel() | ||
|
||
transform := NewProcessor(func(_ context.Context, s string) ([]string, error) { | ||
return strings.Split(s, ","), nil | ||
}, nil) | ||
|
||
double := NewProcessor(func(_ context.Context, s string) (string, error) { | ||
return s + s, nil | ||
}, nil) | ||
|
||
addLeadingZero := NewProcessor(func(_ context.Context, s string) (string, error) { | ||
return "0" + s, nil | ||
}, nil) | ||
|
||
looper := Apply( | ||
transform, | ||
Sequence( | ||
double, | ||
addLeadingZero, | ||
double, | ||
), | ||
) | ||
|
||
gotCount := 0 | ||
input := "1,2,3,4,5" | ||
want := []string{"011011", "022022", "033033", "044044", "055055"} | ||
|
||
for out := range Process(context.Background(), looper, Emit(input)) { | ||
for j := range out { | ||
gotCount++ | ||
if !contains(want, out[j]) { | ||
t.Errorf("does not contains got=%v, want=%v", out[j], want) | ||
} | ||
} | ||
} | ||
|
||
if gotCount != len(want) { | ||
t.Errorf("total results got=%v, want=%v", gotCount, len(want)) | ||
} | ||
} | ||
|
||
func contains(s []string, e string) bool { | ||
for i := range s { | ||
if s[i] == e { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
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
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
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
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
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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
module github.com/deliveryhero/pipeline/v2 | ||
|
||
go 1.18 | ||
|
||
require github.com/deliveryhero/pipeline v0.4.0 |
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 |
---|---|---|
@@ -1,2 +0,0 @@ | ||
github.com/deliveryhero/pipeline v0.4.0 h1:rQ6qHTApvVFouP9Y02k53KoFX+myuO6/OAxVX34iXvo= | ||
github.com/deliveryhero/pipeline v0.4.0/go.mod h1:78CQfQT2DONSGPktr7X71xu333ZMPdrcYuV/gY/Mnkg= | ||
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
Oops, something went wrong.