-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
output_ltsv.go
61 lines (54 loc) · 1.27 KB
/
output_ltsv.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 trdsql
import (
"bufio"
)
// LTSVWriter provides methods of the Writer interface.
type LTSVWriter struct {
writer *bufio.Writer
outNULL string
results []string
delimiter rune
needNULL bool
}
// NewLTSVWriter returns LTSVWriter.
func NewLTSVWriter(writeOpts *WriteOpts) *LTSVWriter {
w := <SVWriter{}
w.delimiter = '\t'
w.writer = bufio.NewWriter(writeOpts.OutStream)
w.needNULL = writeOpts.OutNeedNULL
w.outNULL = writeOpts.OutNULL
return w
}
// PreWrite is area preparation.
func (w *LTSVWriter) PreWrite(columns []string, types []string) error {
w.results = make([]string, len(columns))
return nil
}
// WriteRow is row write to LTSV.
func (w *LTSVWriter) WriteRow(values []any, labels []string) error {
for n, col := range values {
if n > 0 {
if _, err := w.writer.WriteRune(w.delimiter); err != nil {
return err
}
}
if _, err := w.writer.WriteString(labels[n]); err != nil {
return err
}
if err := w.writer.WriteByte(':'); err != nil {
return err
}
str := ValString(col)
if col == nil && w.needNULL {
str = w.outNULL
}
if _, err := w.writer.WriteString(str); err != nil {
return err
}
}
return w.writer.WriteByte('\n')
}
// PostWrite is flush.
func (w *LTSVWriter) PostWrite() error {
return w.writer.Flush()
}