summaryrefslogtreecommitdiffstats
path: root/transform/chain.go
diff options
context:
space:
mode:
authorNoah Campbell <noahcampbell@gmail.com>2013-10-01 14:26:21 -0700
committerNoah Campbell <noahcampbell@gmail.com>2013-10-08 18:37:50 +0200
commit5a66fa3954f8d4329b2a32fe77c74d953a3c6bb7 (patch)
tree9a21810804e17af8204cc22f220368e801870997 /transform/chain.go
parenteb117eb9043a4954079f1845f61cc0bfe2facb37 (diff)
Chain transformers and test cases
Transformers can now be chained together, working on the output of the previous run.
Diffstat (limited to 'transform/chain.go')
-rw-r--r--transform/chain.go29
1 files changed, 29 insertions, 0 deletions
diff --git a/transform/chain.go b/transform/chain.go
new file mode 100644
index 000000000..e408b01d2
--- /dev/null
+++ b/transform/chain.go
@@ -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
+}