-
Notifications
You must be signed in to change notification settings - Fork 64
/
writer.go
61 lines (54 loc) · 1.19 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package textio
import (
"fmt"
"io"
"strings"
"time"
"github.com/brimdata/zed"
"github.com/brimdata/zed/runtime/sam/expr"
"github.com/brimdata/zed/zio/zeekio"
)
type Writer struct {
writer io.WriteCloser
flattener *expr.Flattener
}
func NewWriter(w io.WriteCloser) *Writer {
return &Writer{
writer: w,
flattener: expr.NewFlattener(zed.NewContext()),
}
}
func (w *Writer) Close() error {
return w.writer.Close()
}
func (w *Writer) Write(val zed.Value) error {
if _, ok := zed.TypeUnder(val.Type()).(*zed.TypeRecord); ok {
return w.writeRecord(val)
}
_, err := fmt.Fprintln(w.writer, zeekio.FormatValue(val))
return err
}
func (w *Writer) writeRecord(rec zed.Value) error {
rec, err := w.flattener.Flatten(rec)
if err != nil {
return err
}
var out []string
for k, f := range zed.TypeRecordOf(rec.Type()).Fields {
var s string
value := rec.DerefByColumn(k).MissingAsNull()
if f.Type == zed.TypeTime {
if value.IsNull() {
s = "-"
} else {
s = zed.DecodeTime(value.Bytes()).Time().Format(time.RFC3339Nano)
}
} else {
s = zeekio.FormatValue(value)
}
out = append(out, s)
}
s := strings.Join(out, "\t")
_, err = fmt.Fprintln(w.writer, s)
return err
}