forked from taggledevel2/ratchet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
io_writer.go
44 lines (37 loc) · 1.1 KB
/
io_writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package processors
import (
"fmt"
"io"
"github.com/dailyburn/ratchet/data"
"github.com/dailyburn/ratchet/logger"
"github.com/dailyburn/ratchet/util"
)
// IoWriter wraps any io.Writer object.
// It can be used to write data out to a File, os.Stdout, or
// any other task that can be supported via io.Writer.
type IoWriter struct {
Writer io.Writer
AddNewline bool
}
// NewIoWriter returns a new IoWriter wrapping the given io.Writer object
func NewIoWriter(writer io.Writer) *IoWriter {
return &IoWriter{Writer: writer, AddNewline: false}
}
// ProcessData writes the data
func (w *IoWriter) ProcessData(d data.JSON, outputChan chan data.JSON, killChan chan error) {
var bytesWritten int
var err error
if w.AddNewline {
bytesWritten, err = fmt.Fprintln(w.Writer, string(d))
} else {
bytesWritten, err = w.Writer.Write(d)
}
util.KillPipelineIfErr(err, killChan)
logger.Debug("IoWriter:", bytesWritten, "bytes written")
}
// Finish - see interface for documentation.
func (w *IoWriter) Finish(outputChan chan data.JSON, killChan chan error) {
}
func (w *IoWriter) String() string {
return "IoWriter"
}