2013-10-01 21:26:21 +00:00
|
|
|
package transform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2013-10-02 02:59:35 +00:00
|
|
|
"io"
|
2013-10-01 21:26:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type chain struct {
|
|
|
|
transformers []Transformer
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewChain(trs ...Transformer) Transformer {
|
|
|
|
return &chain{transformers: trs}
|
|
|
|
}
|
|
|
|
|
2013-10-01 21:42:08 +00:00
|
|
|
func (c *chain) Apply(w io.Writer, r io.Reader) (err error) {
|
2013-10-01 21:26:21 +00:00
|
|
|
in := r
|
|
|
|
for _, tr := range c.transformers {
|
|
|
|
out := new(bytes.Buffer)
|
2013-10-01 21:42:08 +00:00
|
|
|
err = tr.Apply(out, in)
|
2013-10-01 21:26:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
in = bytes.NewBuffer(out.Bytes())
|
|
|
|
}
|
2013-10-02 02:59:35 +00:00
|
|
|
|
2013-10-01 21:26:21 +00:00
|
|
|
_, err = io.Copy(w, in)
|
|
|
|
return
|
|
|
|
}
|