feat: add chain writer
This commit was merged in pull request #4.
This commit is contained in:
@@ -5,7 +5,7 @@ import "context"
|
|||||||
// Chain chains provided Processors.
|
// Chain chains provided Processors.
|
||||||
// When an error is returned by a Processor in chain, processing
|
// When an error is returned by a Processor in chain, processing
|
||||||
// stops and the error is retuned without running further stages.
|
// stops and the error is retuned without running further stages.
|
||||||
func Chain(processors ...Processor) Processor {
|
func ChainProcessor(processors ...Processor) Processor {
|
||||||
return func(ctx context.Context, in []string) ([]string, error) {
|
return func(ctx context.Context, in []string) ([]string, error) {
|
||||||
var err error
|
var err error
|
||||||
for _, p := range processors {
|
for _, p := range processors {
|
||||||
@@ -21,3 +21,23 @@ func Chain(processors ...Processor) Processor {
|
|||||||
return in, nil
|
return in, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type chainWriter struct {
|
||||||
|
w []Writer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *chainWriter) Write(in []string) error {
|
||||||
|
for _, w := range c.w {
|
||||||
|
if err := w.Write(in); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ChainWriter(writers ...Writer) Writer {
|
||||||
|
return &chainWriter{
|
||||||
|
w: writers,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
package script
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"slices"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestChain(t *testing.T) {
|
|
||||||
p := func(_ context.Context, in []string) ([]string, error) {
|
|
||||||
in[0] = in[0] + in[0]
|
|
||||||
return in, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
chain := Chain(p, p, p)
|
|
||||||
|
|
||||||
in := []string{"a"}
|
|
||||||
want := []string{"aaaaaaaa"}
|
|
||||||
|
|
||||||
res, err := chain(t.Context(), in)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !slices.Equal(res, want) {
|
|
||||||
t.Fatalf("slices are not equal, have: %+v, want: %+v", res, want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
49
chain_test.go
Normal file
49
chain_test.go
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
package script
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"slices"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestChainProcessor(t *testing.T) {
|
||||||
|
p := func(_ context.Context, in []string) ([]string, error) {
|
||||||
|
in[0] = in[0] + in[0]
|
||||||
|
return in, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
chain := ChainProcessor(p, p, p)
|
||||||
|
|
||||||
|
in := []string{"a"}
|
||||||
|
want := []string{"aaaaaaaa"}
|
||||||
|
|
||||||
|
res, err := chain(t.Context(), in)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !slices.Equal(res, want) {
|
||||||
|
t.Fatalf("slices are not equal, have: %+v, want: %+v", res, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestChainWriter(t *testing.T) {
|
||||||
|
w1 := NewMemWriter()
|
||||||
|
w2 := NewMemWriter()
|
||||||
|
|
||||||
|
w := ChainWriter(w1, w2)
|
||||||
|
|
||||||
|
in := []string{"a"}
|
||||||
|
|
||||||
|
if err := w.Write(in); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !slices.Equal(w1.Output()[0], in) {
|
||||||
|
t.Fatalf("w1 slices are not equal, have: %+v, want: %+v", w1.Output()[0], in)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !slices.Equal(w2.Output()[0], in) {
|
||||||
|
t.Fatalf("w2 slices are not equal, have: %+v, want: %+v", w2.Output()[0], in)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user