Chain transformers and test cases

Transformers can now be chained together, working on the output of the
previous run.
This commit is contained in:
Noah Campbell
2013-10-01 14:26:21 -07:00
parent eb117eb904
commit 5a66fa3954
4 changed files with 86 additions and 10 deletions

29
transform/chain.go Normal file
View File

@@ -0,0 +1,29 @@
package transform
import (
"io"
"bytes"
)
type chain struct {
transformers []Transformer
}
func NewChain(trs ...Transformer) Transformer {
return &chain{transformers: trs}
}
func (c *chain) Apply(r io.Reader, w io.Writer) (err error) {
in := r
for _, tr := range c.transformers {
out := new(bytes.Buffer)
err = tr.Apply(in, out)
if err != nil {
return
}
in = bytes.NewBuffer(out.Bytes())
}
_, err = io.Copy(w, in)
return
}