/
protobuf.go
119 lines (107 loc) · 2.83 KB
/
protobuf.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2020 The SQLFlow Authors. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tablewriter
import (
"fmt"
"io"
"github.com/golang/protobuf/proto"
pb "sqlflow.org/sqlflow/go/proto"
)
// ProtobufWriter write table as protobuf text format
type ProtobufWriter struct {
out io.Writer
head map[string]interface{}
rows [][]interface{}
bufSize int
hasWrittenHeader bool
}
// NewProtobufWriter returns ProtobufWriter
func createProtobufWriter(bufSize int, out io.Writer) *ProtobufWriter {
return &ProtobufWriter{
out: out,
head: make(map[string]interface{}),
rows: [][]interface{}{},
bufSize: bufSize,
}
}
// SetHeader set the table header
func (table *ProtobufWriter) SetHeader(head map[string]interface{}) error {
table.head = head
return nil
}
// AppendRow appends row into buffer
func (table *ProtobufWriter) AppendRow(row []interface{}) error {
table.rows = append(table.rows, row)
if len(table.rows) >= table.bufSize {
if e := table.Flush(); e != nil {
return e
}
}
return nil
}
// Flush the buffer to writer
func (table *ProtobufWriter) Flush() error {
if e := table.writeHead(); e != nil {
return e
}
if e := table.writeRows(); e != nil {
return e
}
table.rows = [][]interface{}{}
return nil
}
// FlushWithError flushes the buffer and end with the error message
func (table *ProtobufWriter) FlushWithError(err error) error {
if e := table.Flush(); e != nil {
return nil
}
response, e := pb.EncodeMessage(fmt.Sprintf("%v", err))
if e != nil {
return e
}
return table.formatWrite(response)
}
func (table *ProtobufWriter) writeRows() error {
for _, row := range table.rows {
response, e := pb.EncodeRow(row)
if e != nil {
return e
}
table.formatWrite(response)
}
return nil
}
func (table *ProtobufWriter) formatWrite(msg proto.Message) error {
if e := proto.CompactText(table.out, msg); e != nil {
return e
}
if _, e := table.out.Write([]byte{'\n'}); e != nil {
return e
}
return nil
}
func (table *ProtobufWriter) writeHead() error {
if len(table.head) == 0 {
return nil
}
// skip write head if it has been written to table.out
if table.hasWrittenHeader {
return nil
}
response, e := pb.EncodeHead(table.head)
if e != nil {
return e
}
table.hasWrittenHeader = true
return table.formatWrite(response)
}