53 lines
971 B
Go
53 lines
971 B
Go
|
package transformer
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"io"
|
||
|
)
|
||
|
|
||
|
type writer interface {
|
||
|
Write(ctx context.Context, p []byte) (n int, err error)
|
||
|
}
|
||
|
|
||
|
type multiWriter struct {
|
||
|
writers []writer
|
||
|
}
|
||
|
|
||
|
func (t *multiWriter) Write(ctx context.Context, p []byte) (n int, err error) {
|
||
|
for _, w := range t.writers {
|
||
|
n, err = w.Write(ctx, p)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
if n != len(p) {
|
||
|
err = io.ErrShortWrite
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
return len(p), nil
|
||
|
}
|
||
|
|
||
|
func newMultiWriter(writers ...writer) writer {
|
||
|
allWriters := make([]writer, 0, len(writers))
|
||
|
for _, w := range writers {
|
||
|
if mw, ok := w.(*multiWriter); ok {
|
||
|
allWriters = append(allWriters, mw.writers...)
|
||
|
} else {
|
||
|
allWriters = append(allWriters, w)
|
||
|
}
|
||
|
}
|
||
|
return &multiWriter{allWriters}
|
||
|
}
|
||
|
|
||
|
type writerWrapper struct {
|
||
|
Writer io.Writer
|
||
|
}
|
||
|
|
||
|
func (w *writerWrapper) Write(_ context.Context, p []byte) (n int, err error) {
|
||
|
return w.Writer.Write(p)
|
||
|
}
|
||
|
|
||
|
func newWriter(w io.Writer) writer {
|
||
|
return &writerWrapper{Writer: w}
|
||
|
}
|