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