-
Notifications
You must be signed in to change notification settings - Fork 3
/
write.go
61 lines (51 loc) · 1.42 KB
/
write.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 filetypes
import (
"compress/gzip"
"fmt"
"io"
"github.com/apache/arrow/go/v17/arrow"
"github.com/cloudquery/filetypes/v4/types"
"github.com/cloudquery/plugin-sdk/v4/schema"
)
func (cl *Client) WriteTableBatchFile(w io.Writer, table *schema.Table, records []arrow.Record) error {
return types.WriteAll(cl, w, table, records)
}
func (cl *Client) WriteHeader(w io.Writer, t *schema.Table) (h types.Handle, retErr error) {
defer func() {
if msg := recover(); msg != nil {
switch v := msg.(type) {
case error:
retErr = fmt.Errorf("panic: %w [recovered]", v)
default:
retErr = fmt.Errorf("panic: %v [recovered]", msg)
}
}
}()
switch cl.spec.Compression {
case CompressionTypeNone:
return cl.filetype.WriteHeader(w, t)
case CompressionTypeGZip:
gw := gzip.NewWriter(w)
h, err := cl.filetype.WriteHeader(gw, t)
if err != nil {
return nil, err
}
return newClosableHandle(h, gw.Close), nil
default:
return nil, fmt.Errorf("unhandled compression type %s", cl.spec.Compression)
}
}
type closableHandle struct {
types.Handle
afterCloseFunc func() error
}
var _ types.Handle = (*closableHandle)(nil)
func newClosableHandle(h types.Handle, afterCloseFunc func() error) types.Handle {
return &closableHandle{Handle: h, afterCloseFunc: afterCloseFunc}
}
func (c *closableHandle) WriteFooter() error {
if err := c.Handle.WriteFooter(); err != nil {
return err
}
return c.afterCloseFunc()
}