-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.go
146 lines (125 loc) · 3.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package hake
import (
"encoding/csv"
"fmt"
"strconv"
"cloud.google.com/go/spanner"
structpb "github.com/golang/protobuf/ptypes/struct"
gspanner "google.golang.org/genproto/googleapis/spanner/v1"
)
// RecordWriter writes a record.
// csv.Writer implements RecordWriter.
type RecordWriter interface {
Write(record []string) error
}
var _ RecordWriter = (*csv.Writer)(nil)
// Writer is writes *spanner.Row to Recordwriter.
type Writer struct {
w RecordWriter
header bool
writtenHeader bool
}
// NewWriter creates a Writer.
func NewWriter(w RecordWriter, header bool) *Writer {
return &Writer{
w: w,
header: header,
}
}
// Write writes a row of spanner to RecordWriter.
// If it is first time to write, Write writes also a header before writing a row.
// When second argument of NewWriter is false, the header would be omit.
//
// Example with csv.Writer:
// func query(ctx context.Context, w io.Writer, client *spanner.Client) error {
// stmt := spanner.Statement{SQL: `SELECT * FROM mytable`}
// iter := client.Single().Query(ctx, stmt)
// defer iter.Stop()
// cw := csv.NewWriter(w)
// hw := hake.NewWriter(cw, true)
// for {
// row, err := iter.Next()
// switch {
// case err == iterator.Done:
// return nil
// case err != nil:
// return err
// }
//
// if err := hw.Write(row); err != nil {
// return err
// }
// cw.Flush()
// }
// }
func (w *Writer) Write(row *spanner.Row) error {
if row == nil {
return nil
}
if err := w.validateTypes(row); err != nil {
return err
}
if w.header && !w.writtenHeader {
w.writtenHeader = true
if err := w.writeHeader(row); err != nil {
return err
}
}
cols, err := Columns(row)
if err != nil {
return err
}
record := make([]string, len(cols))
for i := range cols {
v, err := w.value(cols[i])
if err != nil {
return err
}
record[i] = v
}
if err := w.w.Write(record); err != nil {
return err
}
return nil
}
func (w *Writer) writeHeader(row *spanner.Row) error {
return w.w.Write(row.ColumnNames())
}
func (w *Writer) validateTypes(row *spanner.Row) error {
types, err := Types(row)
if err != nil {
return err
}
for i := range types {
if err := w.validateType(types[i]); err != nil {
return err
}
}
return nil
}
func (w *Writer) validateType(t *gspanner.Type) error {
switch t.Code {
case gspanner.TypeCode_INT64, gspanner.TypeCode_FLOAT64,
gspanner.TypeCode_STRING, gspanner.TypeCode_BOOL,
gspanner.TypeCode_DATE, gspanner.TypeCode_TIMESTAMP,
gspanner.TypeCode_BYTES:
return nil
}
return fmt.Errorf("unsupported type: %v", t)
}
func (w *Writer) value(c *spanner.GenericColumnValue) (string, error) {
if _, isNull := c.Value.Kind.(*structpb.Value_NullValue); isNull {
return "", nil
}
switch c.Type.Code {
case gspanner.TypeCode_INT64, gspanner.TypeCode_STRING,
gspanner.TypeCode_DATE, gspanner.TypeCode_TIMESTAMP,
gspanner.TypeCode_BYTES:
return c.Value.GetStringValue(), nil
case gspanner.TypeCode_FLOAT64:
return strconv.FormatFloat(c.Value.GetNumberValue(), 'f', -1, 64), nil
case gspanner.TypeCode_BOOL:
return strconv.FormatBool(c.Value.GetBoolValue()), nil
}
return "", fmt.Errorf("unsupport type: type:%v value:%T", c.Type, c.Value.Kind)
}