-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
output_jsonl.go
42 lines (35 loc) · 920 Bytes
/
output_jsonl.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
package trdsql
import (
"encoding/json"
"github.com/iancoleman/orderedmap"
)
// JSONLWriter provides methods of the Writer interface.
type JSONLWriter struct {
writer *json.Encoder
outNULL string
needNULL bool
}
// NewJSONLWriter returns JSONLWriter.
func NewJSONLWriter(writeOpts *WriteOpts) *JSONLWriter {
w := &JSONLWriter{}
w.writer = json.NewEncoder(writeOpts.OutStream)
w.needNULL = writeOpts.OutNeedNULL
w.outNULL = writeOpts.OutNULL
return w
}
// PreWrite does nothing.
func (w *JSONLWriter) PreWrite(columns []string, types []string) error {
return nil
}
// WriteRow is write one JSONL.
func (w *JSONLWriter) WriteRow(values []any, columns []string) error {
m := orderedmap.New()
for i, col := range values {
m.Set(columns[i], compatibleJSON(col, w.needNULL, w.outNULL))
}
return w.writer.Encode(m)
}
// PostWrite does nothing.
func (w *JSONLWriter) PostWrite() error {
return nil
}